全局定义:
var i = 0;
var serviceIds = [];
var finalServiceId = "";
var characteristicId = [];
var checkIndex = 0;
1.wx.createBLEConnection成功后执行以下方法;
getServiceId() {
var that = this
wx.getBLEDeviceServices({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: deviceId,
success: function(res) {
console.log('获取蓝牙设备所有 service(服务)', res.services);
i = 0;
while (res.services[i]) {
serviceIds[i] = res.services[i].uuid;
console.log(serviceIds[i]);
i++;
}
},
fail: function(res) {
console.log(res, '失败')
wx.showToast({
title: '失败',
icon: 'fails',
duration: 1000
})
}
})
},
2.触发getVersion获取版本号 ;
备注:通过上方方法获取到蓝牙设备所有的service服务,将service下所有的uuid都push到serviceIds,遍历serviceIds,通过wx.getBLEDeviceCharacteristics方法拿到蓝牙设备某个服务中所有特征值,比如2A28是查询固件的版本号,拿到characteristics内uuid为00002A28-***-***-***-***的值与characteristics并列返回的serviceId进行下一操作;
(*需注意:wx.getBLEDeviceCharacteristics在安卓内不会返回serviceId)
ios操作:
getVersion() {
let that = this;
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
let deviceId = deviceId;
for (var i = 0; i < serviceIds.length; i++) {
wx.getBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: serviceIds[i],
success: function(res) {
console.log(res, '查看数据')
for (var i = 0; i < res.characteristics.length; i++) {
if (res.characteristics[i].uuid === '00002A28-***-***-***-***') {
console.log(res.characteristics, '对比后拿到当前选中的一栏数据')
characteristicId[i] = res.characteristics[i].uuid;
finalServiceId = res.serviceId;
that.startnotify();
that.startread();
}
}
}
})
}
},
安卓操作:
getVersion() {
checkIndex = 0;
this.getServiceId(checkIndex);
},
getServiceId(num) {
let that = this;
console.log(num, '查看num')
//这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
let deviceId = deviceId
var IsCheck = true;
wx.getBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: serviceIds[num],
success: function(res) {
console.log(res, '查看res')
if (res.characteristics) {
for (var i = 0; i < res.characteristics.length; i++) {
if(res.characteristics[i].uuid == '00002A28-***-***-***-***'){
IsCheck = false;
characteristicId[i] = res.characteristics[i].uuid;
finalServiceId = serviceIds[num];
that.startnotify();
that.startread();
return;
}
}
if (IsCheck) {
that.getServiceId(checkIndex++)
}
}
}
})
},
3.notify ,indicate操作 ,开启监听订阅特征消息:
startnotify() {
let that = this;
//这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
let deviceId = deviceId;
//该特征是否支持 notify ,indicate操作 ,开启监听订阅特征消息
wx.notifyBLECharacteristicValueChange({
state: true,
deviceId: deviceId,
serviceId: finalServiceId,
characteristicId: characteristicId[0],
success: function(res) {
console.log('notifyBLECharacteristicValueChange success', res.errMsg)
}
})
//监听低功耗蓝牙设备的特征值变化事件。必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification。
//拿到版本号
wx.onBLECharacteristicValueChange(function(res) {
console.log(res, '查看res')
let bufToHexs = bufToHex(res.value)
console.log(bufToHexs, 'bufToHexs')
let hexCharCodeToStrs = hexCharCodeToStr(bufToHexs) //‘hexCharCodeToStrs’拿到版本号
app.globalData.valueNum = hexCharCodeToStrs;
console.log(hexCharCodeToStrs, 'hexCharCodeToStrs')
})
},
4.执行read操作:
startread() {
//这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
let deviceId = deviceId;
// 该特征是否支持 read 操作
wx.readBLECharacteristicValue({
deviceId: deviceId,
serviceId: finalServiceId,
characteristicId: characteristicId[0],
success: function(res) {
console.log('readBLECharacteristicValue:', res.errCode)
wx.readBLECharacteristicValue({
deviceId: deviceId,
serviceId: finalServiceId,
characteristicId: characteristicId[0],
success: function(res) {
console.log('获取到的值:', res);
},
fail: function(res) { //如果手机上的蓝牙没有打开,可以提醒用户
console.log(res, '获取到失败')
}
});
}
})
},