微信小程序ibeacon三点定位详解


Posted in Javascript onOctober 31, 2018

空余时间简单写了一个微信小程序ibeacon三点定位。

事先淘宝买了七八个ibeacon小设备,放置在办公司角落。分别设置三个ibeacon的位置坐标点,根据每一个ibeacon到已经开启蓝牙的目标物距离,计算出目标物在当前区域内坐标位置。适用于区域内购物指示。当然,进入该区域事先要打开手机蓝牙。

下面代码:

var app = getApp() 
Page({
 data: {
  motto: 'Hello World',
  openBLE:'打开蓝牙设备',
  startBLEDiscovery:'初始化蓝牙设备',
  startBLEDevices:'目标定位',
  reStartSearchBLE:'重置蓝牙',
  startSearchBLE:'默认空',
  userInfo: {},
  deviceId: '',
  searchFlag:true,
  deviceRSSI:'',
  deviceName:'',
  deviceId:[],
  advertisServiceUUIDs: [],
  advertisData:[],
  canvasPointX:'',
  canvasPointY:'',
  avatarUrl:'../index/bg-image.jpg',
  showPosition:'' 
 },
 //事件处理函数
 bindViewTap: function() {
  wx.navigateTo({
   url: '../logs/logs'
  })
 },
 onLoad: function () { 
  var that = this
  //调用应用实例的方法获取全局数据 
  //that.setData({ deviceId: opt.deviceId }) 
  app.getUserInfo(function(userInfo){
   //更新数据 
   that.setData({
    userInfo:userInfo
   })
   //判断兼容性
   if (wx.openBluetoothAdapter) { 
   //打开蓝牙适配器,如果没有打开 showtoast
   wx.openBluetoothAdapter({
    success: function(res){
     // success
     //获取本机的蓝牙适配器状态
     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 本机是否已经打开蓝牙设备
      wx.showToast({title:'请打开本机蓝牙设备,重新扫码', duration:10000 })
    },
    complete: function(res) {
     // complete
    }
   })
   } else {
     // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
     wx.showModal({
      title: '提示',
      content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
     })
    } 
    
  })
  }, 
 
  startBLEDevices: function(){
    var that = this 
     wx.startBluetoothDevicesDiscovery({
      services: [],
      success: function(res){ 
       //获取本机蓝牙设备状态  
         // 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  ;
        调用计算rssi对应距离的函数
        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)固定坐标点,做定位
        
        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 取三个距离定位点最近的ibeacon参与定位
      if(arrayRSSI.length > 3){
        //根据arrayRSSI进行信号强弱排序.距离越远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;
          }
         } 
        }
        //获取最近的三个距离
        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); 
          }  
        } 
      } 
        //获取定位点的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('目标所在位置X是'+pointDX);
        console.log('目标所在位置Y是'+pointDY);
         if(pointDX > 0 && pointDY > 0){ 
         wx.showToast({title:'欢迎进入25楼craft', duration:4000 }); 
         }
        that.setData({
          canvasPointX:pointDX,
          canvasPointY:pointDY
         })
        
        //创建画布 
        //计算坐标点在规定canvas上的位置显示
        var context = wx.createCanvasContext(); 
        context.setStrokeStyle("#00ff00");
        //a,b,c,d,e,f  a,b起始坐标,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() // 获取绘图动作数组
        }) 
 
        wx.showModal({title:'X轴:'+pointDX+'Y轴:'+pointDY, duration:5000 }); 
         //开始网络请求
         wx.request({
          url: 'http://craftww.cn/weixinIbeacon/index.php',
          data: {},
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
           // success
          },
          fail: function(res) {
           // fail
          },
          complete: function(res) {
           // complete
          }
         })
 
        }else{
        wx.showToast({title:'正在搜索...', duration:1000 })
         开始网络请求
         wx.request({
          url: 'http://craftww.cn/weixinIbeacon/index.php',
          data: {},
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
           // success
          },
          fail: function(res) {
           // fail
          },
          complete: function(res) {
           // complete
          }
         })
        } 
 
     },
     fail: function(res) {
      // fail
     },
     complete: function(res) {
      // complete
     }
    }) 
  } ,
  //动态显示
  createList: function(thisName){
   var that = this 
     that.setData({
      array:[{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"}]
     }) 
  },
  reStartSearchBLE: function(){ 
   var that = this
  //清除本地数据缓存
  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);
   //断开蓝牙
   var that = this
   wx.closeBluetoothAdapter({
    success: function(res){
     // success
     console.log('重置成功');
     //打开蓝牙适配器,如果没有打开 showtoast
   wx.openBluetoothAdapter({
    success: function(res){
     // success
     //获取本机的蓝牙适配器状态
     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
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript的Cookies
Jan 16 Javascript
通用JS事件写法实现代码
Jan 07 Javascript
Json对象替换字符串占位符实现代码
Nov 17 Javascript
javascript 进阶篇3 Ajax 、JSON、 Prototype介绍
Mar 14 Javascript
javascript实现的平方米、亩、公顷单位换算小程序
Aug 11 Javascript
jquery attr()设置和获取属性值实例教程
Sep 25 Javascript
拖动时防止选中
Feb 03 Javascript
vue之将echart封装为组件
Jun 02 Javascript
在create-react-app中使用css modules的示例代码
Jul 31 Javascript
微信小程序实现自定义底部导航
Nov 18 Javascript
Vue3+elementui plus创建项目的方法
Dec 01 Vue.js
js+for循环实现字符串自动转义的代码(把后面的字符替换前面的字符)
Dec 24 Javascript
小程序获取周围IBeacon设备的方法
Oct 31 #Javascript
详解js删除数组中的指定元素
Oct 31 #Javascript
Vue中使用方法、计算属性或观察者的方法实例详解
Oct 31 #Javascript
Vue项目History模式404问题解决方法
Oct 31 #Javascript
Vue.js中对css的操作(修改)具体方式详解
Oct 30 #Javascript
vue组件中的样式属性scoped实例详解
Oct 30 #Javascript
node上的redis调用优化示例详解
Oct 30 #Javascript
You might like
php 空格,换行,跳格使用说明
2009/12/18 PHP
PHPCMS的使用小结
2010/09/20 PHP
PHP实现设计模式中的抽象工厂模式详解
2014/10/11 PHP
PHP实现的下载远程图片自定义函数分享
2015/01/28 PHP
PHP5.4起内置web服务器使用方法
2016/08/09 PHP
php类的自动加载操作实例详解
2016/09/28 PHP
实例介绍PHP删除数组中的重复元素
2019/03/03 PHP
一些相见恨晚的 JavaScript 技巧
2010/04/25 Javascript
js下将字符串当函数执行的方法
2011/07/13 Javascript
jQuery选择器简明总结(含用法实例,一目了然)
2014/04/25 Javascript
js验证IP及子网掩码的合法性有效性示例
2014/04/30 Javascript
对比分析AngularJS中的$http.post与jQuery.post的区别
2015/02/27 Javascript
JQuery插入DOM节点的方法
2015/06/11 Javascript
JavaScript实现时间倒计时跳转(推荐)
2016/06/28 Javascript
js Canvas绘制圆形时钟教程
2017/02/06 Javascript
浅谈react.js 之 批量添加与删除功能
2017/04/17 Javascript
AngularJS ng-repeat指令及Ajax的应用实例分析
2017/07/06 Javascript
微信小程序页面生命周期详解
2018/01/31 Javascript
[01:07:21]NAVI vs VG Supermajor 败者组 BO3 第二场 6.5
2018/06/06 DOTA
python 文件与目录操作
2008/12/24 Python
python实现从字典中删除元素的方法
2015/05/04 Python
Python中shutil模块的常用文件操作函数用法示例
2016/07/05 Python
Python实现公历(阳历)转农历(阴历)的方法示例
2017/08/22 Python
Python tkinter实现的图片移动碰撞动画效果【附源码下载】
2018/01/04 Python
高效使用Python字典的清单
2018/04/04 Python
对python numpy.array插入一行或一列的方法详解
2019/01/29 Python
python使用wxpy实现微信消息防撤回脚本
2019/04/29 Python
python-图片流传输的思路及示例(url转换二维码)
2020/12/21 Python
澳大利亚优质葡萄酒专家:Vintage Cellars
2019/01/08 全球购物
中国专业的音频分享平台:喜马拉雅
2019/05/24 全球购物
德国前卫设计师时装在线商店:Luxury Loft
2019/11/04 全球购物
薇姿法国官网:Vichy法国
2021/01/28 全球购物
农场厂长岗位职责
2013/12/28 职场文书
教师师德反思材料
2014/02/15 职场文书
2014年教师节国旗下讲话稿
2014/09/10 职场文书
2015年党员创先争优承诺书
2015/01/22 职场文书