Working With Bluetooth Devices Using C# – Part 1
Working With Bluetooth Devices Using C# – Part 1
32feet.NET is a shared-source project to make personal area networking technologies such as Bluetooth, Infrared (IrDA) and more, easily accessible from .NET code.
Supports desktop, mobile or embedded systems. The project currently consists of the following libraries:-
- Bluetooth
- IrDA
- Object Exchange
In this post I’ll show how to find Bluetooth devices using C# code.
Download Demo Project
First download 32Feet.net assemblies, the create new project and add those assemblies.
Add using for using InTheHand.Net.Sockets;
BluetoothClient bc = new BluetoothClient();
Use BluetoothClient to discover bluetooth devices:
public BluetoothDeviceInfo[] DiscoverDevices();
public BluetoothDeviceInfo[] DiscoverDevices(int maxDevices);
public BluetoothDeviceInfo[] DiscoverDevices(int maxDevices, bool authenticated, bool remembered, bool unknown);
*** DiscoverDevices takes about 20-30 seconds to complete.
List<Device> devices = new List<Device>();
BluetoothClient bc = new BluetoothClient();
BluetoothDeviceInfo[] array = bc.DiscoverDevices();
int count = array.Length;
for (int i = 0; i < count; i++)
{
Device device = new Device(array[i]);
devices.Add(device);
}
public class Device
{
public string DeviceName { get; set; }
public bool Authenticated { get; set; }
public bool Connected { get; set; }
public ushort Nap { get; set; }
public uint Sap { get; set; }
public DateTime LastSeen { get; set; }
public DateTime LastUsed { get; set; }
public bool Remembered { get; set; }
public Device(BluetoothDeviceInfo device_info)
{
this.Authenticated = device_info.Authenticated;
this.Connected = device_info.Connected;
this.DeviceName = device_info.DeviceName;
this.LastSeen = device_info.LastSeen;
this.LastUsed = device_info.LastUsed;
this.Nap = device_info.DeviceAddress.Nap;
this.Sap = device_info.DeviceAddress.Sap;
this.Remembered = device_info.Remembered;
}
public override string ToString()
{
return this.DeviceName;
}
}
Download Demo Project