当前位置: 首页 > news >正文

Web前端之UniApp低功耗蓝牙一键开门、数组匹配数组、多对多查找、开锁

MENU

  • Html
  • JavaScript

Html

<viewclass="m_t_36"><viewclass="w_50_ h_100 lh_100 m_l_a m_r_a bc_409eff radius_10 color_fff ta_c"@click="openBluetoothAdapter()">一键开门</view></view>

JavaScript

exportdefault{data(){return{timeout:undefined,// 后端保存蓝牙信息bluetooths:[{deviceId:"0A:45:32:0C:78:C6",name:"YX_0A45320C78C6",serviceId:'0000FFB0-0000-1000-8000-00805F9B34FB',notifyId:'0000FFB2-0000-1000-8000-00805F9B34FB',writeId:'0000FFB1-0000-1000-8000-00805F9B34FB',instruction:'EE03E30100'},{deviceId:"5C:C3:36:8D:B9:FC",name:"RTK_BT_4.1v",serviceId:'0000FFB0-0000-1000-8000-00805F9B34FB',notifyId:'0000FFB2-0000-1000-8000-00805F9B34FB',writeId:'0000FFB1-0000-1000-8000-00805F9B34FB',instruction:'EE03E30100'}],bluetooth:{}}},methods:{/** * 根据不同方法名调用方法,统一定时器的触发判断, * 当定时器触发时,无法确定当前调用的方法是哪个 * @param {String} fnName */methodExecution(fnName=''){if(this.timeout){this[fnName]();}else{console.log('执行方法');}},// 数据初始化initInfo(){// 清除定时器无效的解决方案this.timeout=undefined;this.bluetooth={};},// 初始化蓝牙模块openBluetoothAdapter(){letthat=this;// 向低功耗蓝牙设备特征值中写入二进制数据。// 注意:必须设备的特征值支持 write 才可以成功调用。if(Object.keys(that.bluetooth).length)returnthat.writeBLECharacteristicValue();// 数据初始化that.initInfo();// 初始化蓝牙模块uni.openBluetoothAdapter({// 主服务的UUID是YX。传入这个参数,只搜索主服务UUID为YX的设备// services: ['YX'],success(){// 开始搜索附近的蓝牙设备that.startBluetoothDevicesDiscovery();},fail(){uni.showToast({icon:'none',title:'查看手机蓝牙是否打开'});}});},// 开始搜索附近的蓝牙设备startBluetoothDevicesDiscovery(){letthat=this;uni.showLoading({title:'加载中',mask:true});that.timeout=setTimeout(()=>{// 数据初始化that.initInfo();uni.hideLoading();uni.showToast({icon:'none',title:'开门失败'});},1000*20);// 开始搜索附近的蓝牙设备uni.startBluetoothDevicesDiscovery({success(){// 监听返回的蓝牙设备uni.onBluetoothDeviceFound(({devices})=>{if(devices.length){letbluetooths=that.bluetooths;// 多对多查找for(leti=0;i<devices.length;i++){letitemI=devices[i];if(itemI.name){for(letj=0;j<bluetooths.length;j++){letitemJ=bluetooths[i];if(itemI.deviceId===itemJ.deviceId&&itemI.name===itemJ.name){that.bluetooth=itemJ;setTimeout(()=>{that.createBLEConnection()},0);returnundefined;}}}}}});},fail(){that.methodExecution('startBluetoothDevicesDiscovery');}});},// 连接低功耗蓝牙设备createBLEConnection(){letthat=this;// 连接低功耗蓝牙设备uni.createBLEConnection({deviceId:that.bluetooth.deviceId,success(){// 停止搜寻附近的蓝牙外围设备uni.stopBluetoothDevicesDiscovery({success(){// 获取蓝牙服务// that.getBLEDeviceServices();// 启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值that.notifyBLECharacteristicValueChange();},fail(){}});},fail(){// 连接低功耗蓝牙设备that.methodExecution('createBLEConnection');}});},// 启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值notifyBLECharacteristicValueChange(){letthat=this,{deviceId,serviceId,notifyId}=that.bluetooth;// 启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值uni.notifyBLECharacteristicValueChange({state:true,deviceId,serviceId,characteristicId:notifyId,success(){// 监听低功耗蓝牙设备的特征值变化事件。// 必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification。that.onBLECharacteristicValueChange();},fail(){// 启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值that.methodExecution('notifyBLECharacteristicValueChange');}});},// 监听低功耗蓝牙设备的特征值变化事件。// 必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification。onBLECharacteristicValueChange(){letthat=this;// 监听低功耗蓝牙设备的特征值变化事件。// 必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification。uni.onBLECharacteristicValueChange(()=>{that.writeBLECharacteristicValue();});},// 向低功耗蓝牙设备特征值中写入二进制数据。// 注意:必须设备的特征值支持 write 才可以成功调用。writeBLECharacteristicValue(){letthat=this,{deviceId,serviceId,writeId,instruction}=that.bluetooth,typedArray=newUint8Array(instruction.match(/[\da-f]{2}/gi).map((h)=>parseInt(h,16))),buffer=typedArray.buffer;// 向低功耗蓝牙设备特征值中写入二进制数据。// 注意:必须设备的特征值支持 write 才可以成功调用。uni.writeBLECharacteristicValue({deviceId,serviceId,characteristicId:writeId,value:buffer,success(){uni.showToast({title:'开门成功',icon:'none'});},fail(){uni.showToast({title:'开门失败',icon:'none'});},complete(){clearTimeout(that.timeout);that.timeout=undefined;uni.hideLoading();}});},// 关闭(断开)蓝牙模块closeBluetoothAdapter(){letthat=this;// 关闭(断开)蓝牙模块uni.closeBluetoothAdapter({success:()=>{uni.showToast({title:'蓝牙已关闭',icon:'none'});clearTimeout(that.timeout);that.timeout=undefined;uni.hideLoading();that.initInfo();},fail(){that.closeBluetoothAdapter();}});},// 监听页面隐藏onHide(){// 关闭(断开)蓝牙模块// this.closeBluetoothAdapter();},// 监听页面卸载onUnload(){// 关闭(断开)蓝牙模块this.closeBluetoothAdapter();}}}

1、notifyBLECharacteristicValueChange方法用的是读取的特征值。
2、writeBLECharacteristicValue发送指令,返回成功,蓝牙设备无反应,大概率是指令问题。
3、关闭蓝牙有两个API,目前测试只有closeBluetoothAdapter彻底断开蓝牙。
4、setTimeout无法彻底清除定时器,需要手动设置属性值为undefined

http://www.jsqmd.com/news/446892/

相关文章:

  • Labelme安装以及qt.qpa.plugin: Could not load the Qt platform plugin “xcb“ in ““ even though it was f问题解决
  • CAS原理
  • 微信小程序TS+SASS使用vant导致体验版白屏SystemError (jsEnginScriptError) X(...).bem is not a function
  • 【节点】[Fog节点]原理解析与实际应用
  • 酷炫 css 按钮 边框霓虹
  • 软件设计师考试中需要掌握的一些常用算法,基于C++实现
  • Mybatis的延迟加载
  • 教程 | 如何动用智慧安装NavicatPremium 16
  • Python 潮流周刊#141:Python 早期贡献者口述历史
  • Web前端之旋转木马的图片效果、鼠标进入停止动画、keyframes、hover、child、nth
  • canvas_3_绘制弧形
  • linux-centos常用指令、tar.gz解压、mv重命名、cp复制、ss -ltnp、curl测试任意端口网络是否可达等
  • 女生必看!用OpenClaw龙虾当你的24小时免费助理,职场、生活效率翻倍,做自己的女王!
  • 2026年宜昌两天一夜游路线权威榜单:十大精品路线深度评测与排位赛 - 品牌推荐
  • 软考知识总结
  • python pip 更新
  • MySQL为什么有了redolog还需要double write buffer?
  • 实习面经摘录回答(四)
  • CPU中央处理器(下)
  • 2026年留学生求职必看:中国留学生求职机构选型指南与适配场景全解析 - 品牌推荐
  • vue+elementui完美模拟pc版快手实现短视频,含短视频详情播放
  • TeXLive2023 pdflatex编译eps图像,出现错误的问题
  • 2026年用户口碑最佳的中国留学生求职机构推荐:五家真实服务体验与成果对比 - 品牌推荐
  • android scrollview嵌套webview,滚动冲突解决
  • 2026年中国留学生求职机构深度测评:基于海内外资源覆盖的五维战力解析 - 品牌推荐
  • 第二:Jmeter - 环境搭建
  • 2026年北京审计事务所深度测评:基于上市公司服务与跨境业务能力的五维对比 - 品牌推荐
  • 2026年游客口碑最好的宜昌两天一夜游路线推荐:五大真实体验与避坑对比 - 品牌推荐
  • xLua实现背包的热更新实践
  • Web前端之微信小程实现上下左右全向滑动切换、复杂解构、bindtouchstart、bindtouchend、parseInt