当前位置:   article > 正文

html获取蓝牙设备,Web Bluetooth 读写蓝牙串口

navigator.bluetooth.requestdevice

HTML 定义触发按钮

www/index.html

Web Bluetooth

Send And Receive!

navigator.bluetooth.requestDevice 必须在某个用户触发的回调内执行,例如在 button 元素的 onclick 事件中调用。在其它环境下调用将产生错误:

Must be handling a user gesture to show a permission request

JS 操作 Web Bluetooth

www/js/web-bluetooth.js

async function sendAndReceive() {

let device = await navigator.bluetooth.requestDevice({

filters: [

{services: [0xffe0]},

]

});

let server = await device.gatt.connect();

let service = await server.getPrimaryService(0xffe0);

let characteristic = await service.getCharacteristic(0xffe1);

characteristic.addEventListener(

'characteristicvaluechanged', e => {

console.log(e.target.value);

}

);

characteristic.startNotifications();

await characte

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/1011078
推荐阅读