昨天有接收到这通知的时候,但加了一些代码后,再也无法在小程序中收到onBLECharacteristicValueChange通知,但可以肯定说蓝牙模块已经接收到数据的。
//index.js
//获取应用实例
var app = getApp()
Page({
data: {
isOpenLock: false,
pwdHidden: true,
//isConnected: true,//测试
isConnected: false,
powerAmount: 0,//剩余电量百分比
opResult: "",
openPwd: "",
newPwd: "",
againPwd: "",
deveiceID: "",
deveiceName: "",
services: [],
CDffe5: "0000ffe5-0000-1000-8000-00805f9b34fb",//写数据通道服务UUID
CD180f: "0000180f-0000-1000-8000-00805f9b34fb",//电池电量报告通道服务UUID
CDffe0: "0000ffe0-0000-1000-8000-00805f9b34fb",//通知通道服务UUID
characteristicsffe5: null,//写数据通道服务特征UUID
characteristics180f: null,//电池电量报告通道服务UUID
characteristicsffe0: null,//通知通道服务特征UUID
tmpIndex: 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) {
var that = this;
that.setData({ opResult: "" });
if (null == that.data.openPwd || that.data.openPwd.length != 6) {
that.setData({ opResult: "密码不能为空或者长度不为6位" });
return;
}
var tmpArray = that.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) {
var that = this;
that.setData({ opResult: "" });
if (null == that.data.openPwd || that.data.openPwd.length != 6) {
that.setData({ opResult: "旧密码不能为空或者长度不为6位" });
return;
}
if (null == that.data.newPwd || that.data.newPwd.length != 6) {
that.setData({ opResult: "新密码不能为空或者长度不为6位" });
return;
}
if (null == that.data.againPwd || that.data.againPwd.length != 6) {
that.setData({ opResult: "新密码不能为空或者长度不为6位" });
return;
}
if (that.data.newPwd != that.data.againPwd) {
that.setData({ opResult: "输入的两次密码不一致" });
return;
}
var tmpArray = that.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) {
var that = this;
wx.getBLEDeviceCharacteristics({
deviceId: that.data.deveiceID,
serviceId: tmpServiceId,
success: function (res) {
if (res.characteristics[0].properties.notify == true) {
wx.notifyBLECharacteristicValueChanged({
deviceId: that.data.deveiceID,
serviceId: tmpServiceId,
characteristicId: res.characteristics[0].uuid,
state: true,
success: function (res) {
console.log("uuid ble cvc success>>>>>>>", tmpServiceId);
},
fail: function (res) {
console.log("uuid ble cvc fail>>>>>>>", 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) {
var typedArray = new Uint8Array(frameStr.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16); }));
return typedArray.buffer;
},
buildFrame: function (orderFix, orderContent) {
var array = new Array();
for (var i = 0; i < orderContent.length; i++) {
array[i] = orderContent.charCodeAt(i);
}
return this.buildOrderFrame(this.cs(orderFix + this.frameToString(array)));
},
cs: function (orderStr) {
var tmpInt = 0;
for (var i = 0; i < orderStr.length / 2; i++) {
tmpInt = tmpInt + parseInt(orderStr.substr(i * 2, 2), 16);
}
return orderStr + this.frameToString([(tmpInt & 0xFF)]) + "55";
},
frameToString: function (frame) {
return Array.prototype.map.call(new Uint8Array(frame), x => ('00' + x.toString(16)).slice(-2)).join('')
},
onLoad: function (query) {
var that = this;
if (null == query.deveiceID || null == query.deveiceName) {
return;
}
// 生命周期函数--监听页面加载
that.setData({
deveiceID: query.deveiceID,
deveiceName: query.deveiceName,
});
wx.onBLEConnectionStateChanged(function (res) {
// callback
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
that.setData({ isConnected: res.connected });
});
wx.onBLECharacteristicValueChange(function (res) {
// callback
console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`);
var hex = that.frameToString(res.value);
console.log("receive>>>>>>>>>>>>>>>", hex);
if (res.characteristicId == that.data.characteristics180f.uuid) {
that.setData({ powerAmount: parseInt(hex, 16) });
}
else if (res.characteristicId == that.data.characteristicsffe0.uuid) {
var result = hex.substr(2, 2);
that.setData({ opResult: result });
console.log("result>>>>>>>>>>>>>>>", result);
}
});
wx.createBLEConnection({
deviceId: that.data.deveiceID,
success: function (res) {
// success
console.log("连接蓝牙成功:" + res.errMsg);
that.setData({ isConnected: true });
wx.getBLEDeviceServices({
deviceId: that.data.deveiceID,
success: function (res) {
// success
console.log('device services:', res.services)
that.setData({ services: res.services });
for (var i = 0; i < res.services.length; i++) {
if (res.services[i].uuid == that.data.CDffe5)
that.getCharacteristics(that.data.CDffe5);
else if (res.services[i].uuid == that.data.CD180f)
that.getCharacteristics(that.data.CD180f);
else if (res.services[i].uuid == that.data.CDffe0)
that.getCharacteristics(that.data.CDffe0);
}
},
fail: function (res) {
// fail
console.log(res);
},
});
},
fail: function (res) {
// fail
console.log("连接蓝牙失败:" + res.errMsg);
that.setData({ isConnected: false });
},
});
},
onUnload: function () {
this.setData({ isConnected: false });
if (null != this.data.deveiceID)
wx.closeBLEConnection();
},
})
亲,你这个问题解决了没有?我也遇到类似的问题,怎么都检查不出来问题