Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript UWP蓝牙:DeviceWatcher和findAllAsync()存在问题_Javascript_Bluetooth_Uwp_Winjs - Fatal编程技术网

Javascript UWP蓝牙:DeviceWatcher和findAllAsync()存在问题

Javascript UWP蓝牙:DeviceWatcher和findAllAsync()存在问题,javascript,bluetooth,uwp,winjs,Javascript,Bluetooth,Uwp,Winjs,我想使用UWP获得蓝牙设备。我真的很想使用Devicewatcher,因为我应该继续寻找新的设备 我在DeviceWatcher的Added事件中得到的结果是DeviceInformation并且没有我在应用程序中需要的所有属性,因此我应该调用一些东西来获取BluetoothDevice对象。问题是,当我使用从DeviceWatcher获取的Device.ID调用以下方法时,会出现不同的错误: BluetoothDevice.fromBluetoothAddressAsync()使用设备ID时

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

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

  • BluetoothDevice.fromBluetoothAddressAsync()
    使用设备ID时,永远不会调用
    done
    方法
  • BluetoothDevice.fromIdAsync()
    引发元素未找到错误
正如我在另一篇文章中读到的,我还尝试从
DeviceInformation.findalsync()
获取设备,所有这些都调用上述两种方法,但结果相同。()

有人能提出问题所在或其他方法吗?我的代码如下

创建观察者的代码: 从findAllAsync获取设备的代码: 是获取具有给定设备Id的对象的正确方法。在中,我们可以获取包含但不包含蓝牙地址信息的对象。所以不能在这里使用

但是,在使用
BluetoothDevice.fromIdAsync
方法时,我们需要注意,此方法需要蓝牙功能。请参见要求功能以及。因此,要使用此方法,我们需要在软件包中添加蓝牙功能。appxmanifest类似:


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

“正如我在另一篇文章中读到的,”…那篇文章是什么?请编辑问题以将该文本链接到帖子…将该链接添加到其他帖子。我今天将试一试。但我真的很想使用DeviceWatcher,因为我需要在启动过程后持续监视蓝牙设备是否打开或可被发现。我想如果我能找到某种方法从devicewatcher获取设备,然后创建Bluetooth设备。fromIDAsync那么它可能是一个完美的解决方案。@PriyankC您可以使用
devicewatcher
Bluetooth设备。fromIDAsync
方法。我已经测试过了,效果很好。嗨@PriyankC,有更新吗?我的回答解决了你的问题吗?这是一个好主意吗?
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();
var selector = Windows.Devices.Bluetooth.BluetoothDevice.getDeviceSelector();
Windows.Devices.Enumeration.DeviceInformation.findAllAsync(selector, null).done(function (data) {
    for (var i = 0; i < data.length; i++) {
        //My code here
    }
});
Windows.Devices.Bluetooth.BluetoothDevice.fromBluetoothAddressAsync(devid).done(
function(devinfo) {
    res = devinfo;
})
Windows.Devices.Bluetooth.BluetoothDevice.fromIdAsync(data.id).done(function(result){
    //my code here
});
Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceService.fromIdAsync(devId).done(function (result) {    
    //my code here
})