当前位置:网站首页>微信小程序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个字节是可以发送的,由于无法修改设备摆烂。
希望对你有帮助。
边栏推荐
猜你喜欢
Kubernetes:(十七)Helm概述、安装及配置
Qiskit官方文档选译之量子傅里叶变换(Quantum Fourier Transform, QFT)
scikit-learn机器学习 读书笔记(二)
深度学习中的学习率调整策略(1)
论文精度 —— 2017 ACM《Globally and Locally Consistent Image Completion》
论文精度 —— 2016 CVPR 《Context Encoders: Feature Learning by Inpainting》
AVL tree insertion--rotation notes
基于Qiskit——《量子计算编程实战》读书笔记(六)
Pony语言学习(六):Struct, Type Alias, Type Expressions
动手写prometheus的exporter-02-Counter(计数器)
随机推荐
深度学习中数据到底要不要归一化?实测数据来说明!
An article will help you understand what is idempotency?How to solve the idempotency problem?
三维点云分割
OAuth2 usage scenarios, common misunderstandings, use cases
使用Tenserboard可视化深度学习训练过程
Kubernetes:(十六)Ingress的概念和原理
cesium 旋转图片
matlab中的常用的类型转换
基于Qiskit——《量子计算编程实战》读书笔记(二)
aliases node analysis
pytorch框架学习(5)torchvision模块&训练一个简单的自己的CNN (二)
Attention candidates for the soft exam! The detailed registration process for the second half of 2022 is coming!
Canal 报错 Could not find first log file name in binary log index file
从GET切换为POST提交数据的方法
手把手带你写嵌入式物联网的第一个项目
Thread.sleep, Thread.yield role explanation
Transforming into a product, is it reliable to take the NPDP test?
如何用Apifox 的智能Mock功能?
图纸怎么折?(A0,A1,A2,A3の图纸如何折成A4大小)
Interface documentation evolution illustration, some ancient interface documentation tools, you may not have used it