我正在寻找可用于检索我附近所有蓝牙设备的 API 调用。这些样本 Web Bluetooth谈论从单个蓝牙设备获得不同的特性,但是我找不到任何检索所有蓝牙设备的示例(例如 Windows native 蓝牙应用程序上的可用蓝牙设备列表)。它甚至受支持吗?
最佳答案
(好吧,这个问题让我掉进了兔子洞...)
几年后,我们开始从 api.Bluetooth.getDevices
获得生命迹象。我最终得到了一个在 Chrome 中运行的实验性演示。
您可能需要启用 Chrome 的“网络蓝牙实验”。 (Firefox 有一个等价物,但我没试过。)
转到
chrome://flags/
,然后搜索bluetooth
并启用 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);
});
}
祝你好运!
关于google-chrome-extension - Web Bluetooth API 获取附近所有可用设备的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50965008/