【问题标题】:Web Bluetooth discovering by Service uuid通过服务 uuid 发现 Web 蓝牙
【发布时间】:2016-03-17 16:25:48
【问题描述】:

我正在尝试编写一个网络应用程序来与 Eddystone 信标交互。信标广告应用程序的 URL 并公开服务和 2 个特征。

我的设备具有以下服务和特性,如 linux 上的 gatttool 所示

$ gatttool -I 
[00:1A:7D:DA:71:15][LE]> connect 00:1A:7D:DA:71:15 
Attempting to connect to 00:1A:7D:DA:71:15 
Connection successful 
[00:1A:7D:DA:71:15][LE]> primary 
attr handle: 0x0001, end grp handle: 0x0005 uuid: 00001800-0000-1000-8000-00805f9b34fb 
attr handle: 0x0006, end grp handle: 0x0009 uuid: 00001801-0000-1000-8000-00805f9b34fb 
attr handle: 0x000a, end grp handle: 0x0012 uuid: ba42561b-b1d2-440a-8d04-0cefb43faece 
[00:1A:7D:DA:71:15][LE]> characteristics 
handle: 0x0002, char properties: 0x02, char value handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb 
handle: 0x0004, char properties: 0x02, char value handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb 
handle: 0x0007, char properties: 0x20, char value handle: 0x0008, uuid: 00002a05-0000-1000-8000-00805f9b34fb 
handle: 0x000b, char properties: 0x1a, char value handle: 0x000c, uuid: 6bcb06e2-7475-42a9-a62a-54a1f3ce11e6 
handle: 0x000f, char properties: 0x1a, char value handle: 0x0010, uuid: 6bcb06e2-7475-42a9-a62a-54a1f3ce11e5 
[00:1A:7D:DA:71:15][LE]

我正在使用示例 here

if (navigator.bluetooth) {
  console.log("bluetooth found");
  navigator.bluetooth.requestDevice({
    filters: [{
      services: ['ba42561b-b1d2-440a-8d04-0cefb43faece']
    }]
  })
  .then(device => {
    bluetoothDevice = device;
    console.log(device.name);
    console.log(device.uuids);
    device.connectGATT();
  })
  .then(server => {
    return server.getPrimaryService('ba42561b-b1d2-440a-8d04-0cefb43faece');
  })
  .then(service => {
    return service.getCharacteristic('6bcb06e2-7475-42a9-a62a-54a1f3ce11e6');
  })
  .then( characteristic => {
    var toggleOn = new Uint8Array([1]);
    return characteristic.writeValue(toggleOn);
  })
  .catch(error => { 
    console.log("error:- " + error); 
  });
}

此代码永远找不到设备。如果我将 过滤器 更改为使用 namePrefix,它会找到设备,但只列出其中一项服务,而不是我感兴趣的服务。

有人知道我在发现和寻找正确服务方面做错了什么吗?

【问题讨论】:

    标签: google-chrome bluetooth-lowenergy web-bluetooth


    【解决方案1】:

    filters 参数主要根据设备发送的广告数据包过滤设备,而不是设备的 gatt 服务器实际公开的服务。因此,如果信标只是宣传一个 url 及其名称,您不太可能通过过滤特定服务 UUID 来找到它。

    我说“不太可能”而不是“你不会”,因为如果您已经连接到设备一次,您的系统可能会缓存设备实际公开的服务集,然后使用这些服务来匹配过滤器。

    如果您切换到namePrefix 参数,您需要记住在optionalServices 中列出您要使用的所有服务:

    navigator.bluetooth.requestDevice({
      filters: [{
        namePrefix: 'something',
      }],
      optionalServices: ['ba42561b-b1d2-440a-8d04-0cefb43faece']
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多