微信小程序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 相关文章推荐
JS BASE64编码 window.atob(), window.btoa()
Mar 09 Javascript
使用jQuery实现dropdownlist的联动效果(sharepoint 2007)
Mar 30 Javascript
JavaScript控制Session操作方法
Jan 17 Javascript
Js与下拉列表处理问题解决
Feb 13 Javascript
百度UEditor编辑器如何关闭抓取远程图片功能
Mar 03 Javascript
javascript实现网页中涉及的简易运动(改变宽高、透明度、位置)
Nov 29 Javascript
BootStrap.css 在手机端滑动时右侧出现空白的原因及解决办法
Jun 07 Javascript
详解用vue.js和laravel实现微信支付
Jun 23 Javascript
Express之托管静态文件的方法
Jun 01 Javascript
vue2.0获取鼠标位置的方法
Sep 13 Javascript
jquery实现直播视频弹幕效果
Feb 25 jQuery
js前端面试常见浏览器缓存强缓存及协商缓存实例
Jun 21 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
自制短波长线天线频率预选器 - 成功消除B2K之流的镜像
2021/03/02 无线电
PHP+AJAX实现无刷新注册(带用户名实时检测)
2006/12/02 PHP
实用函数5
2007/11/08 PHP
php中通过虚代理实现延迟加载的实现代码
2011/06/10 PHP
php中url函数介绍及使用示例
2014/02/13 PHP
PHP实现数组递归转义的方法
2014/08/28 PHP
php好代码风格的阶段性总结
2016/06/25 PHP
eclipse php wamp配置教程
2016/06/30 PHP
Ubuntu中支持PHP5与PHP7双版本的简单实现
2018/08/19 PHP
php扩展开发入门demo示例
2019/09/23 PHP
YUI 读码日记之 YAHOO.lang.is*
2008/03/22 Javascript
javascript实现json页面分页实例代码
2014/02/20 Javascript
Ubuntu中搭建Nodejs开发环境过程分享
2014/06/01 NodeJs
js的toUpperCase方法用法实例
2015/01/27 Javascript
JavaScript使用位运算符判断奇数和偶数的方法
2015/06/01 Javascript
Bootstrap面板使用方法
2017/01/16 Javascript
javascript计算渐变颜色的实例
2017/09/22 Javascript
Vue.js实现的表格增加删除demo示例
2018/05/22 Javascript
jQuery实现图片上传预览效果功能完整实例【测试可用】
2018/05/28 jQuery
nodejs中request库使用HTTPS代理的方法
2019/04/30 NodeJs
简单了解three.js 着色器材质
2020/08/03 Javascript
[01:08]DOTA2“血战之命”预告片
2017/08/12 DOTA
Python 随机生成中文验证码的实例代码
2013/03/20 Python
全面解读Python Web开发框架Django
2014/06/30 Python
python学习笔记--将python源文件打包成exe文件(pyinstaller)
2018/05/26 Python
使用Python的Dataframe取两列时间值相差一年的所有行方法
2018/07/10 Python
python3.6使用tkinter实现弹跳小球游戏
2019/05/09 Python
在python中计算ssim的方法(与Matlab结果一致)
2019/12/19 Python
pytorch 计算Parameter和FLOP的操作
2021/03/04 Python
Omio俄罗斯:一次搜索公共汽车、火车和飞机的机票
2018/11/17 全球购物
中英双版中文教师求职信
2013/10/27 职场文书
历史系毕业生自荐信
2013/10/28 职场文书
《广玉兰》教学反思
2014/04/14 职场文书
中英文求职信范文
2015/03/19 职场文书
交通安全温馨提示语
2015/07/14 职场文书
postgresql使用filter进行多维度聚合的解决方法
2021/07/16 PostgreSQL