小程序ibeacon三点定位

空余时间简单写了一个微信小程序 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 3){
               //根据arrayRSSI进行信号强弱排序.距离越远rssi值越小
                for(var i = 0 ; i < arrayRSSI.length ; i ++){
                  for(var j = i+1 ; j< arrayRSSI.length ; j++){
                    if(arrayRSSI[i] 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,微信,小程序,定位,ibeacon)