少尉

嗯。

 

lanya

  1. var app = getApp()
  2.  
    Page({
  3.  
    data: {
  4.  
    motto: 'Hello World',
  5.  
    openBLE:'打开蓝牙设备',
  6.  
    startBLEDiscovery:'初始化蓝牙设备',
  7.  
    startBLEDevices:'目标定位',
  8.  
    reStartSearchBLE:'重置蓝牙',
  9.  
    startSearchBLE:'默认空',
  10.  
    userInfo: {},
  11.  
    deviceId: '',
  12.  
    searchFlag:true,
  13.  
    deviceRSSI:'',
  14.  
    deviceName:'',
  15.  
    deviceId:[],
  16.  
    advertisServiceUUIDs: [],
  17.  
    advertisData:[],
  18.  
    canvasPointX:'',
  19.  
    canvasPointY:'',
  20.  
    avatarUrl:'../index/bg-image.jpg',
  21.  
    showPosition:''
  22.  
    },
  23.  
    //事件处理函数
  24.  
    bindViewTap: function() {
  25.  
    wx.navigateTo({
  26.  
    url: '../logs/logs'
  27.  
    })
  28.  
    },
  29.  
    onLoad: function () {
  30.  
    var that = this
  31.  
    //调用应用实例的方法获取全局数据
  32.  
    //that.setData({ deviceId: opt.deviceId })
  33.  
    app.getUserInfo(function(userInfo){
  34.  
    //更新数据
  35.  
    that.setData({
  36.  
    userInfo:userInfo
  37.  
    })
  38.  
    //判断兼容性
  39.  
    if (wx.openBluetoothAdapter) {
  40.  
    //打开蓝牙适配器,如果没有打开 showtoast
  41.  
    wx.openBluetoothAdapter({
  42.  
    success: function(res){
  43.  
    // success
  44.  
    //获取本机的蓝牙适配器状态
  45.  
    wx.getBluetoothAdapterState({
  46.  
    success: function(res){
  47.  
    // success
  48.  
    that.setData({
  49.  
    searchFlag:true
  50.  
    })
  51.  
    },
  52.  
    fail: function(res) {
  53.  
    // fail
  54.  
    that.setData({
  55.  
    searchFlag:false
  56.  
    })
  57.  
    },
  58.  
    complete: function(res) {
  59.  
    // complete
  60.  
    }
  61.  
    })
  62.  
    },
  63.  
    fail: function(res) {
  64.  
    // fail 本机是否已经打开蓝牙设备
  65.  
    wx.showToast({title:'请打开本机蓝牙设备,重新扫码', duration:10000 })
  66.  
    },
  67.  
    complete: function(res) {
  68.  
    // complete
  69.  
    }
  70.  
    })
  71.  
    } else {
  72.  
    // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  73.  
    wx.showModal({
  74.  
    title: '提示',
  75.  
    content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  76.  
    })
  77.  
    }
  78.  
     
  79.  
    })
  80.  
    },
  81.  
     
  82.  
    startBLEDevices: function(){
  83.  
    var that = this
  84.  
    wx.startBluetoothDevicesDiscovery({
  85.  
    services: [],
  86.  
    success: function(res){
  87.  
    //获取本机蓝牙设备状态
  88.  
    // success
  89.  
    that.setData({
  90.  
    showPosition:setInterval(that.devicesFunc,1000)
  91.  
    })
  92.  
    },
  93.  
    fail: function(res) {
  94.  
    // fail
  95.  
    },
  96.  
    complete: function(res) {
  97.  
    // complete
  98.  
    }
  99.  
    })
  100.  
    },
  101.  
    // startBLEDevices: function(){
  102.  
    // var that = this
  103.  
    // that.setData({
  104.  
    // showPosition:setInterval(that.devicesFunc,1000)
  105.  
    // })
  106.  
    // },
  107.  
    devicesFunc: function(){
  108.  
    var that = this
  109.  
     
  110.  
    wx.getBluetoothDevices({
  111.  
    success: function(res){
  112.  
    // console.log(res);
  113.  
    var arrayRSSI = new Array();
  114.  
    var arraydeviceName = new Array();
  115.  
    var arraydeviceId = new Array();
  116.  
    // var arrayUUIDs = new Array();
  117.  
    var arrayadvertisData = new Array();
  118.  
    var pointADistance = '';
  119.  
    var pointBDistance = '';
  120.  
    var pointCDistance = '';
  121.  
    for(var i = 0; i<res.devices.length;i++){
  122.  
    //console.log(res.devices[i].name);
  123.  
    if(res.devices[i].name.indexOf('craft')==0){
  124.  
    //console.log(res.devices[i]);
  125.  
    arrayRSSI.push(res.devices[i].RSSI);
  126.  
    arraydeviceName.push(res.devices[i].name);
  127.  
    arraydeviceId[i]= res.devices[i].deviceId;
  128.  
    arrayUUIDs[i]= res.devices[i].advertisServiceUUIDs[i];
  129.  
    arrayadvertisData[i]= res.devices[i].advertisData ;
  130.  
    调用计算rssi对应距离的函数
  131.  
    var iRssi = Math.abs(arrayRSSI[i]);
  132.  
    var power = (iRssi-59)/(10*2.0);
  133.  
    var mm = Math.pow(10, power);
  134.  
    console.log(arraydeviceName[i]+"距离的位置是"+mm+"米");
  135.  
    01,02,03分别为,(2,0),(2,2),(0,2)固定坐标点,做定位
  136.  
     
  137.  
    if(res.devices[i].name.indexOf('craft01')==0){
  138.  
     
  139.  
    var pointARSSi = res.devices[i].RSSI ;
  140.  
    var iRssi = Math.abs(pointARSSi);
  141.  
    var power = (iRssi-55)/(10*2.0);
  142.  
    var pointADistance = Math.pow(10, power);
  143.  
    console.log("a"+pointADistance);
  144.  
    console.log(pointARSSi);
  145.  
     
  146.  
    }
  147.  
    if(res.devices[i].name.indexOf('craft02')==0){
  148.  
     
  149.  
    var pointBRSSi = res.devices[i].RSSI;
  150.  
    var iRssi = Math.abs(pointBRSSi);
  151.  
    var power = (iRssi-55)/(10*2.0);
  152.  
    var pointBDistance = Math.pow(10, power);
  153.  
    console.log("b"+pointBDistance);
  154.  
    console.log(pointBRSSi);
  155.  
     
  156.  
    }
  157.  
    if(res.devices[i].name.indexOf('craft03')==0){
  158.  
     
  159.  
    var pointCRSSi = res.devices[i].RSSI;
  160.  
    var iRssi = Math.abs(pointCRSSi);
  161.  
    var power = (iRssi-57)/(10*2.0);
  162.  
    var pointCDistance = Math.pow(10, power);
  163.  
    console.log("c"+pointCDistance);
  164.  
    console.log(pointCRSSi);
  165.  
    }
  166.  
    }
  167.  
    }
  168.  
    // 从 arrayRSSI 取三个距离定位点最近的ibeacon参与定位
  169.  
    if(arrayRSSI.length > 3){
  170.  
    //根据arrayRSSI进行信号强弱排序.距离越远rssi值越小
  171.  
    for(var i = 0 ; i < arrayRSSI.length ; i ++){
  172.  
    for(var j = i+1 ; j< arrayRSSI.length ; j++){
  173.  
    if(arrayRSSI[i]<arrayRSSI[j]){
  174.  
    var select = arrayRSSI[i];
  175.  
    arrayRSSI[i] = arrayRSSI[j];
  176.  
    arrayRSSI[j] = select;
  177.  
    }
  178.  
    }
  179.  
    }
  180.  
    //获取最近的三个距离
  181.  
    for(var i = 0 ; i < 3; i++){
  182.  
    if(i==0){
  183.  
    var pointARSSi = res.devices[i].RSSI ;
  184.  
    var iRssi = Math.abs(pointARSSi);
  185.  
    var power = (iRssi-55)/(10*2.0);
  186.  
    var pointADistance = Math.pow(10, power);
  187.  
    console.log("a"+pointADistance);
  188.  
    console.log(pointARSSi);
  189.  
    }
  190.  
    if(i==1){
  191.  
     
  192.  
    var pointBRSSi = res.devices[i].RSSI;
  193.  
    var iRssi = Math.abs(pointBRSSi);
  194.  
    var power = (iRssi-55)/(10*2.0);
  195.  
    var pointBDistance = Math.pow(10, power);
  196.  
    console.log("b"+pointBDistance);
  197.  
    console.log(pointBRSSi);
  198.  
    }
  199.  
    if(i==2){
  200.  
    var pointCRSSi = res.devices[i].RSSI;
  201.  
    var iRssi = Math.abs(pointCRSSi);
  202.  
    var power = (iRssi-57)/(10*2.0);
  203.  
    var pointCDistance = Math.pow(10, power);
  204.  
    console.log("c"+pointCDistance);
  205.  
    console.log(pointCRSSi);
  206.  
    }
  207.  
    }
  208.  
    }
  209.  
    //获取定位点的x和y
  210.  
    if(!pointADistance==''&&!pointBDistance==''&&!pointCDistance==''){
  211.  
    var pointDX='';var pointDY = '';
  212.  
    var p = Math.pow(pointADistance,2)/10-Math.pow(pointBDistance,2)/10;
  213.  
    pointDX = 2.5 - p;
  214.  
     
  215.  
    var m = Math.pow(pointADistance,2)/10-Math.pow(pointCDistance,2)/10;
  216.  
    pointDY = 2.5 - m;
  217.  
     
  218.  
    console.log('目标所在位置X是'+pointDX);
  219.  
    console.log('目标所在位置Y是'+pointDY);
  220.  
    if(pointDX > 0 && pointDY > 0){
  221.  
    wx.showToast({title:'欢迎进入25楼craft', duration:4000 });
  222.  
    }
  223.  
    that.setData({
  224.  
    canvasPointX:pointDX,
  225.  
    canvasPointY:pointDY
  226.  
    })
  227.  
     
  228.  
    //创建画布
  229.  
    //计算坐标点在规定canvas上的位置显示
  230.  
    var context = wx.createCanvasContext();
  231.  
    context.setStrokeStyle("#00ff00");
  232.  
    //a,b,c,d,e,f a,b起始坐标,c半径,d,e起始和终止角度
  233.  
    context.arc(that.data.canvasPointX*30,that.data.canvasPointY*30,5,0,2*Math.PI);
  234.  
    context.fill();
  235.  
    wx.drawCanvas({
  236.  
    canvasId: 'firstCanvas',
  237.  
    actions: context.getActions() // 获取绘图动作数组
  238.  
    })
  239.  
     
  240.  
    wx.showModal({title:'X轴:'+pointDX+'Y轴:'+pointDY, duration:5000 });
  241.  
    //开始网络请求
  242.  
    wx.request({
  243.  
    url: 'http://craftww.cn/weixinIbeacon/index.php',
  244.  
    data: {},
  245.  
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  246.  
    // header: {}, // 设置请求的 header
  247.  
    success: function(res){
  248.  
    // success
  249.  
    },
  250.  
    fail: function(res) {
  251.  
    // fail
  252.  
    },
  253.  
    complete: function(res) {
  254.  
    // complete
  255.  
    }
  256.  
    })
  257.  
     
  258.  
    }else{
  259.  
    wx.showToast({title:'正在搜索...', duration:1000 })
  260.  
    开始网络请求
  261.  
    wx.request({
  262.  
    url: 'http://craftww.cn/weixinIbeacon/index.php',
  263.  
    data: {},
  264.  
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  265.  
    // header: {}, // 设置请求的 header
  266.  
    success: function(res){
  267.  
    // success
  268.  
    },
  269.  
    fail: function(res) {
  270.  
    // fail
  271.  
    },
  272.  
    complete: function(res) {
  273.  
    // complete
  274.  
    }
  275.  
    })
  276.  
    }
  277.  
     
  278.  
    },
  279.  
    fail: function(res) {
  280.  
    // fail
  281.  
    },
  282.  
    complete: function(res) {
  283.  
    // complete
  284.  
    }
  285.  
    })
  286.  
    } ,
  287.  
    //动态显示
  288.  
    createList: function(thisName){
  289.  
    var that = this
  290.  
    that.setData({
  291.  
    array:[{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"},{deviceDistance:"1"}]
  292.  
    })
  293.  
    },
  294.  
    reStartSearchBLE: function(){
  295.  
    var that = this
  296.  
    //清除本地数据缓存
  297.  
    wx.stopBluetoothDevicesDiscovery({
  298.  
    success: function(res){
  299.  
    // success
  300.  
    },
  301.  
    fail: function(res) {
  302.  
    // fail
  303.  
    },
  304.  
    complete: function(res) {
  305.  
    // complete
  306.  
    }
  307.  
    })
  308.  
    wx.clearStorageSync();
  309.  
    clearInterval(that.data.showPosition);
  310.  
    console.log(that.data.showPosition);
  311.  
    // that.context.clearRect(0,0,canvas.width,canvas.height);
  312.  
    //断开蓝牙
  313.  
    var that = this
  314.  
    wx.closeBluetoothAdapter({
  315.  
    success: function(res){
  316.  
    // success
  317.  
    console.log('重置成功');
  318.  
    //打开蓝牙适配器,如果没有打开 showtoast
  319.  
    wx.openBluetoothAdapter({
  320.  
    success: function(res){
  321.  
    // success
  322.  
    //获取本机的蓝牙适配器状态
  323.  
    wx.getBluetoothAdapterState({
  324.  
    success: function(res){
  325.  
    // success
  326.  
     
  327.  
    },
  328.  
    fail: function(res) {
  329.  
    // fail
  330.  
    },
  331.  
    complete: function(res) {
  332.  
    // complete
  333.  
    }
  334.  
    })
  335.  
    },
  336.  
    fail: function(res) {
  337.  
    },
  338.  
    complete: function(res) {
  339.  
    // complete
  340.  
    }
  341.  
    })
  342.  
    },
  343.  
    fail: function(res) {
  344.  
    // fail
  345.  
    },
  346.  
    complete: function(res) {
  347.  
    // complete
  348.  
    }
  349.  
    })
  350.  
    }
  351.  
    //zheli
  352.  
    })
  353.  
     
  354.  

posted on 2018-08-10 10:04  moonvan  阅读(696)  评论(0编辑  收藏  举报

导航