Obtaining a COM port name for a known Bluetooth device in UWP

I use DeviceWatcher to get DeviceInformation for a paired Bluetooth device in a UWP application. I installed DeviceWatcher this way

var requestedProperties = new string[] { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" }; var deviceWatcher = DeviceInformation.CreateWatcher("(System.Devices.Aep.ProtocolId:=\"{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}\")", requestedProperties, DeviceInformationKind.AssociationEndpoint); // ClassGuid = {e0cbf06c-cd8b-4647-bb8a-263b43f0f974} includes all Bluetooth devices deviceWatcher.Added += DeviceWatcher_Added; deviceWatcher.Updated += DeviceWatcher_Updated; deviceWatcher.Start(); 

When the DeviceWatcher_Added event handler is called, I check if the device is the one I'm interested in, checking its name and offering RfcommServiceId. SerialPort.Uuid .

As soon as I have DeviceInformation for a bluetooth device, I wonder how can I get a COM port? I see this in the Device Manager, where it is listed as โ€œStandard Serial Bluetooth Communication (COM8),โ€ but I donโ€™t see how to programmatically get this โ€œCOM8โ€ in UWP.

I tried to make DeviceInformation in SerialDevice, thanks to which I was able to get SerialDevice.PortName (cf this ), but my call to SerialDevice.FromIdAsync(deviceInfo.Id) fails with a System.Exception error: data is invalid.

(NB Some painful answers, such as this and, use the Windows WMI management toolkit, but they are not available in UWP.)

+7
c # bluetooth uwp serial-port
source share
1 answer

In another question, Rita suggested looking at the UART Sequential Pattern , which helped me see a way to do this. I will not mark this as an answer for a while, as it seems too indirect to be a canonical way.

Although I have DeviceInformation for a paired Bluetooth device in my UWP application, I also need a SerialDevice list so that I can match them. Here is the resulting code.

 public async Task<string> ComPort(DeviceInformation deviceInfo) { var serialDevices = new Dictionary<string, SerialDevice>(); var serialSelector = SerialDevice.GetDeviceSelector(); var serialDeviceInformations = (await DeviceInformation.FindAllAsync(serialSelector)).ToList(); var hostNames = NetworkInformation.GetHostNames().Select(hostName => hostName.DisplayName.ToUpper()).ToList(); // So we can ignore inbuilt ports foreach (var serialDeviceInformation in serialDeviceInformations) { if (hostNames.FirstOrDefault(hostName => hostName.StartsWith(serialDeviceInformation.Name.ToUpper())) == null) { try { var serialDevice = await SerialDevice.FromIdAsync(serialDeviceInformation.Id); if (serialDevice != null) { serialDevices.Add(deviceInfo.Id, serialDevice); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } } } // Example Bluetooth DeviceInfo.Id: "Bluetooth#Bluetooth9c:b6:d0:d6:d7:56-00:07:80:cb:56:6d" // from device with Association Endpoint Address: "00:07:80:cb:56:6d" var lengthOfTrailingAssociationEndpointAddresss = (2 * 6) + 5; var bluetoothDeviceAddress = deviceInfo.Id.Substring(deviceInfo.Id.Length - lengthOfTrailingAssociationEndpointAddresss, lengthOfTrailingAssociationEndpointAddresss).Replace(":", "").ToUpper(); var matchingKey = serialDevices.Keys.FirstOrDefault(id => id.Contains(bluetoothDeviceAddress)); if (matchingKey != null) { return serialDevices[matchingKey].PortName; } return ""; } 
+3
source share

All Articles