当前位置:网站首页>Wechat applet communicates with low-power Bluetooth - receives data sent by hardware (IV)

Wechat applet communicates with low-power Bluetooth - receives data sent by hardware (IV)

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

Accept data as long as wx.notifyBLECharacteristicValueChange Listener on ,
wx.onBLECharacteristicValueChange Is a function that accepts data .
After receiving the data, you need to analyze the data

 function ab2hex(buffer) {
    var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function(bit) {
    return ('00' + bit.toString(16)).slice(-2)
    }
    )
    return hexArr.join('');
    }

This process also needs to obtain the corresponding parameters of the interface :
How to get it, please see Wechat applet communicates with low-power Bluetooth - Send data to the hardware side

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

Wechat applet to obtain data :
Put it in onload function


onLoad:function(){
    console.log(" monitor hc-09 Incoming data ")
   // Turn on the listener    obtain hc-09 Data sent 
   wx.notifyBLECharacteristicValueChange({
    state: true, 
    deviceId: "2C:AB:33:33:94:08",
    serviceId: "0808FF00-0808-0A09-0807-060504030201",
    characteristicId:"0808FF01-0808-0A09-0807-060504030201",
    success: function (res) {
    console.log('notifyBLECharacteristicValueChange success', res.errMsg)
    }
    })
    //hc-09 Analysis of incoming data   Convert to string 
    function ab2hex(buffer) {
    var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function(bit) {
    return ('00' + bit.toString(16)).slice(-2)
    }
    )
    return hexArr.join('');
    }
    // monitor hc-09 Incoming data 
    wx.onBLECharacteristicValueChange(function (res) {
    console.log('hc-09 The data is :', ab2hex(res.value))
    var date=ab2hex(res.value)
    // according to hc-09 The data from the phone vibrates  on== open  off== Turn off 
    if(date==="on"){
        console.log(" Open vibration ")
        wx.vibrateLong();
    }else{
        console.log(" Turn off vibration ")
    }
    })

   

},

Wechat applet for Bluetooth initialization 、 Search for nearby Bluetooth devices and connect to designated Bluetooth devices ( One )
Wechat applet performs positioning and ranging through low-power Bluetooth devices ( Two )
Wechat applet communicates with low-power Bluetooth - Send data to the hardware side ( 3、 ... and )
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/202204231401297689.html