Hugo DB
Hugo DB

Reputation: 23

Retrieve name of bluetooth device from rfcomm device services

The context is the following, we have multiple trucks that contains a bluetooth to serial device, we have given each truck bluetooth a unique name to be able to connect to a specific truck.

I use this code to retrieve all the RFComm services :

DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort))

The problem is that all the DeviceInformation objects returned contains the name of the RFComm service in the Name property instead of the bluetooth device name. When my project was a Win 8 store app, all was fine since the name property contained the bluetooth device name.

I found that I could create a BluetoothDevice object using the device id returned by the above code, but then the app ask to use the bluetooth device for all devices until I find the good one. I would like to prevent that as it wasn't the case with Win 8 store app.

Second solution I found was to parse the device id of the RFComm service which look like this one

Bluetooth#Bluetooth00:c2:c6:56:b0:61-00:15:be:0f:02:d7#RFCOMM:00000000:{00001101-0000-1000-8000-00805f9b34fb}

to remove everything past "#RFCOMM" and use the DeviceInformation.CreateFromIdAsync() function. This works but I was wondering if there was a cleaner solution to my problem since parsing a string can be a real problem if the string format change.

Is there a way to retrieve the name of the bluetooth device without having to ask to use all bluetooth device until we find it?

Upvotes: 2

Views: 2398

Answers (1)

Jay Zuo
Jay Zuo

Reputation: 15758

You can try with following code to get the name of the Bluetooth device:

var serviceInfoCollection = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort), new string[] { "System.Devices.AepService.AepId" });

foreach (var serviceInfo in serviceInfoCollection)
{
    var deviceInfo = await DeviceInformation.CreateFromIdAsync((string)serviceInfo.Properties["System.Devices.AepService.AepId"]);

    System.Diagnostics.Debug.WriteLine($"Device name is: '{deviceInfo.Name}' and Id is: '{deviceInfo.Id}'");
}

The key point here is that Bluetooth device is one type of AssociationEndpoint objects. AEPs usually represent a device discovered over a wireless or network protocol. An AssociationEndpoint object is a child of a single AssociationEndpointContainer object and can contain 0 or more AssociationEndpointService objects. And RFComm service is one AssociationEndpointService that Bluetooth device contains. For more info, please see DeviceInformationKind enumeration and Enumerate devices over a network.

AssociationEndpointService has several properties. One of them is System.Devices.AepService.AepId which represents the identifier of the parent AssociationEndpoint object. So we can use this property to get the Bluetooth device information and once we get the device information, we can get the device name easily. However System.Devices.AepService.AepId property is not a commen property in DeviceInformation. So we need to use DeviceInformation.FindAllAsync(String, IIterable(String)) method to require this additional propertie. For more info, please see Device information properties.

Upvotes: 1

Related Questions