【问题标题】:How to initiate a new device pairing using the Chrome web bluetooth API?如何使用 Chrome 网络蓝牙 API 启动新设备配对?
【发布时间】:2019-01-27 20:04:34
【问题描述】:

使用navigator.bluetooth.requestDevice(),我可以访问我已经使用本机 OSX 蓝牙配对设置配对的蓝牙设备,但没有出现以前未配对的设备,即使它们的属性与我的通用查询匹配。可用设备以 Chrome 模式显示,请求用户同意配对,但唯一显示的设备是我已经配对的设备。

我是否误解了此处的预期用例,或者是否有其他方法可以通过 Chrome 与附近的(以前未配对的)设备建立连接?

文档:https://webbluetoothcg.github.io/web-bluetooth/ (见示例 2)

function bluetoothConnect() {
  navigator.bluetooth.requestDevice({filters: [{services: ['generic_access']}]})
	  .then(device => {console.log(`Connected to: ${device.name}`)})
	  .catch(console.error);
}

【问题讨论】:

  • 嗨.. 希望您找到了答案,看来您在这个问题上工作了很长时间。如果您还记得,您实际上是如何通过 api 取消配对 BLE 设备的?

标签: javascript google-chrome web-bluetooth


【解决方案1】:

首先,我们所说的 Mac OS X 还没有完全实现。目前只有发现和 GATT 服务器连接/断开连接有效。请参阅https://github.com/WebBluetoothCG/web-bluetooth/blob/gh-pages/implementation-status.md 上的 Chrome 实施状态。查看 Chrome OS、Linux 和 Android M(Android Lollipop 解决方法)。

关于您的具体问题,我认为generic_access 不是由附近的 BLE 设备广播的,而是因为您已经配对(缓存)了此设备而被发现。例如,如果您的设备名为“foo”,您可以转到https://googlechrome.github.io/samples/web-bluetooth/device-info.html 并填写“foo”作为设备名称,然后点击“获取蓝牙设备信息”按钮。

我建议您也尝试一下https://googlechrome.github.io/samples/web-bluetooth/index.html 上的所有网络蓝牙示例。

【讨论】:

    【解决方案2】:

    为了补充其他答案,请注意 Chrome 的 chrome://bluetooth-internals 选项卡/工具。此工具不仅可以列出可用设备及其服务(我认为您无法使用 Web 蓝牙 API 完成,除非您在 optionalServicesfilters/services 中请求了服务)以及这些服务的特征。

    在设备列表中,有一个 忘记 按钮,一旦您使用 Inspect 按钮连接到设备 GATT 服务器,该按钮就会激活。这应该允许您在需要端到端测试流程的情况下完全取消配对并再次配对。

    编辑:实际上在对此进行了一些实验之后,即使使用了“忘记”按钮,设备仍然保持配对状态。可能是 Chrome 中的一个错误,但是,配对设备的缓存似乎是每个配置文件。这意味着真正的答案是:

    使用隐身模式,配对流程将从一开始就为每个新的隐身会话启动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 2017-09-08
      相关资源
      最近更新 更多