【发布时间】:2017-11-02 15:13:26
【问题描述】:
我正在使用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)”,但我看不到如何以编程方式在 UWP 中获取该“COM8”。
我尝试将 DeviceInformation 设置为 SerialDevice,然后我可以得到 SerialDevice.PortName(参见 this 答案)但我对 SerialDevice.FromIdAsync(deviceInfo.Id) 的调用失败并出现 System.Exception: The data is invalid。
(注意,一些诱人的答案,例如 this 和 this,使用 Windows 管理工具 WMI 功能,但这些在 UWP 中不可用。)
【问题讨论】:
-
如何确保 deviceInfo.Id 是您设备的 id (COM8)?你能显示完整的代码使这个异常:
System.Exception: The data is invalid? -
完整代码是here
-
鉴于您已经知道
deviceInfo.Name,想知道您是否可以从中获得Id?(await DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelector())).Single(di => di.Name == deviceInfo.Name).Id -
基于your device info,蓝牙 rfcomm 是正确的方向,但为什么要“SerialDevice.PortName”?
-
这样我就可以调用依赖它的现有代码库了。
标签: c# bluetooth uwp serial-port