【发布时间】:2017-05-20 08:12:06
【问题描述】:
我想获得使用 UWP 的蓝牙设备。我真的很想使用Devicewatcher,因为我应该继续寻找新设备。
我在DeviceWatcher 的Added 事件中得到的结果是DeviceInformation,并且我的应用程序中没有我需要的所有属性,因此我应该调用一些东西来获取BluetoothDevice 对象。问题是当我使用从DeviceWatcher 得到的Device.ID 调用以下方法时,会出现不同的错误:
-
BluetoothDevice.fromBluetoothAddressAsync()的设备 ID 永远不会调用done方法。 -
BluetoothDevice.fromIdAsync()引发 Element not Found 错误。
正如我在另一篇文章中所读到的,我还尝试从DeviceInformation.findAllAsync() 获取设备,它们都调用了上述两种方法,但结果相同。 (winjs-and-bluetooth-connection-error)
任何人都可以提出问题所在或其他方法吗?我的代码如下。
创建观察者的代码:
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();
从 findAllAsync 获取设备的代码:
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
})
【问题讨论】:
-
"我在另一篇文章中读到,"...那篇文章是什么?请编辑问题以将该文本链接到帖子...
-
添加了另一个帖子的链接。
标签: javascript bluetooth uwp winjs