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

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

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

最佳答案

(好吧,这个问题让我掉进了兔子洞...)

几年后,我们开始从 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);
  });
}

祝你好运!

关于google-chrome-extension - Web Bluetooth API 获取附近所有可用设备的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50965008/

相关文章:

javascript - 如何增加通知时间

javascript - "Chrome PDF viewer"扩展 "mhjfbmdgcfjbbpaeojofohoefgiehjai"位于何处?

c++ - 浏览器扩展与系统托盘应用

iphone - 与外部设备的蓝牙通信

android - 如何设置蓝牙打印的字体大小?

google-chrome-extension - chrome.tabs.executeScript引发错误 “Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url …”

android - 蓝牙延迟和 Android 音频的提示

javascript - 如何为 Imagus 悬停缩放扩展开发自定义过滤器?

javascript - 如何在没有不安全的内联 JavaScript/CSS 代码的情况下使用 React?