api是这样使用吗
export default {
data(){
return {
}
},
onLoad(){
},
methods:{
// ArrayBuffer转16进度字符串示例
ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
}
},
//监听低功耗蓝牙设备的特征值变化事件
uni.onBLECharacteristicValueChange(function (res) {
console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
console.log(_this.ab2hex(res.value))
})
}
还是这样使用
methods:{
//启用低功耗蓝牙设备特征值变化时的 notify 功能
notifyBLECharacteristicValueChange(){
uni.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: _this.deviceId,
serviceId:_this.serviceUUID,
characteristicId: _this.characteristicId[0].uuid,
success(res) {
console.log('启用低功耗蓝牙设备特征值变化时的 notify 功能',res)
//监听低功耗蓝牙设备的特征值变化事件
uni.onBLECharacteristicValueChange(function (res) {
console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
console.log(_this.ab2hex(res.value))
})
var hex = '2F24941FA8525D75E0497A548B43F6E'//需要发送的指令
var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16)
}))
var buffer1 = typedArray.buffer
uni.writeBLECharacteristicValue({
deviceId: DeviceID,
serviceId: _this.ServiceUUID[2].uuid,
characteristicId: _this.characteristicId[0].uuid,
// 这里的value是ArrayBuffer类型
value: buffer1,
success(res) {
console.log('writeBLECharacteristicValue success', res.errMsg)
console.log('writeBLECharacteristicValue success', res)
}
})
}
})
},
// ArrayBuffer转16进度字符串示例
ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
}
}
我没有将完整的代码放出来,因为代码太长了,我就把关键问题放出来了
已经进入写入成功的回调里面了,但我现在拿不到设备的返回值
还有就是请教一下,我需要发送的指令是一串32位的16进制指令
请问这样处理是正确的的吗
var hex = '2F24941FA8525D75E0497A548B43F6E'//需要发送的指令
var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16)
}))
var buffer1 = typedArray.buffer
0 个回复