Bluetooth serial client using Windows socket API


This post shows how you can discover paired Bluetooth devices, and communicate with them, using Windows socket API. The Windows socket API is available in .NET through the excellent 32feet.NET library.

This is how you can discover Bluetooth devices paired with Windows

client = new BluetoothClient();
devices = client.DiscoverDevices(10, true, true, false);

This is how you can connect with a device, and obtain a NetworkStream to read from

Guid MyServiceUuid = new Guid("{00001101-0000-1000-8000-00805F9B34FB}");
client.Connect(devices[0].DeviceAddress, MyServiceUuid);
NetworkStream stream = client.GetStream();
ReadAsync(stream);

Here’s the implementation of ReadAsync

byte[] buffer = new byte[100];
while (true)
{
    try
    {
        int length = await stream.ReadAsync(buffer, 0, buffer.Length);
        // do something with buffer
    }
    catch
    {
        break;
    }
}

The application can send data at any time as follows

stream.Write(buffer, 0, buffer.Length);

The code above is available at GitHub as part of the Bluetooth Serial Client Tool.

bluetooth-serial-client-tool.PNG

Bluetooth serial server using Windows socket API


This post describes a means to simulate a Bluetooth serial device on Windows. This can be useful to test Bluetooth applications running on Android and Windows, that use a virtual serial port to communicate with devices.

Windows Bluetooth socket API can be used to create a server (listener). I use 32feet.NET here, a neat .NET library layered over the C/C++ socket APIs provided by Microsoft.

Here’s how you can create a Bluetooth listener on the primary adapter/radio

Guid MyServiceUuid = new Guid("{00001101-0000-1000-8000-00805F9B34FB}");
BluetoothListener listener = new BluetoothListener(MyServiceUuid); // Listen on primary radio
listener.Start();
listener.BeginAcceptBluetoothClient(acceptBluetoothClient, null);

The acceptBluetoothClient callback will be called when a client connects, and may be implemented as follows

if (listener == null) return;
client = listener.EndAcceptBluetoothClient(ar);
stream = client.GetStream();
ReadAsync(stream);

ReadAsync is an async method that continuously receives data over the Bluetooth socket, and does something useful with it

byte[] buffer = new byte[100];
while (true)
{
    try
    {
        int length = await stream.ReadAsync(buffer, 0, buffer.Length);
        // do something useful with data in buffer
    }
    catch
    {
        break;
    }
}

The application can send data at any time as follows

stream.WriteAsync(buffer, 0, buffer.Length);

As a bonus to the reader who’s come this far, the code above is available at GitHub as part of the Bluetooth Serial Server Tool.

bluetooth-spp-tool.PNG

View USB device descriptors on Windows


I have occasionally fired up a Linux virtual machine just to view USB descriptors of devices using lsusb -v. This post briefly describes a couple of tools for Windows that can be used to view descriptors of USB devices.

Thesycon USB Descriptor Dumper

This single purpose utility, from a German device driver development company, does what it proposes. It lists all connected USB devices and dumps the information for the selected device.

thesycon-usb-dd.PNG

USBView

This tool is part of the Windows Driver Kit (WDK) and Debugging Tools for Windows. Its source code is available as part of the Windows driver samples GitHub repo.

usbview.PNG

Inquire Bluetooth service record on Windows


Linux distros have excellent built-in support for inquiring characteristics of Bluetooth devices around you, using hcitool and sdptool. The closest thing on Windows is the Bluetooth Inquiry Record Verifier tool (sdpverify.exe) that ships with Windows Driver Kit (WDK). Beware, WDK is a rather large download.

Screen Shot 2016-12-06 at 10.27.36.png