当前位置:网站首页>微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
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
边栏推荐
- Expression「Func「TSource, object」」 转Expression「Func「TSource, object」」[]
- cnpm的诡异bug
- 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
- go 语言 数组,字符串,切片
- crontab定时任务输出产生大量邮件耗尽文件系统inode问题处理
- 1256: bouquet for algenon
- 服务器中挖矿病毒了,屮
- freeCodeCamp----time_ Calculator exercise
- STM32 learning record 0007 - new project (based on register version)
- json反序列化匿名数组/对象
猜你喜欢
第一章 电商秒杀商品回顾
Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
scikit-learn構建模型的萬能模板
The latest development of fed digital currency
Express②(路由)
Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
visio安装报错 1:1935 2:{XXXXXXXX...
Choreographer全解析
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
Nacos+AspnetCore+Ocelot实战编码
随机推荐
CentOS mysql多实例部署
Analysis and understanding of atomicintegerarray source code
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
Problems encountered in the project (V) understanding of operating excel interface poi
centOS下mysql主从配置
Oracle alarm log alert Chinese trace and trace files
Scientists say Australian plan to cull up to 10,000 wild horses doesn’t go far enough
AtomicIntegerArray源码分析与感悟
编程旅行之函数
YARN线上动态资源调优
美联储数字货币最新进展
SPC简介
初识go语言
scikit-learn构建模型的万能模板
Neuron and neural network
关于stream流,浅记一下------
Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
Express②(路由)
第一章 电商秒杀商品回顾
2021年秋招,薪资排行NO