jueves, 4 de diciembre de 2014

C# - How to scan for Bluetooth devices (using 32feet.NET)



Hi again!



I received a comment from a friend about if it possible to scan for Bluetooth devices using C#. So I started the research looking for options and then I found “32feet.NET”. You can read the full description and download the project here:







#setup



Follow the steps described in the page and install the project. After this, make sure the Bluetooth port is turned on in your computer



#codeTime!



Add a dataGridView and a button to your Form. Then adapt the following code to a VS project:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using InTheHand.Net;
using System.IO;


namespace BT
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            scan();
        }
        private void scan()
        {
            BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
            BluetoothClient client = new BluetoothClient();
            BluetoothDeviceInfo[] devices = client.DiscoverDevices();
            BluetoothClient bluetoothClient = new BluetoothClient();
            String authenticated;
            String classOfDevice;
            String connected;
            String deviceAddress;
            String deviceName;
            String installedServices;
            String lastSeen;
            String lastUsed;
            String remembered;
            String rssi;
            foreach (BluetoothDeviceInfo device in devices)
            {
                authenticated = device.Authenticated.ToString();
                classOfDevice = device.ClassOfDevice.ToString();
                connected = device.Connected.ToString();
                deviceAddress = device.DeviceAddress.ToString();
                deviceName = device.DeviceName.ToString();
                installedServices = device.InstalledServices.ToString();
                lastSeen = device.LastSeen.ToString();
                lastUsed = device.LastUsed.ToString();
                remembered = device.Remembered.ToString();
                rssi = device.Rssi.ToString();
                string[] row = new string[] { authenticated, classOfDevice, connected, deviceAddress, deviceName, installedServices, lastSeen, lastUsed, remembered, rssi };
                dataGridView1.Rows.Add(row);
            }
        }
    }
}


Click your “Scan” button and wait for few seconds to get data in your dataGridView. This is my working result showing my phone’s details:

Enjoy!

4 comentarios:

  1. An example of an imaging system is the type used in desktop publishing this device scans in artwork or photos that can then be positioned within a page of text. barcode scanning devices

    ResponderBorrar