当前位置:网站首页>Wechat applet positioning and ranging through low-power Bluetooth device (2)

Wechat applet positioning and ranging through low-power Bluetooth device (2)

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

Principle of positioning and ranging
Wechat applet searches for nearby Bluetooth devices , Get the signal by specifying the Bluetooth name rssi, Then it is transformed into distance through signal strength , And then realize positioning and ranging .( See code for specific implementation )
The formula of signal loudness to distance
 Insert picture description here
Code implementation

var pointBRSSi = res.devices[i].RSSI;
var iRssi = Math.abs(pointBRSSi);  
var power = (iRssi-55)/(10*2.0);  
var pointBDistance = Math.pow(10, power);

Wechat applet positioning and ranging
js


// Search for nearby Bluetooth devices   At the same time find hc-09rssi   Convert to distance 
toClock1: function () { 
  var that = this;
  //console.log(that.data.timer)
// Bluetooth initialization 
  wx.openBluetoothAdapter({
    success: function (res) {
      console.log(" Initialize Bluetooth adapter ");
      /*getBluetoothAdapterState()  Get the status of the native Bluetooth adapter , Determine if it is available ,available by false Because the user does not turn on the system Bluetooth */
      wx.getBluetoothAdapterState({
        success:function (res) {
          // Print related information 
          console.log(JSON.stringify(res.errMsg) + "\n Bluetooth is available :" + res.available);
          // Adapter available 
          if(res.available){
            
              //clearTimeout(timer);
            // Search for Bluetooth devices 
            wx.startBluetoothDevicesDiscovery({
              services: [],
              allowDuplicatesKey: false,
              success: function (res) {
                // Get Bluetooth device output information list 
                wx.getBluetoothDevices({
                  success: function (res) {
                    console.log(' Number of search devices :' + res.devices.length)
                    console.log(' Equipment information :\n' + JSON.stringify(res.devices)+"\n")
                    for (var i = 0; i < res.devices.length; i++) {
                      //console.log(" The first "+(i+1) + " individual RSSI:" + res.devices[i].RSSI+"\n")
                      if(res.devices[i].name==="HC-09"){
                        var rssi=res.devices[i].RSSI
                       // var pointBRSSi = res.devices[i].RSSI;
                        var iRssi = Math.abs(rssi);  
                        var power = (iRssi-55)/(10*2.0);  
                        var pointBDistance = Math.pow(10, power);
                        console.log("hc-o9 What is the distance from the mobile phone :"+pointBDistance)
                        that.setData({
                          dis:pointBDistance
                        })
                        // Greater than 5 rice    shock 
                        if(pointBDistance>5){
                          wx.vibrateLong();
                        }
                      }
                    }
                  }
                })
              },
              fail: function (err) {
                console.log(err);
                wx.showModal({
                  title: ' reminder ',
                  content: ' Bluetooth search failed '
                })
              }
            });
        
          }else{
            wx.showModal({
              title: ' reminder ',
              content: ' Bluetooth device not available '
            })
          }
        
        },
        fail: function (res) {
          // Print related information 
          console.log(JSON.stringify(res.errMsg) + "\n Bluetooth is available :" + res.available);
          wx.showModal({
            title: ' reminder ',
            content: ' Bluetooth device not available '
          })
        }
      })
    
    },
    fail: function (err) {
      console.log(err);
      wx.showToast({
        title: ' Bluetooth initialization failed ',
        icon: 'fail',
        duration: 2000
      })
       // The search timeout     Stop scanning the device 
    setTimeout(function () {
      var that = this;
      wx.stopBluetoothDevicesDiscovery({
        success: function (res) {
          console.log(" stop searching " + JSON.stringify(res.errMsg));
        }
      })
      }, 4000); 
    }
  });
 // },10000)
},

About Bluetooth initialization of wechat applet 、 To search for nearby Bluetooth devices and specify Bluetooth connections, you can refer to the following articles :
https://blog.csdn.net/baidu_38978508/article/details/123439507?spm=1001.2014.3001.5502
More about wechat applet knowledge and javaweb Develop knowledge and pay attention to the following official account for more source code. :
 Insert picture description here

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