Web蓝牙API获取附近所有可用设备列表

3

我正在寻找一个API调用,可以用来检索我附近的所有蓝牙设备。这些示例 Web Bluetooth 谈论从单个蓝牙设备获取不同特征,然而我没有找到任何检索所有蓝牙设备的示例(例如Windows本地蓝牙应用程序中的可用蓝牙设备列表)。它是否被支持?

3个回答

2

(好的,这个问题让我陷入了一个兔子洞……)

几年后,我们开始看到来自api.Bluetooth.getDevices的生命迹象。我最终在Chrome中成功地实现了一个实验性演示。

您可能需要启用Chrome的“Web蓝牙实验”。 (Firefox也有类似的功能,但我没有尝试过。)

  1. 转到chrome://flags/,然后搜索蓝牙并启用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);
  });
}

Good luck!


1

1
我个人没有使用过Web Bluetooth API,但我认为你正在寻找设备发现请求蓝牙设备
这个版本的Web Bluetooth API规范允许处于中心角色的网站通过BLE连接连接到远程GATT服务器。它支持实现了Bluetooth 4.0或更高版本的设备之间的通信。
当网站使用navigator.bluetooth.requestDevice请求附近设备的访问权限时,Google Chrome将提示用户选择设备或取消请求。
navigator.bluetooth.requestDevice函数需要一个强制性的对象,该对象定义了筛选器。这些筛选器用于仅返回与某些广告蓝牙GATT服务和/或设备名称匹配的设备。
此外,这里是GATT服务列表。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接