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

dum*_*dad 6 c# serial-port bluetooth uwp

我正在使用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();
Run Code Online (Sandbox Code Playgroud)

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

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

我已经尝试将DeviceInformation设置为SerialDevice,然后我可以获得SerialDevice.PortName(参见答案)但我的调用SerialDevice.FromIdAsync(deviceInfo.Id)失败并出现System.Exception:数据无效.

(NB一些诱人的答案,像这样这个,使用Windows Management Intrumentation WMI功能,但这些在UWP中不可用.)

dum*_*dad 3

另一个问题上, Rita建议查看串行 UART 示例,这帮助我找到了实现此目的的方法。我暂时不会将此标记为答案,因为它似乎太间接而不是规范的方式。

虽然我的 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 "";
}
Run Code Online (Sandbox Code Playgroud)