扫描附近蓝牙低功耗设备的广告是我的首要任务。
我尝试的代码要求设备(它想要配对以获取特性)。
但是我的用例是听他们的广告。我找到了这个代码
function recordNearbyBeacon(major, minor, pathLossVs1m) { ... }
navigator.bluetooth.requestLEScan({
filters: [{manufacturerData: {0x004C: {dataPrefix: new Uint8Array([
0x02, 0x15, // iBeacon identifier.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 // My beacon UUID.
])}}}],
options: {
keepRepeatedDevices: true,
}
}).then(() => {
navigator.bluetooth.addEventListener('advertisementreceived', event => {
let appleData = event.manufacturerData.get(0x004C);
if (appleData.byteLength != 23) {
// Isn’t an iBeacon.
return;
}
let major = appleData.getUint16(18, false);
let minor = appleData.getUint16(20, false);
let txPowerAt1m = -appleData.getInt8(22);
let pathLossVs1m = txPowerAt1m - event.rssi;
recordNearbyBeacon(major, minor, pathLossVs1m);
});
})
我尝试了一些官方网站上的uuid,但似乎没有任何作用。
我想从广告数据中获取特征值。如何获取Javascript中的数据?提前致谢。
最佳答案
根据Web Bluetooth GitHub上的implementation status文档,Web Bluetooth Scanning API(提供requestLEScan
方法的东西)尚未在任何浏览器中实现。