网络蓝牙,获取配对设备列表

Rum*_*ato 1 javascript bluetooth web web-bluetooth

我在我的项目中使用网络蓝牙,我使用此代码连接到设备

async function requestDevice() {
    bluetoothDevice = await navigator.bluetooth.requestDevice({
        // filters: [ { name: 'Device test' } ],
        acceptAllDevices: true,
        optionalServices: [
            'battery_service',
            '03b80e5a-ede8-4b33-a751-6ce34ec4c700'
        ]
    });

    bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);       
} 
Run Code Online (Sandbox Code Playgroud)

始终要进行连接,我会调用该对话框,然后在其中选择我需要的设备。我可以通过编程方式获取已连接设备的列表吗?为了不每次都调用对话框?

Ovi*_*uez 7

目前还没有办法在 Chrome 中获取蓝牙设备列表。不过,我正在努力实现一种getDevices()蓝牙方法。请关注Chrome 平台状态中的状态

2020 年 6 月 22 日编辑:

我最近实现了一个新的权限后端以及两个 API,可以使用之前允许的蓝牙设备。

新的权限后端是在 chrome://flags/#enable-web-bluetooth-new-permissions-backend 后面实现的。新后端将保留授予的设备权限,requestDevice()直到在“站点设置”或“页面信息”对话框中重置权限为止。

对于 Chrome 85.0.4165.0 或更高版本,和在 chrome://flags/#enable-experimental-web-platform-features 标志后面实现getDevices()watchAdvertisements()建议使用这些 APIgetDevices()来检索允许的蓝牙设备数组,然后调用watchAdvertisements()这些设备来开始扫描。当从设备检测到广告数据包时,advertisementreceived将在其对应的设备上触发该事件。此时,蓝牙设备在范围内并且可以连接。

请尝试一下这个新功能,并使用 Blink>Bluetooth 组件在https://crbug.com上提交任何错误。