我正在寻找一个API调用,可以用来检索我附近的所有蓝牙设备。这些示例 Web Bluetooth 谈论从单个蓝牙设备获取不同特征,然而我没有找到任何检索所有蓝牙设备的示例(例如Windows本地蓝牙应用程序中的可用蓝牙设备列表)。它是否被支持?
我正在寻找一个API调用,可以用来检索我附近的所有蓝牙设备。这些示例 Web Bluetooth 谈论从单个蓝牙设备获取不同特征,然而我没有找到任何检索所有蓝牙设备的示例(例如Windows本地蓝牙应用程序中的可用蓝牙设备列表)。它是否被支持?
(好的,这个问题让我陷入了一个兔子洞……)
几年后,我们开始看到来自api.Bluetooth.getDevices
的生命迹象。我最终在Chrome中成功地实现了一个实验性演示。
您可能需要启用Chrome的“Web蓝牙实验”。 (Firefox也有类似的功能,但我没有尝试过。)
转到chrome://flags/
,然后搜索蓝牙
并启用API。
单击演示页面上的按钮: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!