我想使用UWP获取蓝牙设备。我真的想使用Devicewatcher
因为我应该继续寻找新设备。
我在Added
的{{1}}事件中得到的结果是DeviceWatcher
并且我的应用中没有我需要的所有属性,因此我应该调用一些东西来获取{ {1}}对象。问题是,当我从DeviceInformation
获得的BluetoothDevice
调用以下方法时出现了不同的错误:
Device.ID
永远不会调用DeviceWatcher
方法。BluetoothDevice.fromBluetoothAddressAsync()
引发找不到元素错误。正如我在另一篇文章中所读到的那样,我也尝试从done
获取设备,同时调用上述方法但结果相同。 (winjs-and-bluetooth-connection-error)
任何人都可以提出问题或其他方法吗?我的代码如下。
BluetoothDevice.fromIdAsync()
DeviceInformation.findAllAsync()
ondeviceadd = function (args) {
//my code here
};
deviceWatch = Windows.Devices.Enumeration.DeviceInformation.createWatcher(selector, null);
deviceWatch.addEventListener("added", ondeviceadd);
deviceWatch.addEventListener("removed", ondeviceRemove);
deviceWatch.addEventListener("updated", ondeviceUpdate);
deviceWatch.addEventListener("stopped", onStopped);
deviceWatch.addEventListener("enumerationcompleted", ondeviceComplete);
deviceWatch.start();
答案 0 :(得分:0)
BluetoothDevice.FromIdAsync | fromIdAsync method是获取具有给定设备ID的BluetoothDevice对象的正确方法。在DeviceWatcher.Added | added event中,我们可以获取包含DeviceInformation但没有蓝牙地址信息的DeviceInformation.Id | id property对象。因此,BluetoothDevice.FromBluetoothAddressAsync | fromBluetoothAddressAsync method无法使用。
但是,在使用BluetoothDevice.fromIdAsync
方法时,我们需要注意此方法需要蓝牙功能。请参阅要求→功能以及Device capabilities。因此,要使用此方法,我们需要在 Package.appxmanifest 中添加蓝牙功能,例如:
然后当我们第一次调用此方法时,系统将请求用户授予访问蓝牙设备的权限。如果用户选择是,则此方法将返回表示设备的BluetoothDevice
对象。但如果使用选择否,则此方法将返回null
。