Web Bluetooth API获取附近所有可用设备的列表

时间:2018-06-21 09:25:07

标签: google-chrome-extension bluetooth browser-extension bluetooth-gatt web-bluetooth

我正在寻找一个API调用,该调用可用于检索我附近的所有Bluetooth设备。这些示例Web Bluetooth讨论了如何从单个Bluetooth设备获得不同的特性,但是我找不到任何示例可以检索所有Bluetooth设备(例如Windows本机Bluetooth应用程序上的可用蓝牙设备列表)。甚至支持吗?

3 个答案:

答案 0 :(得分:1)

我个人还没有尝试过Web蓝牙API,但是我认为您正在寻找Device DiscoveryRequest Bluetooth Devices

  

此版本的Web蓝牙API规范允许网站,   以中央角色运行,以通过以下方式连接到远程GATT服务器   BLE连接。它支持实现的设备之间的通信   蓝牙4.0或更高版本。

     

网站请求使用以下设备访问附近的设备时:   navigator.bluetooth.requestDevice,Google Chrome会提示用户   设备选择器,他们可以选择一个设备或简单地取消   请求。

     

navigator.bluetooth.requestDevice函数具有强制性   定义过滤器的对象。这些过滤器仅用于返回   与某些宣传的Bluetooth GATT服务和/或   设备名称。

此外,这是GATT Services的列表。

答案 1 :(得分:1)

存在Web Bluetooth Scanning规范草案,但是implementation isn't started截至2018年的任何地方。

答案 2 :(得分:0)

(嗯,这个问题把我送进了一个兔子洞...)

已经过了几年,我们开始从api.Bluetooth.getDevices看到生命迹象。我最终得到了一个在Chrome中进行实验的演示。

您可能需要启用Chrome的“网络蓝牙实验”。 (Firefox具有等效功能,但我没有尝试过。)

  1. 转到chrome://flags/,然后搜索bluetooth并启用API。

  2. 单击演示页面上的按钮:Web Bluetooth / Get Devices Sample


在同一页面上的

演示代码

function onGetBluetoothDevicesButtonClick() {
  log('Getting existing permitted Bluetooth devices...');
  navigator.bluetooth.getDevices()
  .then(devices => {
    log('> Got ' + devices.length + ' Bluetooth devices.');
    for (const device of devices) {
      log('  > ' + device.name + ' (' + device.id + ')');
    }
  })
  .catch(error => {
    log('Argh! ' + error);
  });
}

function onRequestBluetoothDeviceButtonClick() {
  log('Requesting any Bluetooth device...');
  navigator.bluetooth.requestDevice({
 // filters: [...] <- Prefer filters to save energy & show relevant devices.
    acceptAllDevices: true
  })
  .then(device => {
    log('> Requested ' + device.name + ' (' + device.id + ')');
  })
  .catch(error => {
    log('Argh! ' + error);
  });
}

祝你好运!