这是测试文章内容
navigator.bluetooth.getAvailability().then((available) => {
if (available) {
console.log("This device supports Bluetooth!");
} else {
console.log("Doh! Bluetooth is not supported");
}
});
navigator.bluetooth.requestDevice({
optionalServices:['蓝牙设备服务ID'],
acceptAllDevices:true
}).then(async (device) => {
that.unCharDevice = device;
let server = await device.gatt.connect();
let service = await server.getPrimaryService('蓝牙设备服务ID')
let characteristic = await service.getCharacteristic('设备主动上报状态ID')
let unCharacteristic = await service.getCharacteristic('向设备写入命令ID')
characteristic.readValue();//读取命令
characteristic.startNotifications();//监听命令下发后的返回值
//监听蓝牙设备命令下发后的返回值
characteristic.addEventListener('characteristicvaluechanged',item => {
console.log(item)
})
})
.catch(error => {
alert('报错'+error)
})
navigator.bluetooth.requestDevice({
optionalServices:['蓝牙设备服务ID'],
acceptAllDevices:true
}).then(async (device) => {
device.fore
})
.catch(error => {
alert('报错'+error)
})
// 下发命令
unCharacteristic.writeValue(
new Uint8Array('下发的命令byte数组')
)
// 断开连接
that.unCharDevice.addEventListener('gattserverdisconnected', event => {
const device = event.target;
console.log(`Device ${device.name} is disconnected.`);
});
return device.gatt.connect();
血氧