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.  
     

lanya的更多相关文章

  1. PC上面的蓝牙的通信(C#)

    添加引用InTheHand.Net.Personal.dll 首先创建一个蓝牙类 class LanYa { public string blueName { get; set; } //l蓝牙名字 ...

  2. pom.xml文件模板、application文件模板、configuration逆向生成文件、

    pom: <?xml version="1.0" encoding="UTF-8"?><project xmlns="http:// ...

  3. python爬虫入门(2)re模块-正则表达式

    正则表达式 search //匹配第一次遇到符合规则的 匹配IP地址 import re re.search(r'(([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3 ...

  4. PC蓝牙通信C#代码实现

    PC蓝牙通信C#代码实现 这篇文章主要为大家详细介绍了PC蓝牙通信C#代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了C#实现PC蓝牙通信代码,供大家参考,具体内容如下 ...

随机推荐

  1. [转]CPU-bound(计算密集型) 和I/O bound(I/O密集型)

    转自:http://blog.csdn.net/q_l_s/article/details/51538039 I/O密集型 (CPU-bound) I/O bound 指的是系统的CPU效能相对硬盘/ ...

  2. JS:Math 对象方法

    Math 对象方法方法     描述Math.ceil(x)     对数进行上舍入.(向上取整:大于等于x的最小整数)Math.floor(x)     对数进行下舍入.(小于等于x的最大整数)Ma ...

  3. tensorboard OSError:[Errno 22] Invalid argument

    哈哈 问题解决了.感谢大佬 Bill.Z 附上原文链接:https://blog.csdn.net/u013244846/article/details/88380860 解决方法:更改manager ...

  4. GitHub下载单个文件

    1. 点击某个文件. 2. 右键点击RAW. 3. 另存为

  5. sql 查询结果自定义排序

    sqlserver 使用case when then 语句来实现 select name from fruit order by case name end oracle 使用decode实现 ,,, ...

  6. C# 利用Unity 实现IOC+AOP

    public interface INoticy { void Noticy(string msg); } public class SMSNoticy : INoticy { public void ...

  7. 使用svn创建分支!

    1 在主分支上 右键svn---选中 branch/Tag选项 2,填写新分支目录之后 点击 ok键 3,在新创建的分支目录 右键 --> Chenckout下  就可以把代码拉下来了 4.更新 ...

  8. New Journey--工作五年所思所感小记

    正式参加工作满五年了,2013年7月9日走入游戏行业.之前就计划着应该提笔写点什么留下来,但当日子真的到了,竟是不知说些什么了.14年年中的时候我曾写过一篇工作一年的总结,现在回头读起来,还真是别有一 ...

  9. 记一次bond引起的网络故障

    本案中3个关键服务器 物理服务器:192.168.6.63,简称P,(Physical server) KVM-VM:192.168.6.150,是物理服务器P上的一个KVM虚机,简称VM NAS:外 ...

  10. db powerdesign CDM、LDM、PDM、OOM的区别

        导读 在本篇文章中,你将会了解到PowerDesigner工具中的三种模型CDM,OOM,PDM的区别和联系. PowerDesigner 简称PD,是一种数据建模工具,适合于开发大型应用系统 ...