准备工作:
软件:微信小程序
硬件:
蓝牙设备: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 = thislet 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.bufferwx.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)}})
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
/baidu_38978508/article/details/123439507?spm=1001..3001.5502
微信小程序通过低功耗蓝牙设备进行定位及测距(二)
/baidu_38978508/article/details/123441079
更多关于微信小程序与蓝牙的知识关注下面公众号:
小白XBIT
如果觉得《微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)》对你有帮助,请点赞、收藏,并留下你的观点哦!