当前位置:网站首页>Wechat applet communicates with low-power Bluetooth - sending data to hardware (III)

Wechat applet communicates with low-power Bluetooth - sending data to hardware (III)

2022-04-23 14:05:00 one billion twenty-nine million one hundred and seventy-nine th

preparation :
Software : Wechat applet
Hardware :
Bluetooth devices :hc-09
Single chip microcomputer :stm32
Wechat applet sends data to the hardware , The corresponding function is wx.writeBLECharacteristicValue, His parameters are :( The corresponding parameters below are 2 There are two ways to get it (1): Through hardware at Instructions for ,(2) It can also be obtained through the characteristic value of wechat applet )

deviceId:"2C:AB:33:33:94:08",
serviceId:"0808FF00-0808-0A09-0807-060504030201",
characteristicId:"0808FF01-0808-0A09-0807-060504030201",

value: buffer( The type of data to be transmitted is buffer, Must be buffer, Otherwise, data transmission fails )
Specific as the following code

let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0,0x23)

Parameter acquisition method :
deviceId:“2C:AB:33:33:94:08”,
serviceId:“0808FF00-0808-0A09-0807-060504030201”,
characteristicId:“0808FF01-0808-0A09-0807-060504030201”,
(1)wx.getBLEDeviceCharacteristics obtain serviceId、characteristicId
deviceId according to getBluetoothDevices or onBluetoothDeviceFound Interface

wx.getBLEDeviceCharacteristics({
                //  there  deviceId  according to   getBluetoothDevices  or  onBluetoothDeviceFound  Interface 
                // deviceId: that.data.connectedDeviceId,
                deviceId: "deviceId",
                //  there  serviceId  according to   getBLEDeviceServices  Interface 
                serviceId: "serviceId ",
                success: function(res) {
                  for (var i = 0; i < res.characteristics.length; i++) {
                    if (res.characteristics[i].properties.notify) {
                      console.log(" The first " + 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) adopt at Instructions for
All parameters and data types are ready for data transmission
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(" binary data :"+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",
          //  there value yes ArrayBuffer type 
          value: buffer,
          success: function (res) {
            console.log(' Write successfully ', res.errMsg)
          },
          fail(res){
            console.log(' Write failure ', res.errMsg)
          }
        })

Wechat applet for Bluetooth initialization 、 Search for nearby Bluetooth devices and connect to designated Bluetooth devices ( One )
https://blog.csdn.net/baidu_38978508/article/details/123439507?spm=1001.2014.3001.5502
Wechat applet performs positioning and ranging through low-power Bluetooth devices ( Two )
https://blog.csdn.net/baidu_38978508/article/details/123441079
More about WeChat's small program and Bluetooth knowledge, the following official account. :
The small white XBIT

版权声明
本文为[one billion twenty-nine million one hundred and seventy-nine th]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401297720.html