微信小程序实现蓝牙BLE1.步骤:1、wx.openBluetoothAdapter//蓝牙初始化2、wx.onBluetoothDeviceFound//监听寻找到新设备的事件3、wx.startBluetoothDevicesDiscovery//开始搜寻附近的蓝牙外围设备4、wx.getBluetoothDevices//获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。5、wx.stopBluetoothDevicesDiscovery//停止搜寻附近的蓝牙外围设备。搜索到需要设备时或者连接时候停止搜索6、wx.createBLEConnection//连接设备7、wx.getBLEDeviceServices//获取所有服务列表8、wx.getBLEDeviceCharacteristics//获取蓝牙设备某个服务中的所有特征值9、wx.onBLEConnectionStateChange//监听设备连接状态(本人用小米2手环测试经常断)10、wx.notifyBLECharacteristicValueChange//启用低功耗蓝牙设备特征值变化是的notify功能11、wx.onBLECharacteristicValueChange//开始监听特征值的变化12、wx.writeBLECharacteristicValue//写入特征值13、wx.readBLECharacteristicValue//读取特征值
2.以下是demo代码:
onLoad:function(options){varthatthis//蓝牙初始化wx.openBluetoothAdapter({success:function(res){console.log(res)//监听寻找到新设备的事件wx.onBluetoothDeviceFound(function(res){//console.log(res)})},fail:function(res){wx.showModal({title:'提示',content:'请检查手机蓝牙是否打开',})}})},3.点击搜索设备按钮(lanya):
lanya:function(){wx.showLoading({title:'搜索中',})setTimeout(function(){wx.hideLoading()},1500)//开始搜寻附近的蓝牙外围设备wx.startBluetoothDevicesDiscovery({success:function(res){console.log('搜索完成')//获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。wx.getBluetoothDevices({success:function(res){if(res.devices.length0){wx.showModal({title:'提示',content:'没有搜索到设备,请重试',})wx.navigateTo({url:'../index/index',})}console.log(res)//console.log(res.devices[0].deviceId)//console.log(res.devices[0].name)console.log(res.devices)vardatares.deviceswx.navigateTo({url:'../lanya/lanya?data'+JSON.stringify(data),})//停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。//wx.stopBluetoothDevicesDiscovery({//success:function(res){//console.log('已经停止搜索')//},//})},fail:function(res){console.log('没有找到设备')}})},fail:function(fa){console.log(fa)}})},onLoad:function(options){varId[]varName[]vardataJSON.parse(options.data)console.log(data)varthatthisconsole.log(data)that.setData({array:data})},4.点击连接设备按钮:
sub:function(e){vardeviceIde.currentTarget.idapp.globalData.deviceIddeviceIdconsole.log(e.currentTarget.id)//连接设备wx.createBLEConnection({deviceId:deviceId,success:function(res){console.log(res)//获取服务列表wx.navigateTo({url:'../res/res?id'+deviceId,})console.log('id:'+deviceId)},fail:function(res){wx.showModal({title:'提示',content:'连接超时,请重试',})//console.log('id:'+deviceId)console.log(res)}})},onLoad:function(options){vardeviceIdoptions.idvarthatthis//获取所有服务wx.getBLEDeviceServices({deviceId:deviceId,success:function(res){//console.log(res)console.log(res.services)that.setData({array:res.services})},})},5.进入某个服务
sub:function(e){varthatthisvarserviceIde.target.id//获取蓝牙设备某个服务中的所有特征值wx.getBLEDeviceCharacteristics({deviceId:app.globalData.deviceId,serviceId:serviceId,success:function(res){console.log(res)that.data.characteristicsres.characteristics//获取characteristicwx.navigateTo({url:'../notify/notify?ct'+JSON.stringify(that.data.characteristics)+'serviceId'+serviceId,})},fail:function(res){console.log(res)}})//监听设备连接状态wx.onBLEConnectionStateChange(function(res){console.log(res)if(res.connectedfalse){wx.showModal({title:'提示',content:'设备连接已断开',})}})},onLoad:function(options){varctJSON.parse(options.ct)serviceIdoptions.serviceIdvarthatthisthat.setData({array:ct,serviceId:serviceId,})},6.进入某个特征值
sub:function(e){varthatthisvarchte.target.idvardeviceIdapp.globalData.deviceId//启用低功耗蓝牙设备特征值变化是的notify功能wx.notifyBLECharacteristicValueChange({deviceId:deviceId,serviceId:serviceId,characteristicId:cht,state:true,success:function(res){//开始监听wx.onBLECharacteristicValueChange(function(characteristics){console.log(characteristics.value)varacharacteristics.valuevarint8arraynewInt8Array(a);console.log("监听到特征值更新:"+int8array[0])//varhexab2str(a);//console.log("返回的值:".hex)})console.log(res)wx.navigateTo({url:'../write/write?cht'+cht+'serviceId'+serviceId+'deviceId'+deviceId,})},fail:function(res){console.log(res)},})},**```onLoad:function(options){varthatthisvarchtoptions.chtvardeviceIdoptions.deviceIdvarserviceIdoptions.serviceIdthat.setData({deviceId:deviceId,cht:cht,serviceId:serviceId})},7.向该特征值写入数据:**
formSubmit:function(e){varthatthisvarvaluee.detail.value.pswconsole.log(value)//写入数据wx.writeBLECharacteristicValue({deviceId:that.data.deviceId,serviceId:that.data.serviceId,characteristicId:that.data.cht,value:that.str2ab(value),success:function(res){console.log(res)},fail:function(res){console.log(res)}});wx.readBLECharacteristicValue({deviceId:that.data.deviceId,serviceId:that.data.serviceId,characteristicId:that.data.cht,success:function(e){console.log(e)},})},8.重点写入的字符串要转换成Arraybuffer
//字符串转为ArrayBuffer对象,参数为字符串str2ab:function(str){varbufnewArrayBuffer(str.length*2);//每个字符占用2个字节varbufViewnewUint16Array(buf);for(vari0,strLenstr.length;istrLen;i++){bufView[i]str.charCodeAt(i);}returnbuf;},9.**监听的特征值变化回调是ArrayBuffer也要转换为字符串,可是我直接写下标就能获取,写转换获得就是undefined
functionab2str(arrayBuffer){returnString.fromCharCode.apply(null,newUint8Array(arrayBuffer));//letunit8ArrnewUint8Array(arrayBuffer);//letencodedStringString.fromCharCode.apply(null,unit8Arr),//decodedStringdecodeURIComponent(escape((encodedString)));//没有这一步中文会乱码//returndecodedString;}
微信小程序实现蓝牙BLEdemo版-微信小程序ble-微信小程序ibeacon
浏览量:2021
时间:
来源:吴小友
版权声明
即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。
最新资讯
-

即速应用,赋能企业玩转微信小程序智慧经营
作为国内领军的智慧商业经营服务商,即速应用始终秉承“让每个企业都拥有自己的智慧店铺”的愿景,持续赋能更多企业玩转智慧经营。即速应用旗下拥有“小程序搭建工具-即速应用”、“私域流量专家-即客云”等产品,帮助商家打通互联网全生态营销闭环。 -

即客云2.0重磅更新,让微信小程序运营更简单!
即客云作为一款基于企业微信的第三方工具,现从多维度提供超过30种功能,自上线以来,已服务多家企业,受到一致好评。近期,我们根据客户反馈和市场调研正式推出升级版 即客云2.0!更新了私域运营SOP,群日历功能,批量拓客,客户雷达,消息推送,个人欢迎语,帮助企业更好运用企业微信;同时提升了社群运营工作标准化,提升运营效率,帮助企业实现客户增长,玩转私域流量。 -

零代码 + AI 双轮驱动|即速应用解锁人工智能小程序开发新范式
无需代码、无需 AI 算法功底,普通人也能快速搭建智能小程序。即速应用将人工智能与零代码开发深度融合,推出 AI 智能生成能力,用户通过自然语言描述需求,AI 自动生成小程序页面、功能模块与后台配置,覆盖商城、预约、同城、社区团购等全场景。平台内置 AI 智能推荐、智能客服、用户画像分析等能力,一键对接微信生态,打通视频号、企业微信、短信跳转,帮企业快速落地 AI 应用,抢占智慧经营先机,让每家企业都拥有 AI 驱动的智慧店铺。










