【问题标题】:Web Bluetooth - getting error while connect mobile device to web browser via bluetooth网络蓝牙 - 通过蓝牙将移动设备连接到网络浏览器时出错
【发布时间】:2018-04-30 14:17:22
【问题描述】:

我正在尝试通过蓝牙将移动请求数据发送到网络浏览器(笔记本电脑),所以我的第一步是将系统蓝牙连接到网络浏览器,但使用以下代码收到错误消息。或者有没有其他方法可以通过蓝牙将手机连接到网络浏览器以传输数据?

navigator.bluetooth.requestDevice().then(
function (d){console.log("found Device !!");}, 
function (e){console.log("Oh no !!",e);});

我在 chrome 中尝试了上面的代码。

错误信息:

TypeError: Failed to execute 'requestDevice' on 'Bluetooth': 1 argument required, but only 0 present

【问题讨论】:

标签: javascript bluetooth web-bluetooth


【解决方案1】:

您可能想阅读https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web,它显示了您必须通过的所有强制性选项:

例如,请求蓝牙设备广播蓝牙 GATT 电池服务就是这么简单:

navigator.bluetooth.requestDevice({ filters: [{ services: ['battery_service'] }] })
.then(device => { /* ... */ })
.catch(error => { console.log(error); });

如果您的蓝牙 GATT 服务不在标准化蓝牙 GATT 服务列表中,您可以 提供完整的蓝牙 UUID 或简短的 16 位或 32 位格式。

navigator.bluetooth.requestDevice({
  filters: [{
    services: [0x1234, 0x12345678, '99999999-0000-1000-8000-00805f9b34fb']
  }]
})
.then(device => { /* ... */ })
.catch(error => { console.log(error); });

你也可以 根据广播的设备名称请求蓝牙设备 使用 name 过滤器键,甚至是此名称的前缀 namePrefix 过滤键。请注意,在这种情况下,您还需要 定义optionalServices 键以便能够访问某些服务。如果 你不这样做,你稍后在尝试访问它们时会收到错误消息。

navigator.bluetooth.requestDevice({
  filters: [{
    name: 'Francois robot'
  }],
  optionalServices: ['battery_service']
})
.then(device => { /* ... */ })
.catch(error => { console.log(error); });

【讨论】:

    【解决方案2】:

    正如错误消息告诉您的,您需要为requestDevice(options) 方法提供一个选项对象。见https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多