首页 > 解决方案 > 网络蓝牙:想要在使用 navigator.bluetooth.requestDevice 时过滤制造商数据

问题描述

我看到谷歌浏览器没有对制造商数据实施过滤。问题707635,似乎没有任何进展。

使用 navigator.bluetooth.requestDevice (https://webbluetoothcg.github.io/web-bluetooth/#example-filter-by-manufacturer-service-data时,Web 蓝牙规范有一个(不稳定的)规范用于过滤制造商数据

有谁知道这方面是否有任何进展或使这种过滤工作?

标签: bluetoothbluetooth-lowenergyweb-bluetooth

解决方案


根据我们的讨论,我在 Chrome 92 的 Web 蓝牙中添加了对制造商数据过滤器的支持。请参阅https://crbug.com/707635

这是一个例子:

// Filter Bluetooth devices from Google company with manufacturer data bytes
// that start with [0x01, 0x02].
navigator.bluetooth.requestDevice({
  filters: [{
    manufacturerData: [{
      companyIdentifier: 0x00e0,
      dataPrefix: new Uint8Array([0x01, 0x02])
    }]
  }],
  optionalServices: ['battery_service'] // Required to access service later.
})
.then(device => { /* … */ })
.catch(error => { console.error(error); });

您可以在 https://googlechrome.github.io/samples/web-bluetooth/manufacturer-data-filter.html 找到一个示例,在https://web.dev/bluetooth/#manufacturer-data-找到一些开发人员文档筛选

让我知道这是否适合你;)


推荐阅读