在kaiqi()和guanguan()方法中的onBLECharacteristicValueChange 不执行,,,,,
还有我注释的那一部分的作用是什么作用
Page({
data: {
devices: [],
connected: false,
chs: [],
deviceId:’’,//设备id
serviceId:’’,//f0
f1:’’,
f4:’’,
yhnumber:’’,//用户
},
openBluetoothAdapter() {
wx.openBluetoothAdapter({
success: (res) => {
console.log(‘openBluetoothAdapter success’, res)
this.startBluetoothDevicesDiscovery()
},
fail: (res) => {
if (res.errCode === 10001) {
wx.onBluetoothAdapterStateChange(function (res) {
console.log(‘onBluetoothAdapterStateChange’, res)
if (res.available) {
this.startBluetoothDevicesDiscovery()
}
})
}
}
})
},
getBluetoothAdapterState() {
wx.getBluetoothAdapterState({
success: (res) => {
console.log(‘getBluetoothAdapterState’, res)
if (res.discovering) {
this.onBluetoothDeviceFound()
} else if (res.available) {
this.startBluetoothDevicesDiscovery()
}
}
})
},
startBluetoothDevicesDiscovery() {
if (this._discoveryStarted) {
return
}
this._discoveryStarted = true
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: (res) => {
console.log(‘startBluetoothDevicesDiscovery success’, res)
this.onBluetoothDeviceFound()
},
})
},
stopBluetoothDevicesDiscovery() {
wx.stopBluetoothDevicesDiscovery()
},
onBluetoothDeviceFound() {
wx.onBluetoothDeviceFound((res) => {
res.devices.forEach(device => {
if (!device.name && !device.localName) {
return
}
const foundDevices = this.data.devices
const idx = inArray(foundDevices, ‘deviceId’, device.deviceId)
const data = {}
if (idx === -1) {
data[`devices[${foundDevices.length}]`] = device
} else {
data[`devices[${idx}]`] = device
}
this.setData(data)
})
})
},
createBLEConnection(e) {
console.log(‘建立连接’)
console.log(e)
const ds = e.currentTarget.dataset
const deviceId = ds.deviceId
const name = ds.name
wx.createBLEConnection({
deviceId,
success: (res) => {
this.setData({
connected: true,
name,
deviceId,
})
this.getBLEDeviceServices(deviceId)
}
})
// 停止扫描
this.stopBluetoothDevicesDiscovery()
},
closeBLEConnection() {
wx.closeBLEConnection({
deviceId: this.data.deviceId
})
this.setData({
connected: false,
chs: [],
canWrite: false,
})
},
getBLEDeviceServices(deviceId) {
console.log(‘设备id’)
console.log(deviceId)
wx.getBLEDeviceServices({
deviceId,
success: (res) => {
console.log(res)
for (let i = 0; i < res.services.length; i++) {
if (res.services[i].isPrimary) {
this.getBLEDeviceCharacteristics(deviceId, res.services[1].uuid)
return
}
}
}
})
},
getBLEDeviceCharacteristics(deviceId, serviceId) {
var that = this
console.log(‘设备id:’ + deviceId)
console.log(‘UUID:’ + serviceId)
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: (res) => {
console.log(‘getBLEDeviceCharacteristics 成功’, res.characteristics)
this.setData({
chs: res.characteristics,//数组
deviceId:deviceId,
serviceId:serviceId,
f1:res.characteristics[0].uuid,
f4:res.characteristics[1].uuid
})
for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i]
console.log(item)
if (item.properties.read) {
wx.readBLECharacteristicValue({
deviceId,
serviceId,
characteristicId: item.uuid,
})
}
if (item.properties.notify || item.properties.indicate) {
wx.notifyBLECharacteristicValueChange({
state: true,
deviceId,
serviceId,
characteristicId: item.uuid,//F4
success: function(res) {
console.log('notify 开启成功 ’ , res)
},
fail(res){
console.log('notify 开启失败 ’ , res)
},
})
}
if (item.properties.write) {
this.setData({
canWrite: true
})
this._deviceId = deviceId
this._serviceId = serviceId
this._characteristicId = item.uuid
this.writeBLECharacteristicValue()
}
}
},
fail(res) {
console.error(‘getBLEDeviceCharacteristics’, res)
}
})
// 操作之前先监听,保证第一时间获取数据
// wx.onBLECharacteristicValueChange((characteristic) => {
// console.log(‘onBLECharacteristicValueChange操作之前先监听,保证第一时间获取数据’)
// const idx = inArray(this.data.chs, ‘uuid’, characteristic.characteristicId)
// console.log(‘idx:’,idx)
// const data = {}
// if (idx === -1) {
// data[`chs[${this.data.chs.length}]`] = {
// uuid: characteristic.characteristicId,
// value: ab2hex(characteristic.value),
// }
// console.log(’-1-1-上 直接有返回值’)
// console.log(data)
// } else {
// data[`chs[${idx}]`] = {
// uuid: characteristic.characteristicId,
// value: ab2hex(characteristic.value)
// }
// console.log(‘1-1-1下 直接有返回值’)
// console.log(data)
// }
// this.setData(data)
// console.log(‘F1 F4 数组::’ , this.data.chs)
// })
},
// 自动连接 数据发送
writeBLECharacteristicValue() {
console.log(‘自动连接 数据发送 发送一个0x00的16进制数据’)
var that = this
// var hex = ‘<210200000011,10>’
var hex = ‘<0024123456,10>’
// var hex = that.data.commend;
var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16)
}))
var buffer = typedArray.buffer
// 向蓝牙设备发送一个0x00的16进制数据 // let buffer = new ArrayBuffer(1) // let dataView = new DataView(buffer) // dataView.setUint8(0, Math.random() * 255 | 0)
console.log(that.data.f1)
wx.writeBLECharacteristicValue({
deviceId: this._deviceId,
serviceId: this._serviceId,
characteristicId: this._characteristicId,
// deviceId:that.data.deviceId,
// serviceId:that.data.serviceId,
// characteristicId: that.data.f1,
value: buffer,
success: function(res) {
console.log(‘发送数据成功 走两遍是因为上边有循环’)
console.log(res)
wx.onBLECharacteristicValueChange(function(characteristic) {
console.log(‘characteristic value comed:’, characteristic)
console.log(‘res value comed:’, characteristic.value)
console.log(‘value:’+ ab2hex(characteristic.value))
var val = ab2hex(characteristic.value)
if(val){
that.setData({
onvalue:val
})
}
})
},
fail(res){
console.log('写入失败 ’ , res)
},
})
console.log(‘这个是访问的返回值’ + that.data.value)
},
// 结束流程 断开连接
closeBluetoothAdapter() {
wx.closeBLEConnection({
deviceId: this.data.deviceId
})
this.setData({
connected: false,
chs: [],
canWrite: false,
})
// wx.closeBluetoothAdapter()
// this._discoveryStarted = false
},
// 开启电瓶
kaiqi() {
var that = this
var hex = ‘<210200000011,20,01>’
// var hex = that.data.commend;
var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16)
}))
var buffer = typedArray.buffer
// console.log(that.data.value)//一个返回值 特性值
wx.notifyBLECharacteristicValueChange({
deviceId:that.data.deviceId,
serviceId:that.data.serviceId,
characteristicId: that.data.f4,
state: true,
success: function(res) {
console.log(‘notify 启动 成功’, res)
},
fail: function(res) {
// fail
console.log(‘开启电瓶报错了, 10006 链接已断开’)
console.log(res);
},
})
setTimeout(function(){
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.f4,
value: buffer,
success: function(res) {
console.log(“写入成功:::”, res)
wx.onBLECharacteristicValueChange(function(characteristic) {
console.log(‘characteristic value comed:’, characteristic)
console.log(‘res value comed:’, characteristic.value)
console.log(‘阿斯value:’+ ab2hex(characteristic.value))
})
}
})
},1000)
},
// 关闭电瓶
guanguan: function() {
var that = this;
// var hex = that.data.commend2;
var hex = ‘<210200000011,30,01>’
var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16)
}))
var buffer1 = typedArray.buffer
console.log(‘UUID:0000fff4:为读写属性,小程序向里写指令::’ + that.data.f4)
// console.log(buffer1)
wx.notifyBLECharacteristicValueChange({
deviceId:that.data.deviceId,
serviceId:that.data.serviceId,
characteristicId: that.data.f4,
state: true,
success: function(res) {
console.log(‘notify 启动 成功’, res)
},
fail: function(res) {
// fail
console.log(‘开启电瓶报错了, 10006 链接已断开’)
console.log(res);
},
})
setTimeout(function(){
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.f4,
value: buffer1,
success: function(res) {
// success
console.log(“success 指令发送成功”);
console.log(res);
wx.onBLECharacteristicValueChange(function(res) {
console.log(‘关锁’)
console.log(‘characteristic value comed:’, characteristic)
console.log(‘res value comed:’, res)
console.log(‘value:’+ ab2hex(res.value))
})
},
fail: function(res) {
// fail
console.log(res);
},
})
},1000)
},
})
麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html