当前位置:网站首页>微信小程序与低功耗蓝牙通信-接受硬件端发送来的数据(四)
微信小程序与低功耗蓝牙通信-接受硬件端发送来的数据(四)
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
边栏推荐
猜你喜欢
Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
Express中间件③(自定义中间件)
smart-doc + torna生成接口文档
淘宝发布宝贝提示“您的消保保证金额度不足,已启动到期保障”
Multithreading
freeCodeCamp----time_ Calculator exercise
Nodejs安装及环境配置
第一章 电商秒杀商品回顾
Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
服务器中挖矿病毒了,屮
随机推荐
About note 1
浅谈基于openssl的多级证书,Multi-level CA的签发和管理,以及双向认证
JS 力扣刷题 103. 二叉树的锯齿形层序遍历
程序编译调试学习记录
[VMware] address of VMware Tools
第一章 电商秒杀商品回顾
生产环境——
Redis docker 安装
Express中间件③(自定义中间件)
groutine
Logging模块
村上春树 --《当我谈跑步时,我谈些什么》句子摘录
SPC简介
Oracle alarm log alert Chinese trace and trace files
收藏博客贴
elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
初识go语言
linux MySQL数据定时dump
Tensorflow & pytorch common error reporting
Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)