当前位置:网站首页>微信小程序与低功耗蓝牙通信-接受硬件端发送来的数据(四)
微信小程序与低功耗蓝牙通信-接受硬件端发送来的数据(四)
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
边栏推荐
猜你喜欢
随机推荐
1256:献给阿尔吉侬的花束
解决方案架构师的小锦囊 - 架构图的 5 种类型
[code analysis (2)] communication efficient learning of deep networks from decentralized data
关于stream流,浅记一下------
神经元与神经网络
收藏博客贴
json反序列化匿名数组/对象
Logging模块
剑南春把文字游戏玩明白了
What is the difference between blue-green publishing, rolling publishing and gray publishing?
Strange bug of cnpm
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
Android篇:2019初中级Android开发社招面试解答(中
编程旅行之函数
Jenkins construction and use
低频量化之明日涨停预测
Kettle--控件解析
Port occupied 1
nodejs通过require读取本地json文件出现Unexpected token / in JSON at position
网站_收藏