收藏
回答

wx.onBLECharacteristicValueCha 无回调,回调参数为undefined?

我贴出代码,这个问题卡了快一周了很多办法都尝试了,还是不能解决。

问题前景:与蓝牙设备连接进行指令交互

/**

* 连接低功耗蓝牙设备。

*/

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)

}

})


},

最后一次编辑于  2020-03-09
回答关注问题邀请回答
收藏

2 个回答

  • 人偶?!
    人偶?!
    2020-03-11

    已解决,是指令格式的转换问题

    2020-03-11
    有用
    回复
  • 是小白啊
    是小白啊
    2020-03-09

    安卓还是ios呢?麻烦在手机微信那里上传下日志: 我->设置->帮助与反馈右上角有个上报日志的入口,麻烦提供一下微信号,时间点

    2020-03-09
    有用
    回复 4
    • 人偶?!
      人偶?!
      2020-03-09
      安卓的,日志已上传
      微信号:gyf19970422
      时间点:11:25
      2020-03-09
      回复
    • 人偶?!
      人偶?!
      2020-03-09
      你好,我问下,小程序给设备发送指令,我要怎么去判断设备收到了这个指令
      2020-03-09
      回复
    • 是小白啊
      是小白啊
      2020-03-10回复人偶?!
      你写入的characteristics根本就不是他注册了notify的那4个characteristics,真正写入数据的characteristics没有注册监听,所以没有回调;这里你代码上虽然写死了注册4个监听,但是本身调用write的时候没有判断characteristics到底是不是这4个其中之一
      2020-03-10
      回复
    • 人偶?!
      人偶?!
      2020-03-10
      是因为我测试的这个设备在 res.services[i].uuid.indexOf("BA11F08C") != -1  这个服务下只有5个特征值,4个cd01-04为notify,1个cd20为write。我从cd20 写入指令,下位机会从cd01返还指令信息,从cd04自动传输数据。所以我只监听了cd01和cd04
      2020-03-10
      回复
登录 后发表内容
问题标签