无论输入什么?监听到的都是一堆0,这是什么原因?
// 【6】获取服务
getServices() {
let that = this
uni.getBLEDeviceServices({
deviceId: that.deviceId,
success(res) {
that.serviceId = res.services[0].uuid
uni.showToast({
title: '获取服务成功'
})
},
fail(err) {
console.error(err)
uni.showToast({
title: '获取服务失败',
icon: 'error'
})
}
})
},
// 【7】获取特征值
getCharacteristics() {
let that = this
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: that.serviceId,
success(res) {
that.characteristicId = res.characteristics[0].uuid //微信小程序调试
uni.showToast({
title: '获取特征值成功'
})
},
fail(err) {
console.error(err)
uni.showToast({
title: '获取特征值失败',
icon: 'error'
})
}
})
},
// 【8】开启消息监听
notify() {
let that = this
uni.notifyBLECharacteristicValueChange({
deviceId: that.deviceId, // 设备id
serviceId: that.serviceId, // 监听指定的服务
characteristicId: that.characteristicId, // 监听对应的特征值
state: true,
type:'notification',
success: (res) => {
console.log(res);
if (res.errMsg == "notifyBLECharacteristicValueChange:ok") {
console.log("开启消息监听------", res);
this.listenMessage()
}
},
fail(err) {
console.error(err)
uni.showToast({
title: '监听失败',
icon: 'error'
})
}
})
},
// 16进制字符串转ArrayBuffer
hex2ArrayBuffer() {
let hex_str = '68656c6c6f20776f726c64'
let typedArray = new Uint8Array(hex_str.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16)
}))
let buffer = typedArray.buffer
return buffer
},
// 【9】监听消息变化
listenMessage() {
let that = this
console.log(9999999)
uni.onBLECharacteristicValueChange((res) => {
console.log("发送过来的数据---------", res)
let resHex = that.ab2hex(res.value)
console.log(resHex)
that.messageHex = resHex
let result = that.hexCharCodeToStr(resHex)
console.log(String(result))
that.message = String(result)
})
},
// 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('')
},
// 将16进制的内容转成我们看得懂的字符串内容
hexCharCodeToStr(hexCharCodeStr) {
var trimedStr = hexCharCodeStr.trim();
var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
var len = rawStr.length;
console.log(len)
if (len % 2 !== 0) {
alert("存在非法字符!");
return "";
}
var curCharCode;
var resultStr = [];
for (var i = 0; i < len; i = i + 2) {
curCharCode = parseInt(rawStr.substr(i, 2), 16);
resultStr.push(String.fromCharCode(curCharCode));
}
console.log(resultStr)
return resultStr.join("");
},
你好,麻烦在手机微信那里上传下日志: 我->设置->帮助与反馈右上角有个上报日志的入口,麻烦提供一下微信号,时间点