0

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

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);       
} 

总是要连接,我调用对话框并在那里选择我需要的设备。我可以以编程方式获取已连接设备的列表吗?为了不每次都调用一个对话框?

4

1 回答 1

6

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

20 年 6 月 22 日编辑:

我最近实现了一个新的权限后端以及两个 API,它们将使以前允许的蓝牙设备能够被使用。

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

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

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

于 2020-03-09T22:04:51.070 回答