当前位置:网站首页>微信小程序wx.writeBLECharacteristicValue汉字转buffer问题
微信小程序wx.writeBLECharacteristicValue汉字转buffer问题
2022-08-10 05:31:00 【梵鸽子】
1、对于utf-8编码的中文汉字转成buffer类型,一个汉字三个字节,由于设备不支持组包,所以超过20字节的也没有进行拆包。仅提供一个方案,下面贴代码:
stringToArrayBuffer(str) {
var bytes = new Array();
var len, c;
len = str.length;
for (var i = 0; i < len; i++) {
c = str.charCodeAt(i);
if (c >= 0x010000 && c <= 0x10ffff) {
bytes.push(((c >> 18) & 0x07) | 0xf0);
bytes.push(((c >> 12) & 0x3f) | 0x80);
bytes.push(((c >> 6) & 0x3f) | 0x80);
bytes.push((c & 0x3f) | 0x80);
} else if (c >= 0x000800 && c <= 0x00ffff) {
bytes.push(((c >> 12) & 0x0f) | 0xe0);
bytes.push(((c >> 6) & 0x3f) | 0x80);
bytes.push((c & 0x3f) | 0x80);
} else if (c >= 0x000080 && c <= 0x0007ff) {
bytes.push(((c >> 6) & 0x1f) | 0xc0);
bytes.push((c & 0x3f) | 0x80);
} else {
bytes.push(c & 0xff);
}
}
var array = new Int8Array(bytes.length);
for (var i in bytes) {
array[i] = bytes[i];
}
return array.buffer;
},
var uuidandpassword = that.data.SSID + "," + that.data.password
var buffer = that.stringToArrayBuffer(uuidandpassword);
wx.writeBLECharacteristicValue({
deviceId: that.data.connectedDeviceId,
serviceId: that.data.services[0].uuid,
characteristicId: that.data.characteristics[0].uuid,
value: buffer,
success: function (res) {
console.log("res:",res)
console.log(uuidandpassword)
},
fail: function (res) {
console.log(res)
}
})
wx.writeBLECharacteristicValue(Object object) | 微信开放文档微信开发者平台文档
https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-ble/wx.writeBLECharacteristicValue.html目前测试中文汉字6个,其他字符10位,是可以发送的,例如:“用于测试名称,12345678900” 长度为30个字节是可以发送的,由于无法修改设备摆烂。
希望对你有帮助。
边栏推荐
- 图纸怎么折?(A0,A1,A2,A3の图纸如何折成A4大小)
- impdp 导入数据
- canvas canvas drawing clock
- IDEA的database使用教程(使用mysql数据库)
- pygame学习计划(1)
- PCL点云滤波
- 基于Qiskit——《量子计算编程实战》读书笔记(四)
- AVL tree insertion--rotation notes
- Interface documentation evolution illustration, some ancient interface documentation tools, you may not have used it
- 你不知道的常规流
猜你喜欢
每周推荐短视频:探索AI的应用边界
深度学习模型训练前的必做工作:总览模型信息
在yolov5的网络结构中添加注意力机制模块
自适应空间特征融合( adaptively spatial feature fusion)一种基于数据驱动的金字塔特征融合策略
Interface documentation evolution illustration, some ancient interface documentation tools, you may not have used it
用Pytorch从0到1实现逻辑回归
Read the excerpt notes made by dozens of lightweight target detection papers for literacy
接口调试还能这么玩?
How does Jenkins play with interface automation testing?
pytorch框架学习(7) tensorboard使用
随机推荐
Transforming into a product, is it reliable to take the NPDP test?
AWR1843型号毫米波雷达使用
如何在报表控件FastReport.NET中连接XLSX 文件作为数据源?
redis集群模式
pygame学习计划(1)
细数国产接口协作平台的六把武器!
论文精度 —— 2017 ACM《Globally and Locally Consistent Image Completion》
训练集Loss收敛,但是测试集Loss震荡的厉害?
IDEA连接MySQL数据库并执行SQL查询操作
Talk about API Management - Open Source Edition to SaaS Edition
利用PyQt5制作YOLOv5的GUI界面
树莓派入门(3)树莓派GPIO学习
scikit-learn机器学习 读书笔记(二)
Error when installing oracle rac 11g and executing root.sh
Touch chip used in smart touch remote control
OneFlow源码解析:算子指令在虚拟机中的执行
pytorch框架学习(2)使用GPU训练
接口调试还能这么玩?
SEO搜索引擎优化
MongoDB 基础了解(一)