小程序wxwriteBLECharacteristicValue写入数据无通知

微信小程序,微信公开课,小程序wxwriteBLECharacteristicValue写入数据无通知 微信公开课

如何看待微信公开课小程序热门讨论「小程序wxwriteBLECharacteristicValue写入数据无通知」
摘要:小程序wxwriteBLECharacteristicValue写入数据无通知
展开:昨天有接收到这通知的时候,但加了一些代码后,再也无法在小程序中收到onBLECharacteristicValueChange通知,但可以肯定说蓝牙模块已经接收到数据的。//index.js//获取应用实例varappgetApp()Page({data:{isOpenLock:false,pwdHidden:true,//isConnected:true,//测试isConnected:false,powerAmount:0,//剩余电量百分比opResult:,openPwd:,newPwd:,againPwd:,deveiceID:,deveiceName:,services:[],CDffe5:0000ffe500001000800000805f9b34fb,//写数据通道服务UUIDCD180f:0000180f00001000800000805f9b34fb,//电池电量报告通道服务UUIDCDffe0:0000ffe000001000800000805f9b34fb,//通知通道服务UUIDcharacteristicsffe5:null,//写数据通道服务特征UUIDcharacteristics180f:null,//电池电量报告通道服务UUIDcharacteristicsffe0:null,//通知通道服务特征UUIDtmpIndex:0,},btnModalBack:function(event){//回退前delta(默认为1)页面wx.navigateBack({delta:1,});},btnModalCancel:function(event){this.setData({openPwd:,newPwd:,againPwd:,opResult:,pwdHidden:true});},btnOpenLock:function(event){this.setData({opResult:,pwdHidden:false,isOpenLock:true});},btnModifyOpenPWD:function(event){this.setData({opResult:,pwdHidden:false,isOpenLock:false});},btnModalOpenConfirm:function(event){varthatthis;that.setData({opResult:});if(nullthat.data.openPwd||that.data.openPwd.length!6){that.setData({opResult:密码不能为空或者长度不为6位});return;}vartmpArraythat.buildFrame(aa0006,that.data.openPwd);console.log(开锁数据:,that.frameToString(tmpArray));wx.writeBLECharacteristicValue({deviceId:that.data.deveiceID,serviceId:that.data.CDffe5,characteristicId:that.data.characteristicsffe5.uuid,value:tmpArray,success:function(res){console.log(`发送数据成功${res.errMsg}`);that.setData({opResult:数据发送成功});}});},btnModalModifyConfirm:function(event){varthatthis;that.setData({opResult:});if(nullthat.data.openPwd||that.data.openPwd.length!6){that.setData({opResult:旧密码不能为空或者长度不为6位});return;}if(nullthat.data.newPwd||that.data.newPwd.length!6){that.setData({opResult:新密码不能为空或者长度不为6位});return;}if(nullthat.data.againPwd||that.data.againPwd.length!6){that.setData({opResult:新密码不能为空或者长度不为6位});return;}if(that.data.newPwd!that.data.againPwd){that.setData({opResult:输入的两次密码不一致});return;}vartmpArraythat.buildFrame(aa030c,that.data.openPwd+that.data.newPwd);console.log(发送数据:,that.frameToString(tmpArray));wx.writeBLECharacteristicValue({deviceId:that.data.deveiceID,serviceId:that.data.CDffe5,characteristicId:that.data.characteristicsffe5.uuid,value:tmpArray,success:function(res){console.log(`发送数据成功${res.errMsg}`);that.setData({opResult:修改数据发送成功});}});},inputPwdUnFocus:function(event){this.setData({openPwd:event.detail.value});},inputNewPwdUnFocus:function(event){this.setData({newPwd:event.detail.value});},inputAgainPwdUnFocus:function(event){this.setData({againPwd:event.detail.value});},getCharacteristics:function(tmpServiceId){varthatthis;wx.getBLEDeviceCharacteristics({deviceId:that.data.deveiceID,serviceId:tmpServiceId,success:function(res){if(res.characteristics[0].properties.notifytrue){wx.notifyBLECharacteristicValueChanged({deviceId:that.data.deveiceID,serviceId:tmpServiceId,characteristicId:res.characteristics[0].uuid,state:true,success:function(res){console.log(uuidblecvcsuccess,tmpServiceId);},fail:function(res){console.log(uuidblecvcfail,tmpServiceId);},});}if(res.characteristics[0].uuid.indexOf(ffe9)!1){that.setData({characteristicsffe5:res.characteristics[0]});}if(res.characteristics[0].uuid.indexOf(2a19)!1){that.setData({characteristics180f:res.characteristics[0]});wx.readBLECharacteristicValue({deviceId:that.data.deveiceID,serviceId:that.data.CD180f,characteristicId:that.data.characteristics180f.uuid,});setInterval(function(){//定时执行获取电量的方法2分钟读取一次wx.readBLECharacteristicValue({deviceId:that.data.deveiceID,serviceId:that.data.CD180f,characteristicId:that.data.characteristics180f.uuid,});},120000);}if(res.characteristics[0].uuid.indexOf(ffe4)!1){that.setData({characteristicsffe0:res.characteristics[0]});}},fail:function(res){console.log(res);}});},buildOrderFrame:function(frameStr){vartypedArraynewUint8Array(frameStr.match(/[daf]{2}/gi).map(function(h){returnparseInt(h,16);}));returntypedArray.buffer;},buildFrame:function(orderFix,orderContent){vararraynewArray();for(vari0;iorderContent.length;i++){array[i]orderContent.charCodeAt(i);}returnthis.buildOrderFrame(this.cs(orderFix+this.frameToString(array)));},cs:function(orderStr){vartmpInt0;for(vari0;iorderStr.length/2;i++){tmpInttmpInt+parseInt(orderStr.substr(i2,2),16);}returnorderStr+this.frameToString([(tmpInt0xFF)])+55;},frameToString:function(frame){returnArray.prototype.map.call(newUint8Array(frame),x(00+x.toString(16)).slice(2)).join()},onLoad:function(query){varthatthis;if(nullquery.deveiceID||nullquery.deveiceName){return;}//生命周期函数监听页面加载that.setData({deveiceID:query.deveiceID,deveiceName:query.deveiceName,});wx.onBLEConnectionStateChanged(function(res){//callbackconsole.log(`device${res.deviceId}statehaschanged,connected:${res.connected}`)that.setData({isConnected:res.connected});});wx.onBLECharacteristicValueChange(function(res){//callbackconsole.log(`characteristic${res.characteristicId}haschanged,nowis${res.value}`);varhexthat.frameToString(res.value);console.log(receive,hex);if(res.characteristicIdthat.data.characteristics180f.uuid){that.setData({powerAmount:parseInt(hex,16)});}elseif(res.characteristicIdthat.data.characteristicsffe0.uuid){varresulthex.substr(2,2);that.setData({opResult:result});console.log(result,result);}});wx.createBLEConnection({deviceId:that.data.deveiceID,success:function(res){//successconsole.log(连接蓝牙成功:+res.errMsg);that.setData({isConnected:true});wx.getBLEDeviceServices({deviceId:that.data.deveiceID,success:function(res){//successconsole.log(deviceservices:,res.services)that.setData({services:res.services});for(vari0;ires.services.length;i++){if(res.services[i].uuidthat.data.CDffe5)that.getCharacteristics(that.data.CDffe5);elseif(res.services[i].uuidthat.data.CD180f)that.getCharacteristics(that.data.CD180f);elseif(res.services[i].uuidthat.data.CDffe0)that.getCharacteristics(that.data.CDffe0);}},fail:function(res){//failconsole.log(res);},});},fail:function(res){//failconsole.log(连接蓝牙失败:+res.errMsg);that.setData({isConnected:false});},});},onUnload:function(){this.setData({isConnected:false});if(null!this.data.deveiceID)wx.closeBLEConnection();},})了解更多

「小程序wxwriteBLECharacteristicValue写入数据无通知」热门讨论订阅

微信小程序,微信公开课,小程序wxwriteBLECharacteristicValue写入数据无通知
订阅小程序

方法1:微信扫描微信公开课小程序码即可订阅热门讨论「小程序wxwriteBLECharacteristicValue写入数据无通知」

方法2:微信搜索微信公开课小程序名称进入,即可订阅热门讨论「小程序wxwriteBLECharacteristicValue写入数据无通知」

方法3:微信网页访问即速商店,长按识别微信公开课小程序码即可订阅热门讨论「小程序wxwriteBLECharacteristicValue写入数据无通知」

微信公开课小程序热门讨论「小程序wxwriteBLECharacteristicValue写入数据无通知」由微信公开课原创摘录于微信小程序商店shop.jisuapp.cn,转载请注明出处。

微信公开课热门讨论「小程序wxwriteBLECharacteristicValue写入数据无通知」由微信公开课开发者向微信用户提供,并对本服务内容、数据资料及其运营行为等真实性、合法性及有效性承担全部责任。