2017-02-09 120 views
1

我想通过蓝牙将移动请求数据传送到网络浏览器(笔记本电脑),因此我的第一步是将系统蓝牙连接到网络浏览器,但收到错误消息通过使用下面的代码。或者有没有其他的方式通过蓝牙连接手机到网页浏览器来传输数据?网络蓝牙 - 通过蓝牙将移动设备连接到网络浏览器时出现错误

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

我在铬上面的代码尝试。

错误消息:

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

如果你告诉我们该错误消息,我们能做的不仅仅是猜测更多。 – Connum

+0

@Connum - 是的,它是标题,我更新。 – 151291

+0

您需要为'requestDevice'方法的参数提供'options'对象。文档在https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice和https://googlechrome.github.io/samples/web-bluetooth/device-info.html应该会让你从正确的方向开始 –

回答

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服务列表中但是,您可能会提供完整的Bluetooth 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); });