欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序ibeacon三點(diǎn)定位詳解

 更新時(shí)間:2018年10月31日 09:26:24   作者:coder_daiwang  
這篇文章主要為大家詳細(xì)介紹了微信小程序ibeacon三點(diǎn)定位的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

空余時(shí)間簡(jiǎn)單寫了一個(gè)微信小程序ibeacon三點(diǎn)定位。

事先淘寶買了七八個(gè)ibeacon小設(shè)備,放置在辦公司角落。分別設(shè)置三個(gè)ibeacon的位置坐標(biāo)點(diǎn),根據(jù)每一個(gè)ibeacon到已經(jīng)開(kāi)啟藍(lán)牙的目標(biāo)物距離,計(jì)算出目標(biāo)物在當(dāng)前區(qū)域內(nèi)坐標(biāo)位置。適用于區(qū)域內(nèi)購(gòu)物指示。當(dāng)然,進(jìn)入該區(qū)域事先要打開(kāi)手機(jī)藍(lán)牙。

下面代碼:

var app = getApp() 
Page({
 data: {
  motto: 'Hello World',
  openBLE:'打開(kāi)藍(lán)牙設(shè)備',
  startBLEDiscovery:'初始化藍(lán)牙設(shè)備',
  startBLEDevices:'目標(biāo)定位',
  reStartSearchBLE:'重置藍(lán)牙',
  startSearchBLE:'默認(rèn)空',
  userInfo: {},
  deviceId: '',
  searchFlag:true,
  deviceRSSI:'',
  deviceName:'',
  deviceId:[],
  advertisServiceUUIDs: [],
  advertisData:[],
  canvasPointX:'',
  canvasPointY:'',
  avatarUrl:'../index/bg-image.jpg',
  showPosition:'' 
 },
 //事件處理函數(shù)
 bindViewTap: function() {
  wx.navigateTo({
   url: '../logs/logs'
  })
 },
 onLoad: function () { 
  var that = this
  //調(diào)用應(yīng)用實(shí)例的方法獲取全局?jǐn)?shù)據(jù) 
  //that.setData({ deviceId: opt.deviceId }) 
  app.getUserInfo(function(userInfo){
   //更新數(shù)據(jù) 
   that.setData({
    userInfo:userInfo
   })
   //判斷兼容性
   if (wx.openBluetoothAdapter) { 
   //打開(kāi)藍(lán)牙適配器,如果沒(méi)有打開(kāi) showtoast
   wx.openBluetoothAdapter({
    success: function(res){
     // success
     //獲取本機(jī)的藍(lán)牙適配器狀態(tài)
     wx.getBluetoothAdapterState({
      success: function(res){
       // success
       that.setData({
        searchFlag:true
       }) 
      },
      fail: function(res) {
       // fail 
       that.setData({
        searchFlag:false
       })
      },
      complete: function(res) {
       // complete
      }
     })
    },
    fail: function(res) {
     // fail 本機(jī)是否已經(jīng)打開(kāi)藍(lán)牙設(shè)備
      wx.showToast({title:'請(qǐng)打開(kāi)本機(jī)藍(lán)牙設(shè)備,重新掃碼', duration:10000 })
    },
    complete: function(res) {
     // complete
    }
   })
   } else {
     // 如果希望用戶在最新版本的客戶端上體驗(yàn)?zāi)男〕绦?,可以這樣子提示
     wx.showModal({
      title: '提示',
      content: '當(dāng)前微信版本過(guò)低,無(wú)法使用該功能,請(qǐng)升級(jí)到最新微信版本后重試。'
     })
    } 
    
  })
  }, 
 
  startBLEDevices: function(){
    var that = this 
     wx.startBluetoothDevicesDiscovery({
      services: [],
      success: function(res){ 
       //獲取本機(jī)藍(lán)牙設(shè)備狀態(tài)  
         // success 
       that.setData({
        showPosition:setInterval(that.devicesFunc,1000)
       })  
      },
      fail: function(res) {
      // fail 
     },
     complete: function(res) {
      // complete 
     }
    })
  },
 // startBLEDevices: function(){
 //  var that = this
 //  that.setData({
 //   showPosition:setInterval(that.devicesFunc,1000)
 //  }) 
 // },
  devicesFunc: function(){
    var that = this 
    
    wx.getBluetoothDevices({
     success: function(res){
      // console.log(res); 
      var arrayRSSI = new Array();
      var arraydeviceName = new Array();
      var arraydeviceId = new Array();
      // var arrayUUIDs = new Array();
      var arrayadvertisData = new Array(); 
      var pointADistance = '';
      var pointBDistance = '';
      var pointCDistance = ''; 
      for(var i = 0; i<res.devices.length;i++){ 
        //console.log(res.devices[i].name); 
       if(res.devices[i].name.indexOf('craft')==0){ 
        //console.log(res.devices[i]);
        arrayRSSI.push(res.devices[i].RSSI);
        arraydeviceName.push(res.devices[i].name); 
        arraydeviceId[i]= res.devices[i].deviceId;
        arrayUUIDs[i]= res.devices[i].advertisServiceUUIDs[i];
        arrayadvertisData[i]= res.devices[i].advertisData  ;
        調(diào)用計(jì)算rssi對(duì)應(yīng)距離的函數(shù)
        var iRssi = Math.abs(arrayRSSI[i]); 
        var power = (iRssi-59)/(10*2.0); 
        var mm = Math.pow(10, power); 
        console.log(arraydeviceName[i]+"距離的位置是"+mm+"米");  
        取01,02,03分別為,(2,0),(2,2),(0,2)固定坐標(biāo)點(diǎn),做定位
        
        if(res.devices[i].name.indexOf('craft01')==0){
         
        var pointARSSi = res.devices[i].RSSI ;
        var iRssi = Math.abs(pointARSSi); 
        var power = (iRssi-55)/(10*2.0); 
        var pointADistance = Math.pow(10, power); 
        console.log("a"+pointADistance); 
        console.log(pointARSSi); 
       
        }
        if(res.devices[i].name.indexOf('craft02')==0){
         
        var pointBRSSi = res.devices[i].RSSI;
        var iRssi = Math.abs(pointBRSSi); 
        var power = (iRssi-55)/(10*2.0); 
        var pointBDistance = Math.pow(10, power); 
        console.log("b"+pointBDistance);
        console.log(pointBRSSi); 
        
        }
        if(res.devices[i].name.indexOf('craft03')==0){
        
        var pointCRSSi = res.devices[i].RSSI;
        var iRssi = Math.abs(pointCRSSi); 
        var power = (iRssi-57)/(10*2.0);  
        var pointCDistance = Math.pow(10, power); 
        console.log("c"+pointCDistance); 
        console.log(pointCRSSi); 
        } 
       } 
      } 
      // 從 arrayRSSI 取三個(gè)距離定位點(diǎn)最近的ibeacon參與定位
      if(arrayRSSI.length > 3){
        //根據(jù)arrayRSSI進(jìn)行信號(hào)強(qiáng)弱排序.距離越遠(yuǎn)rssi值越小
        for(var i = 0 ; i < arrayRSSI.length ; i ++){
         for(var j = i+1 ; j< arrayRSSI.length ; j++){
          if(arrayRSSI[i]<arrayRSSI[j]){
           var select = arrayRSSI[i];
           arrayRSSI[i] = arrayRSSI[j];
           arrayRSSI[j] = select;
          }
         } 
        }
        //獲取最近的三個(gè)距離
        for(var i = 0 ; i < 3; i++){
          if(i==0){ 
           var pointARSSi = res.devices[i].RSSI ;
           var iRssi = Math.abs(pointARSSi); 
           var power = (iRssi-55)/(10*2.0); 
           var pointADistance = Math.pow(10, power); 
           console.log("a"+pointADistance); 
           console.log(pointARSSi); 
          }
          if(i==1){
           
           var pointBRSSi = res.devices[i].RSSI;
           var iRssi = Math.abs(pointBRSSi); 
           var power = (iRssi-55)/(10*2.0); 
           var pointBDistance = Math.pow(10, power); 
           console.log("b"+pointBDistance);
           console.log(pointBRSSi); 
          }
          if(i==2){ 
           var pointCRSSi = res.devices[i].RSSI;
           var iRssi = Math.abs(pointCRSSi); 
           var power = (iRssi-57)/(10*2.0);  
           var pointCDistance = Math.pow(10, power); 
           console.log("c"+pointCDistance); 
           console.log(pointCRSSi); 
          }  
        } 
      } 
        //獲取定位點(diǎn)的x和y
        if(!pointADistance==''&&!pointBDistance==''&&!pointCDistance==''){
        var pointDX='';var pointDY = '';
        var p = Math.pow(pointADistance,2)/10-Math.pow(pointBDistance,2)/10;
        pointDX = 2.5 - p; 
 
        var m = Math.pow(pointADistance,2)/10-Math.pow(pointCDistance,2)/10;
        pointDY = 2.5 - m; 
 
        console.log('目標(biāo)所在位置X是'+pointDX);
        console.log('目標(biāo)所在位置Y是'+pointDY);
         if(pointDX > 0 && pointDY > 0){ 
         wx.showToast({title:'歡迎進(jìn)入25樓craft', duration:4000 }); 
         }
        that.setData({
          canvasPointX:pointDX,
          canvasPointY:pointDY
         })
        
        //創(chuàng)建畫布 
        //計(jì)算坐標(biāo)點(diǎn)在規(guī)定canvas上的位置顯示
        var context = wx.createCanvasContext(); 
        context.setStrokeStyle("#00ff00");
        //a,b,c,d,e,f  a,b起始坐標(biāo),c半徑,d,e起始和終止角度 
        context.arc(that.data.canvasPointX*30,that.data.canvasPointY*30,5,0,2*Math.PI);
        context.fill();
        wx.drawCanvas({
         canvasId: 'firstCanvas',
         actions: context.getActions() // 獲取繪圖動(dòng)作數(shù)組
        }) 
 
        wx.showModal({title:'X軸:'+pointDX+'Y軸:'+pointDY, duration:5000 }); 
         //開(kāi)始網(wǎng)絡(luò)請(qǐng)求
         wx.request({
          url: 'http://craftww.cn/weixinIbeacon/index.php',
          data: {},
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 設(shè)置請(qǐng)求的 header
          success: function(res){
           // success
          },
          fail: function(res) {
           // fail
          },
          complete: function(res) {
           // complete
          }
         })
 
        }else{
        wx.showToast({title:'正在搜索...', duration:1000 })
         開(kāi)始網(wǎng)絡(luò)請(qǐng)求
         wx.request({
          url: 'http://craftww.cn/weixinIbeacon/index.php',
          data: {},
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 設(shè)置請(qǐng)求的 header
          success: function(res){
           // success
          },
          fail: function(res) {
           // fail
          },
          complete: function(res) {
           // complete
          }
         })
        } 
 
     },
     fail: function(res) {
      // fail
     },
     complete: function(res) {
      // complete
     }
    }) 
  } ,
  //動(dòng)態(tài)顯示
  createList: function(thisName){
   var that = this 
     that.setData({
      array:[{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"}]
     }) 
  },
  reStartSearchBLE: function(){ 
   var that = this
  //清除本地?cái)?shù)據(jù)緩存
  wx.stopBluetoothDevicesDiscovery({
   success: function(res){
    // success
   },
   fail: function(res) {
    // fail
   },
   complete: function(res) {
    // complete
   }
  })
   wx.clearStorageSync();
   clearInterval(that.data.showPosition); 
   console.log(that.data.showPosition);
  // that.context.clearRect(0,0,canvas.width,canvas.height);
   //斷開(kāi)藍(lán)牙
   var that = this
   wx.closeBluetoothAdapter({
    success: function(res){
     // success
     console.log('重置成功');
     //打開(kāi)藍(lán)牙適配器,如果沒(méi)有打開(kāi) showtoast
   wx.openBluetoothAdapter({
    success: function(res){
     // success
     //獲取本機(jī)的藍(lán)牙適配器狀態(tài)
     wx.getBluetoothAdapterState({
      success: function(res){
       // success 
     
      },
      fail: function(res) {
       // fail 
      },
      complete: function(res) {
       // complete
      }
     })
    },
    fail: function(res) {
        },
    complete: function(res) {
     // complete
    }
   })
    },
    fail: function(res) {
     // fail
    },
    complete: function(res) {
     // complete
    }
   })
  }
  //zheli
})

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論