我贴出代码,这个问题卡了快一周了很多办法都尝试了,还是不能解决。
问题前景:与蓝牙设备连接进行指令交互
/**
* 连接低功耗蓝牙设备。
*/
BLEConn: function() {
const that = this;
wx.createBLEConnection({
deviceId: that.data.deviceId,
success: function(res) {
console.log('createBLEConnection success');
wx.stopBluetoothDevicesDiscovery({
success: function(res) {
console.log('stopBluetoothDevicesDiscovery success');
}
})
that.getBLEDeviceServices();
that.data.connected = true;
},
fail: function(res) {
console.log('createBLEConnection fail');
}
})
},
/**
* 获取蓝牙设备所有服务(service)
*/
getBLEDeviceServices() {
const that = this;
wx.getBLEDeviceServices({
deviceId: that.data.deviceId,
success: function(res) {
console.log('getBLEDeviceServices success');
for (let i = 0; i < res.services.length; i++) {
if (res.services[i].uuid.indexOf("BA11F08C") != -1) {
that.setData({
serviceId : res.services[i].uuid,
})
setTimeout( function() { that.getBLEDeviceCharacteristics(res.services[i].uuid);
},1500);
}
}
},
fail: function(res) {
console.log('getBLEDeviceServices fail');
}
})
},
/**
* 获取蓝牙设备某个服务中所有特征值(characteristic)
*/
getBLEDeviceCharacteristics(serviceId) {
const that = this;
wx.getBLEDeviceCharacteristics({
deviceId: that.data.deviceId,
serviceId: serviceId,
success: function(res) {
console.log('getBLEDeviceCharacteristics success');
let item = res.characteristics
for (let i = 0; i < item.length; i++) {
if (item[i].uuid.indexOf("CD01") != -1) {
that.setData({
cd01 : item[i].uuid,
})
}
if (item[i].uuid.indexOf("CD02") != -1) {
that.setData({
cd02: item[i].uuid,
})
}
if (item[i].uuid.indexOf("CD03") != -1) {
that.setData({
cd03: item[i].uuid,
})
}
if (item[i].uuid.indexOf("CD04") != -1) {
that.setData({
cd04: item[i].uuid,
})
}
if (item[i].properties.write && writeFlag) {
that.data.writeCha = item[i].uuid;
that.data.writeSer = serviceId;
}
}
wx.onBLECharacteristicValueChange(function (msg) {
if (msg.characteristicId.indexOf("CD01") != -1) {
console.log(`蓝牙设备发送的指令:${ab2hex(msg.value)}`)
console.log('----------------------')
}
if (msg.characteristicId.indexOf("CD04") != -1) {
console.log(`蓝牙设备发送的数据:${ab2hex(msg.value)}`)
console.log('----------------------')
let temp = ab2hex(msg.value).slice(6, 10);
console.log(`截取后的temp:${temp}`)
if (that.data.temperature && temp.indexOf('0e') === 0) {
console.log(`设备发送的数据转化为温度:${theTemperature(temp)}`);
}
}
})
wx.notifyBLECharacteristicValueChange({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cd01,
state: true,
success: function (res) {
console.log(`${that.data.cd01}:notifyBLECharacteristicValueChange success`);
},
fail: function (res) {
console.log('notifyBLECharacteristicValueChange fail');
}
})
wx.notifyBLECharacteristicValueChange({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cd02,
state: true,
success: function (res) {
console.log(`${that.data.cd02}:notifyBLECharacteristicValueChange success`);
},
fail: function (res) {
console.log('notifyBLECharacteristicValueChange fail');
}
})
wx.notifyBLECharacteristicValueChange({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cd03,
state: true,
success: function (res) {
console.log(`${that.data.cd03}:notifyBLECharacteristicValueChange success`);
},
fail: function (res) {
console.log('notifyBLECharacteristicValueChange fail');
}
})
wx.notifyBLECharacteristicValueChange({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cd04,
state: true,
success: function (res) {
console.log(`${that.data.cd04}:notifyBLECharacteristicValueChange success`);
},
fail: function (res) {
console.log('notifyBLECharacteristicValueChange fail');
}
})
},
fail: function(res) {
console.log('getBLEDeviceCharacteristics fail ');
}
})
},
/**
* 写操作
*/
sendMsg:function () {
const that = this
let buffer = hexStringToArrayBuffer(that.data.sendStr);
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceId,
serviceId: that.data.writeSer,
characteristicId: that.data.writeCha,
value: buffer,
success: function (res) {
console.log('writeBLEValue success', res.errMsg)
console.log(`写入的数据:${ab2hex(buffer)}`)
},
fail: function (res) {
console.log('writeBLEValue fail', res.errMsg)
}
})
},