正常流程对蓝牙进行连接并且使用wx.setBLEMTU对mtu设置为了247,但onBLECharacteristicValueChange没有任何数据返回
initBlue() {
wx.showLoading({
title: '连接蓝牙',
})
let thet = this
// 先关闭蓝牙适配器
wx.closeBluetoothAdapter({
success: (res) => {
},
})
// 打开适配器
wx.openBluetoothAdapter({
mode: 'central', // ios必须要带这个参数
success: (res) => {
console.log(res);
// adapter建立成功,可以通过观察者模式告诉页面
// 开启适配器状态的监听
wx.onBluetoothAdapterStateChange((result) => {
console.log(result);
if (!result.available) { // 适配器不可用,可能用户关闭了手机的蓝牙
wx.showModal({
title: '提示',
content: '适配器不可用',
showCancel: false,
success(res) {
if (res.confirm) {
wx.navigateBack()
}
}
})
return
}
})
console.log('连接蓝牙');
// 连接蓝牙
thet.joinBLE()
},
fail(err) { // 蓝牙适配器未开启
if (err.errCode === BLUETOOTH_NOT_OPEN) {
// 手机蓝牙未开启
wx.showModal({
title: '提示',
content: '手机蓝牙未开启',
showCancel: false,
success(res) {
if (res.confirm) {
wx.navigateBack()
}
}
})
} else {
// 可能未授权小程序蓝牙权限
wx.showModal({
title: '提示',
content: '请授权手机蓝牙',
showCancel: false,
success(res) {
if (res.confirm) {
wx.navigateBack()
}
}
})
}
}
})
},
joinBLE() {
wx.createBLEConnection({
deviceId: this.data.mac,
success: (res) => {
// 开启连接状态的监听
console.log(res);
wx.showToast({
title: '蓝牙连接成功',
icon: 'none',
duration: 2000
})
// 获取可用的服务
wx.getBLEDeviceServices({
deviceId: this.data.mac,
success: (res) => {
// 全部的服务UUID
let services = res.services
console.log(services, '全部的服务UUID');
this.getBLEDeviceCharacteristics(services[1].uuid)
},
fail(res) {
console.log(res);
}
})
},
fail(res) {
// 连接失败, 关闭适配器
wx.showModal({
title: '提示',
content: '蓝牙连接失败',
showCancel: false,
success(res) {
if (res.confirm) {
wx.navigateBack()
}
}
})
}
})
},
getBLEDeviceCharacteristics(serviceId) {
wx.getBLEDeviceCharacteristics({
deviceId: this.data.mac,
serviceId,
success: (res) => {
// 当前服务UUID下的特征值列表
let characteristics = res.characteristics;
console.log(characteristics, '当前服务UUID下的特征值列表');
this.data.BLEData.characteristicId = characteristics[0].uuid
this.data.BLEData.deviceId = this.data.mac
this.data.BLEData.serviceId = serviceId
this.setData({
BLEData: this.data.BLEData
})
// 开启监听数据接收的特征值
wx.notifyBLECharacteristicValueChange({
characteristicId: characteristics[0].uuid,
state: true, // 启用 notify 功能
deviceId: this.data.mac,
type: "notification",
serviceId,
success: (res) => {
console.log(res, "notify创建连接,发送指令")
this.exchange_mtu()
},
fail: (res) => {
// ios有可能取消匹配,也会调用这里
},
complete: (res) => {
this.onble()
}
})
},
fail(res) {
// 断开连接,关闭适配器
}
})
},
exchange_mtu() {
wx.setBLEMTU({
deviceId: this.data.mac,
mtu: 247,
success: (res) => {
console.log(res, "mtu update success")
// this.writeBLECharacteristicValue(this.data.BLEsetData.step1)
this.writeBLECharacteristicValue('D1D1000232005AE8')
},
fail: function (res) {
console.log(res, "mtu update failed")
},
complete: function (res) {
console.log(res, "mtu update complete")
}
})
},
onble() {
// 开启数据接收的监听器
console.log('开启数据接收的监听器');
function ab2hex(buffer) {
let hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}
wx.onBLECharacteristicValueChange(function (res) {
console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
console.log(ab2hex(res.value))
})
},
writeBLECharacteristicValue(value) {
let buffer = this.hex2buffer(value)
wx.writeBLECharacteristicValue({
characteristicId: this.data.BLEData.characteristicId,
deviceId: this.data.BLEData.deviceId,
writeType: 'writeNoResponse',
serviceId: this.data.BLEData.serviceId,
value: buffer,
success: (res) => {
console.log(res);
}, // 回调函数
fail: (err) => {
console.log(err);
}, // 回调函数
complete: (result) => {
setTimeout(() => {
}, 1000)
},
})
},
hex2buffer(str) {
console.info("写入的数据====" + str);
//字符串 转 十六进制
var val = "";
for (var i = 0; i < str.length; i++) {
if (val == "")
val = str.charCodeAt(i).toString(16);
else
val += "," + str.charCodeAt(i).toString(16);
}
//十六进制 转 ArrayBuffer
var buffer = new Uint8Array(val.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16)
})).buffer;
return buffer;
},
安卓还是ios呢