当前位置:网站首页>微信小程序与低功耗蓝牙通信-接受硬件端发送来的数据(四)
微信小程序与低功耗蓝牙通信-接受硬件端发送来的数据(四)
2022-04-23 14:02:00 【1029179954】
接受数据只要 wx.notifyBLECharacteristicValueChange监听器打开,
wx.onBLECharacteristicValueChange是接受数据的函数。
数据接收到后需要做数据解析
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}
这个过程也需要获取接口对应参数:
如何获取请看微信小程序与低功耗蓝牙通信-往硬件端发送数据
deviceId: "2C:AB:33:33:94:08",
serviceId: "0808FF00-0808-0A09-0807-060504030201",
characteristicId:"0808FF01-0808-0A09-0807-060504030201",
微信小程序获取数据:
放在onload函数
onLoad:function(){
console.log("监听hc-09传来的数据")
//打开监听器 获取hc-09发来的数据
wx.notifyBLECharacteristicValueChange({
state: true,
deviceId: "2C:AB:33:33:94:08",
serviceId: "0808FF00-0808-0A09-0807-060504030201",
characteristicId:"0808FF01-0808-0A09-0807-060504030201",
success: function (res) {
console.log('notifyBLECharacteristicValueChange success', res.errMsg)
}
})
//hc-09传来的数据解析 转换为字符串
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}
//监听hc-09传来的数据
wx.onBLECharacteristicValueChange(function (res) {
console.log('hc-09传来的数据是:', ab2hex(res.value))
var date=ab2hex(res.value)
//根据hc-09传来的数据调手机震动 on==开 off==关
if(date==="on"){
console.log("打开震动")
wx.vibrateLong();
}else{
console.log("关闭震动")
}
})
},
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
微信小程序通过低功耗蓝牙设备进行定位及测距(二)
微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
更多关于微信小程序与蓝牙的知识关注下面公众号:
小白XBIT
版权声明
本文为[1029179954]所创,转载请带上原文链接,感谢
https://blog.csdn.net/baidu_38978508/article/details/123444787
边栏推荐
- try --finally
- JS 力扣刷题 103. 二叉树的锯齿形层序遍历
- JS 力扣刷题 102. 二叉树的层序遍历
- mysql通过binlog文件恢复数据
- 收藏博客贴
- Express ② (routage)
- 力扣刷题 101. 对称二叉树
- Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
- Port occupied 1
- [code analysis (2)] communication efficient learning of deep networks from decentralized data
猜你喜欢
Interesting talk about network protocol
STM32 learning record 0007 - new project (based on register version)
STM32学习记录0007——新建工程(基于寄存器版)
Postman reference summary
Programming travel function
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
Pytorch 经典卷积神经网络 LeNet
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
Kettle--控件解析
Nodejs安装及环境配置
随机推荐
容差分析相关的计算公式
Express ② (routing)
[code analysis (6)] communication efficient learning of deep networks from decentralized data
PySide2
Universal template for scikit learn model construction
Android: answers to the recruitment and interview of intermediate Android Development Agency in early 2019 (medium)
STM32 learning record 0007 - new project (based on register version)
【项目】小帽外卖(八)
mysql新表,自增id长达20位,原因竟是......
初探 Lambda Powertools TypeScript
China creates vast research infrastructure to support ambitious climate goals
JS force deduction brush question 102 Sequence traversal of binary tree
scikit-learn构建模型的万能模板
程序编译调试学习记录
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
About note 1
JS 力扣刷题 103. 二叉树的锯齿形层序遍历
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
Express②(路由)