首页 > 解决方案 > Javascript获取附近的蓝牙设备Mac地址

问题描述

我需要我的网络项目来获取附近的蓝牙设备 Mac 地址。我使用这段代码:

navigator.bluetooth.requestDevice(acceptAllDevices)
.then(device => {
console.log(device)
console.log('> Name:             ' + device.name);
console.log('> Id:               ' + device.id);
console.log('> Connected:        ' + device.gatt.connected);
})
.catch(error => {
console.log('Argh! ' + error);
});

当我运行这个函数得到这个选项卡:图片

对于不受支持的设备,将显示蓝牙 Mac 地址。但它的 name 属性将为空。

但是,对于已识别的设备,它会打印名称选项。我可以从名称选项中过滤 Mac 地址吗?

可能还有其他查询Mac地址的方法吗?

标签: javascriptwebbluetoothprogressive-web-apps

解决方案


Web 蓝牙的可用过滤器是“服务”、“名称”、“名称前缀”和“制造商数据”,如https://web.dev/bluetooth/#request中所述。

您可以在https://googlechrome.github.io/samples/web-bluetooth/device-info.html和https://googlechrome.github.io/samples/web-bluetooth/manufacturer-data-filter尝试一些组合。 html

关于 MAC 地址,取决于蓝牙设备。一些设备广播一个用于隐私的临时设备,一个“静态”设备,一个用于“device.name”的名称,而不是 MAC 地址。

您要访问哪个设备?检查about:bluetooth-internals#devices并尝试确定使其独特的一件事。


推荐阅读