获取UWP中已知蓝牙设备的COM端口名称

时间:2017-06-01 12:51:55

标签: c# bluetooth uwp serial-port

我使用DeviceWatcher获取UWP应用中配对蓝牙设备的DeviceInformation。我像这样设置了DeviceWatcher

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();

当调用DeviceWatcher_Added事件处理程序时,我通过检查设备名称来检查设备是否是我感兴趣的设备,并提供RfcommServiceId.SerialPort.Uuid服务。

一旦我拥有蓝牙设备的DeviceInformation,我感兴趣的是如何获得它的COM端口?我可以在设备管理器中看到它,它被列为"标准串口通过蓝牙链接(COM8)",但我看不出如何得到它" COM8"在UWP中以编程方式。

我已经尝试将DeviceInformation设置为SerialDevice,然后我可以获得SerialDevice.PortName(cf this answer)但我对SerialDevice.FromIdAsync(deviceInfo.Id)的调用因System.Exception而失败:数据无效。

(N.B。一些诱人的答案,如thisthis,使用Windows Management Intrumentation WMI函数,但这些函数在UWP中不可用。)

1 个答案:

答案 0 :(得分:3)

another question Rita建议查看Serial UART sample,这有助于我找到一种方法。我不会将此标记为一段时间的答案,因为它似乎过于间接而不是规范的方式。

虽然我的UWP应用中的配对蓝牙设备有DeviceInformation,但我还需要SerialDevice的列表,以便我可以匹配它们。这是结果代码。

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 "";
}