当前位置:网站首页>微信小程序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个字节是可以发送的,由于无法修改设备摆烂。
希望对你有帮助。
边栏推荐
猜你喜欢
随机推荐
毫米波雷达基础概念学习
pygame学习计划(1)
使用Tenserboard可视化深度学习训练过程
[Thesis Notes] Prototypical Contrast Adaptation for Domain Adaptive Semantic Segmentation
聊聊 API 管理-开源版 到 SaaS 版
接口文档进化图鉴,有些古早接口文档工具,你可能都没用过
latex图片排版技巧总结
文章复现:超分辨率网络-VDSR
How does Jenkins play with interface automation testing?
网络安全5
el-dropdown下拉菜单样式修改,去掉小三角
手把手带你写嵌入式物联网的第一个项目
Qiskit 学习笔记2
pytorch框架学习(4)torchvision模块&训练一个简单的自己的CNN (一)
深度学习中的学习率调整策略(1)
Error when installing oracle rac 11g and executing root.sh
MySql's json_extract function processes json fields
scikit-learn机器学习 读书笔记(二)
Practical skills 19: Several postures of List to Map List
基于Qiskit——《量子计算编程实战》读书笔记(二)









