当前位置:网站首页>微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
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
边栏推荐
- Scientists say Australian plan to cull up to 10,000 wild horses doesn’t go far enough
- 生产环境——
- [code analysis (1)] communication efficient learning of deep networks from decentralized data
- [code analysis (2)] communication efficient learning of deep networks from decentralized data
- Nodejs安装及环境配置
- 村上春树 --《当我谈跑步时,我谈些什么》句子摘录
- 服务器中挖矿病毒了,屮
- JDBC入门
- 腾讯根据ip解析地址
- AtomicIntegerArray源码分析与感悟
猜你喜欢

Oracle告警日志alert.log和跟踪trace文件中文乱码显示

Postman reference summary

大专的我,闭关苦学 56 天,含泪拿下阿里 offer,五轮面试,六个小时灵魂拷问

PATH环境变量

JMeter pressure test tool

程序编译调试学习记录

初探 Lambda Powertools TypeScript

STM32学习记录0007——新建工程(基于寄存器版)

Pytorch 经典卷积神经网络 LeNet

Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)
随机推荐
程序编译调试学习记录
Nacos+AspnetCore+Ocelot实战编码
Wechat applet
记录一个奇怪的bug:缓存组件跳转之后出现组件复制
YARN线上动态资源调优
Problems encountered in the project (V) understanding of operating excel interface poi
Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
scikit-learn構建模型的萬能模板
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
第一章 电商秒杀商品回顾
Introduction to spark basic operation
cnpm的诡异bug
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
关于pthread多线程一些好文章
Jenkins construction and use
Function executes only the once function for the first time
leetcode--357. 统计各位数字都不同的数字个数
FBS(fman build system)打包
Android: answers to the recruitment and interview of intermediate Android Development Agency in early 2019 (medium)
[code analysis (7)] communication efficient learning of deep networks from decentralized data