UWP蓝牙:DeviceWatcher和findAllAsync()问题

时间:2017-01-05 17:13:40

标签: javascript bluetooth uwp winjs

我想使用UWP获取蓝牙设备。我真的想使用Devicewatcher因为我应该继续寻找新设备。

我在Added的{​​{1}}事件中得到的结果是DeviceWatcher并且我的应用中没有我需要的所有属性,因此我应该调用一些东西来获取{ {1}}对象。问题是,当我从DeviceInformation获得的BluetoothDevice调用以下方法时出现了不同的错误:

    带设备ID的
  • Device.ID永远不会调用DeviceWatcher方法。
  • BluetoothDevice.fromBluetoothAddressAsync()引发找不到元素错误。

正如我在另一篇文章中所读到的那样,我也尝试从done获取设备,同时调用上述方法但结果相同。 (winjs-and-bluetooth-connection-error

任何人都可以提出问题或其他方法吗?我的代码如下。

创建观察者的代码:

BluetoothDevice.fromIdAsync()

从findAllAsync获取设备的代码:

DeviceInformation.findAllAsync()

获取BluetoothDevice的代码:

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

1 个答案:

答案 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 中添加蓝牙功能,例如:enter image description here

然后当我们第一次调用此方法时,系统将请求用户授予访问蓝牙设备的权限。如果用户选择,则此方法将返回表示设备的BluetoothDevice对象。但如果使用选择,则此方法将返回null