Categories
Bluetooth Windows Phone

Persist Bluetooth Addresses on Windows Phone 8

The Bluetooth API on Windows Phone 8 is based around some customisations to the Proximity APIs which are part of the Windows API introduced with Windows 8. When you “discover” devices you can retrieve a collection of device identifiers for paired devices. The first time you perform a task you will want to use this mechanism to select the appropriate device. However you may want your app to remember a specific device and try to connect again on subsequent attempts. In the old .NET world with 32feet.NET we have a constructor for BluetoothAddress which allows you to supply the address in a variety of string or numeric forms. In the Windows API the Windows.Networking.HostName type is used to represent a host name whether it is for a Bluetooth device or an IP address. Normally the read-only Type property will indicate the type of the host.

In order to store a Bluetooth address for later use you should save the RawName property which for Bluetooth devices is in the form “(11:22:33:44:55:66)”. The dotted hex notation is fairly common but notice the address is wrapped in brackets. Now to restore this address in a HostName instance you can later use to open a socket you can use:-

HostName storedHostName = new HostName(storedRawName);

You’ll notice that this sets the host name and correctly sets the Type property to Bluetooth.

By Peter Foot

Microsoft Windows Development MVP