上一次的文章记录了通过蓝牙BLE连接设备并读取设备信息的界面代码,今天来记录一下逻辑功能代码的实现。
想要通过微信小程序连接某个蓝牙BLE设备需要调用wx.createBLEConnection API,需要传入蓝牙BLE设备的deviceId;
连接成功后,需要通过wx.getBLEDeviceServices和wx.getBLEDeviceCharacteristics 两个API来获取蓝牙BLE设备的ServeiceID集合和Characteristic集合;
还要通过wx.setBLEMTU来设置蓝牙通信指令的最大字节数;
最后调用wx.writeBLECharacteristicValue和 wx.onBLECharacteristicValueChangeAPI来进行数据的发送和获取(写/读)。这里只是简单的把逻辑思路进行了概括,实际开发中需要考虑一些细节的问题。好了,话不多说,直接上代码。
首先是Page的data对象,他存储了一些必要的数据,例如设备ID、读取参数的指令以及界面显示参数需要绑定的变量等。需要注意的是,这里发送的指令是特殊定义的,不具备普遍适用性,要稍加注意。
data: {
bluetoothStatus:{
deviceId: null,
deviceName: null,
connected: "未连接",
buttonText: "连接中……",
deviceServices: null,
deviceCharacteristics: null,
isConnected: false,
sendDataMax: 153
},
deviceInfo: {
time: '0',
n_ua: '0',
n_ia: '0',
i_pa: '0',
i_qa: '0',
i_sa: '0',
p_fa: '0',
ft: '0',
ep_com1: '0',
ep_com2: '0',
imp_ep: '0',
exp_ep: '0',
sn: '0',
model: '0',
hardver: '0',
firmware: '0',
brand: '0'
},
cmds: {
readMeter: {
svrui_cmd: "en",
sv_rd_meter: "en",
jobid: "1"
},
readSN: {
svrui_cmd: "en",
sv_rd_sn: "en",
jobid: "1"
},
writeMeter : {
svrui_cmd: "en",
sv_wr_cal: "en",
jobid: "1"
}
},
onfirst: true
},
页面加载事件中调用两个私有函数:一个进行蓝牙BLE连接,一个获取蓝牙BLE的连接状态。
onLoad(options) {
this.setData({
'bluetoothStatus.deviceId': options.deviceId,
"bluetoothStatus.deviceName": options.name,
"bluetoothStatus.connected": "连接中……"
}),
this.connectionBlueTooth(options);
this.getBLEConnectionStateChange();
},
下面是两个函数的具体实现
connectionBlueTooth(obj){
let that = this;
wx.createBLEConnection({
deviceId: obj.deviceId,
success(res) {
that.setData({
'bluetoothStatus.deviceId': obj.deviceId,
'bluetoothStatus.deviceNam': obj.name,
'bluetoothStatus.connected': '已连接',
'bluetoothStatus.buttonText': '断开连接',
'bluetoothStatus.isConnected': true
});
that.getBlueToothServers(obj.deviceId);
that.func_SetBLEMTU(obj.deviceId);
},
fail(msg) {
if(msg.errCode == -1) {
that.setData({
'bluetoothStatus.deviceId': obj.deviceId,
'bluetoothStatus.deviceNam': obj.name,
'bluetoothStatus.connected': '已连接',
'bluetoothStatus.buttonText': '断开连接',
'bluetoothStatus.isConnected': true
});
that.getBlueToothServers(obj.deviceId);
that.func_SetBLEMTU(obj.deviceId);
}
else {
that.setData({
'bluetoothStatus.connected': '连接失败',
'bluetoothStatus.buttonText': '重新连接',
'bluetoothStatus.isConnected': false
})
}
}
})
},
getBLEConnectionStateChange() {
wx.onBLEConnectionStateChange((result) => {
if(result.connected) {
}
else {
this.setData({
'bluetoothStatus.connected': '连接断开',
'bluetoothStatus.buttonText': '重新连接',
'bluetoothStatus.isConnected': false
});
}
});
},
在connectionBlueTooth函数中调用了wx.createBLEConnection 和其他两个私有函数:
getBlueToothServers(serviceId) {
let that = this;
wx.getBLEDeviceServices({
deviceId: serviceId,
success: (res)=>{
that.setData({
'bluetoothStatus.deviceServices': res.services
});
that.getBlueToothCharacteristics(serviceId);
},
fail: ()=>{},
complete: ()=>{}
});
},
func_SetBLEMTU(deviceId) {
let that = this;
wx.onBLEMTUChange((res) => {
that.setData({
'bluetoothStatus.sendDataMax': res.mtu - 3
});
})
let Counter = 0;
wx.setBLEMTU({
deviceId: deviceId,
mtu: 253,
success(res) {
wx.getBLEMTU({
deviceId: deviceId,
success:(res) => {
}
})
},
fail(err) {
}
})
},
由于本页功能的代码量较多,为了自己能够更好的消化,所以将分多次进行记录分享,这次就记录到这里。
谢谢您的阅读,祝您天天开心,完事如意。