1

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

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
4

2 回答 2

3

您可能想阅读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); });
于 2017-02-09T06:50:22.013 回答
1

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

于 2017-02-09T06:49:48.353 回答