灿娘娘
灿娘娘
  • 发布:2022-06-16 11:20
  • 更新:2022-06-16 11:20
  • 阅读:664

uni.onBLECharacteristicValueChang蓝牙监听到的value转成16进制是一堆0???

分类:uni-app

// 【8】开启消息监听
notify() {
let that = this
uni.notifyBLECharacteristicValueChange({
deviceId: that.deviceId, // 设备id
serviceId: that.serviceId, // 监听指定的服务
characteristicId: that.characteristicId, // 监听对应的特征值
state: true,
success: (res) => {
console.log(res);
if (res.errMsg == "notifyBLECharacteristicValueChange:ok") {
console.log("开启消息监听------", res);
// uni.hideLoading();
// this.cut = false;
// this.BLEValue();
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)
//-------------模拟监听到的数据转16进制就没有问题------------
// let hexBuffer = that.hex2ArrayBuffer()
// let resHex = that.ab2hex(hexBuffer)
//------------模拟监听到的数据转16进制就没有问题------------
console.log(resHex)
that.messageHex = resHex
// 68656c6c6f20776f726c64---hello world
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("");  
        },
2022-06-16 11:20 负责人:无 分享
已邀请:

该问题目前已经被锁定, 无法添加新回复

  • 标题 / 粗斜体
  • 代码片段
  • 超链接 / 图片 / 视频
  • 列表 / 引用

文章内容较多时,可以用标题分段 :

## 大标题 
### 小标题

斜体 / 粗体 :

**粗体** 
*斜体*
***粗斜体***

代码片段 :

``` javascript
代码片段
```

超链接 :

[链接文字](链接地址) 例: [百度](http://www.baidu.com)

图片 :

![图片说明](图片地址) 例: ![百度logo](http://www.baidu.com/img/bdlogo.gif)

视频 :

!![视频说明](视频地址) 例: !![优酷视频](http://youku.com)

有序列表 :

1. 123
2. 123
3. 123

无序列表 :

- 123
- 123
- 123

引用 : ( 双回车后结束引用 )

> 引用内容
引用内容
引用内容