wx.getBLEDeviceCharacteristics安卓手机一切正常, IOS write为false ,而且每次监听到的uuid都是一样的. wx.notifyBLECharacteristicValueChange监听不到特征值,直接返回到fail方法里面
IOS手机::::::::::::::::::::(有问题)
安卓手机:::::::::::::(正常 每次的uuid监听到的都不一样)
*/
findBlue() {
var that = this
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
interval: 0,
success: function (res) {
// wx.showLoading({
// title: '正在搜索设备',
// })
that.getBlue()
}
})
},
// /**
// * 获取搜索到的设备信息
// */
getBlue() {
var that = this
wx.getBluetoothDevices({
success: function (res) {
for (var i = 0; i < res.devices.length; i++) {
//that.data.inputValue:表示的是需要连接的蓝牙设备ID,简单点来说就是我想要连接这个蓝牙设备,所以我去遍历我搜索到的蓝牙设备中是否有这个ID
if (res.devices[i].name == that.data.name || res.devices[i].localName == that.data.inputValue) {
// if (res.devices[i].name == that.data.name) {
that.setData({
deviceId: res.devices[i].deviceId,
consoleLog: "设备:" + res.devices[i].deviceId,
})
let deviceId = res.devices[i].deviceId
that.connetBlue(deviceId); //4.0
console.log("搜索周围设备")
return;
}
}
},
})
},
// /**
// * 获取到设备之后连接蓝牙设备
// */
connetBlue(deviceId) {
console.log("进入连接方法deviceId" + deviceId)
var that = this;
// wx.closeBLEConnection({
// deviceId: deviceId
// })
// wx.stopBluetoothDevicesDiscovery();
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: (res) => {
// console.log('startBluetoothDevicesDiscovery success', res)
// wx.onBluetoothDeviceFound((res) => {
// // console.log(res)
// // console.log("扫描蓝牙1")
// })
wx.createBLEConnection({
deviceId,
success: (res) => {
wx.hideLoading()
console.log("连接蓝牙成功1!")
that.setData({
deviceId,
})
},
fail: (res) => {
wx.hideLoading()
console.log('连接蓝牙失败第一次');
console.log(res)
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: (res) => {
console.log('startBluetoothDevicesDiscovery success', res)
// wx.onBluetoothDeviceFound((res) => {
// console.log(res)
// console.log("扫描蓝牙2")
// })
wx.createBLEConnection({
deviceId,
success: (res) => {
console.log("连接蓝牙成功2!")
that.setData({
deviceId,
})
},
fail: (res) => {
console.log('连接蓝牙失败第二次');
console.log(res)
}
})
},
})
}
})
},
})
},
getBLEDeviceServices(deviceId) {
console.log(1)
wx.getBLEDeviceServices({
deviceId,
success: (res) => {
console.log(2)
console.log(res)
console.log(3)
for (let i = 0; i < res.services.length; i++) {
if (res.services[i].isPrimary) {
console.log(4)
console.log(deviceId)
console.log(res.services[i].uuid)
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
return
}
}
},
fail:function(res){
console.log("失败咯")
console.log(res)
}
})
},
getBLEDeviceCharacteristics(deviceId, serviceId) {
var that = this;
console.log(5)
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: (res) => {
console.log(6)
console.log('getBLEDeviceCharacteristics success', res.characteristics)
for (let i = 0; i < res.characteristics.length; i++) {
console.log(7)
let item = res.characteristics[i]
console.log(item)
if (item.properties.read) {
wx.readBLECharacteristicValue({
deviceId,
serviceId,
characteristicId: item.uuid,
})
if (item.properties.write) {
that.setData({
canWrite: true
})
}
}
if (that.data.state == false) {
//关
that.writeBLECharacteristicValue22()
} else {
// //开
// if (that.data.isShow1 == true || that.data.isShow2 == true) {
// that.writeBLECharacteristicValue11()
// } else if(that.data.isShow1 == false && that.data.isShow2 == false){
// that.writeBLECharacteristicValue00()
// }
that.writeBLECharacteristicValue11()
}
// 操作之前先监听,保证第一时间获取数据
if (item.properties.notify || item.properties.indicate) {
wx.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: item.uuid,
state: true,
success(res) {
wx.onBLECharacteristicValueChange(function (resb) {
console.log('监听到特征值更新', resb.characteristicId);
console.log('监听到特征值更新1', resb.value);
let characteristicValue = ab2hex(resb.value);
console.log('转换16进制后:', characteristicValue);
//this._characteristicId = resb.characteristicId
that.setData({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: resb.characteristicId
});
// wx.showModal({
// title: '监听到特征值更新',
// content: `更新后的特征值(16进制格式):${characteristicValue}`,
// showCancel: false
// })
})
that._deviceId = deviceId
that._serviceId = serviceId
that._characteristicId = item.uuid
console.log( that._deviceId )
console.log(that._serviceId )
console.log(that._characteristicId )
that.setData({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: item.uuid
});
},
fail: (res) => {
console.log(res)
console.warn("监听特征值失败");
}
})
}
}
},
fail(res) {
console.error('getBLEDeviceCharacteristics', res)
}
})
},
//30分钟和60分钟定时关闭
writeBLECharacteristicValue11() {
console.log("发送数据11")
// 向蓝牙设备发送一个0x00的16进制数据
//开
你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)