当前位置:网站首页>微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
2022-04-23 14:02:00 【1029179954】
准备工作:
软件:微信小程序
硬件:
蓝牙设备:hc-09
单片机:stm32
微信小程序往硬件端发送数据,对应的函数是wx.writeBLECharacteristicValue,他的参数是:(对应的下面的参数获取有2种获取方式(1):通过硬件的at指令获取,(2)也可以通过微信小程序特征值获取)
deviceId:"2C:AB:33:33:94:08",
serviceId:"0808FF00-0808-0A09-0807-060504030201",
characteristicId:"0808FF01-0808-0A09-0807-060504030201",
value: buffer(往过传数据的类型是buffer,必须是buffer,不然传数据失败)
具体如下面的代码
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0,0x23)
参数获取方式:
deviceId:“2C:AB:33:33:94:08”,
serviceId:“0808FF00-0808-0A09-0807-060504030201”,
characteristicId:“0808FF01-0808-0A09-0807-060504030201”,
(1)wx.getBLEDeviceCharacteristics获取serviceId、characteristicId
deviceId 根据 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
wx.getBLEDeviceCharacteristics({
// 这里的 deviceId 根据 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
// deviceId: that.data.connectedDeviceId,
deviceId: "deviceId",
// 这里的 serviceId 根据 getBLEDeviceServices 接口中获取
serviceId: "serviceId ",
success: function(res) {
for (var i = 0; i < res.characteristics.length; i++) {
if (res.characteristics[i].properties.notify) {
console.log("第" + i)
console.log(that.data.services[0].uuid);
console.log( res.characteristics[0].uuid);
that.setData({
notifyServicweId: that.data.services[0].uuid,
notifyCharacteristicsId: res.characteristics[0].uuid,
})
}
}
console.log('device getBLEDeviceCharacteristics:', res.characteristics);
that.setData({
msg: JSON.stringify(res.characteristics),
})
},
fail: function(res) {
console.log("fail" + res);
},
complete: function() {
}
})
(2)通过at指令获取
所有参数拿到及数据类型准备完毕进行数据发送
js
var that = this
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0,0x23)
// var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
// return parseInt(h, 16)
// }))
// console.log("二进制数据:"+typedArray)
// var buffer1 = typedArray.buffer
wx.writeBLECharacteristicValue({
deviceId:"2C:AB:33:33:94:08",
serviceId:"0808FF00-0808-0A09-0807-060504030201",
characteristicId:"0808FF01-0808-0A09-0807-060504030201",
// 这里的value是ArrayBuffer类型
value: buffer,
success: function (res) {
console.log('写入成功', res.errMsg)
},
fail(res){
console.log('写入失败', res.errMsg)
}
})
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
https://blog.csdn.net/baidu_38978508/article/details/123439507?spm=1001.2014.3001.5502
微信小程序通过低功耗蓝牙设备进行定位及测距(二)
https://blog.csdn.net/baidu_38978508/article/details/123441079
更多关于微信小程序与蓝牙的知识关注下面公众号:
小白XBIT
版权声明
本文为[1029179954]所创,转载请带上原文链接,感谢
https://blog.csdn.net/baidu_38978508/article/details/123444402
边栏推荐
- Un modèle universel pour la construction d'un modèle d'apprentissage scikit
- UML Unified Modeling Language
- 项目中遇到的问题(五)操作Excel接口Poi的理解
- 分库分表 & ShardingSphere
- Pytorch 经典卷积神经网络 LeNet
- Jenkins construction and use
- 解决方案架构师的小锦囊 - 架构图的 5 种类型
- redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
- SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
- Function executes only the once function for the first time
猜你喜欢
随机推荐
[VMware] address of VMware Tools
JS force deduction brush question 103 Zigzag sequence traversal of binary tree
smart-doc + torna生成接口文档
Interesting talk about network protocol
JS brain burning interview question reward
Express middleware ③ (custom Middleware)
Record a strange bug: component copy after cache component jump
Express中间件③(自定义中间件)
Chapter 15 new technologies of software engineering
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
Strange bug of cnpm
服务器中挖矿病毒了,屮
As a junior college student, I studied hard in closed doors for 56 days, won Ali offer with tears, five rounds of interviews and six hours of soul torture
[code analysis (3)] communication efficient learning of deep networks from decentralized data
Spark入门基本操作
编程旅行之函数
Pytorch 经典卷积神经网络 LeNet
Postman reference summary
Un modèle universel pour la construction d'un modèle d'apprentissage scikit
Express ② (routage)