uni.openBluetoothAdapter({
success() {
console.log('打开蓝牙适配器......');
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
interval: 300,
success() {
console.log('开始发现设备......');
uni.onBluetoothDeviceFound(({ devices }) => {
function ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(
-2
);
}
);
return hexArr.join('');
}
let device = devices[0];
device.hex = ab2hex(
device.advertisData
).toUpperCase();
// 这个F82022011301修改成自己设备的特定值即可,如果环境下只有一个蓝牙设备去掉也行
if (device.hex.includes('F82022011301')) {
console.log('发现设备......');
console.log('RSSI', device.RSSI);
}
});
},
});
},
});
1 个回复
浅水321
解决了吗