搜索蓝牙匹配

搜索蓝牙功能很常见了,今天我们就上代码看一看。

  1. 初始化蓝牙状态
//初始化蓝牙状态
  onShow() {
     
    wx.openBluetoothAdapter({
     
      success(res) {
     
        console.log(res)
      },
      fail(err) {
     
        console.log(err);
      }
    })
  },
  1. 获取设备id
//获取设备id
    wx.request({
     
      url: 'https://travel.qhynice.top/api/device/index',
      data: {
     
        id: 1
      },
      success: (res) => {
     
        console.log(res)
        this.setData({
     
          data: res.data.data,
          explain: res.data.explain
        })
      }
    })
  1. 获取uuid进行比较还有一系列的验证是否正确,这边我是用了一个点击事件来触发这个效果
tton: function () {
     
    var arrr = []
    var arrb = []
    var _this = this
    for (var i = 0; i < _this.data.data.length; i++) {
     
      arrr.push(_this.data.data[i].deviceid)
      arrb.push(_this.data.data[i].placeid)
      try {
     
        wx.setStorageSync('keya', arrr)
      } catch (e) {
     }
      try {
     
        wx.setStorageSync('keyb', arrb)
      } catch (e) {
     }
      wx.startBeaconDiscovery({
     
        uuids: [_this.data.data[i].deviceid],
        success(res) {
     
          console.log('startIbeacon', res);
          wx.onBeaconUpdate((arr) => {
     
            console.log('onBeaconUpdate...', arr);
            wx.offBeaconUpdate((res) => {
     })
            var a = arr.beacons[0].uuid;
            a = a.toUpperCase();
            var arrc = ""
            try {
     
              var value = wx.getStorageSync('keya')
              arrc = value
            } catch (e) {
     
              // Do something when catch error
            }
            try {
     
              var valu = wx.getStorageSync('keyb')
              for (var j = 0; j < arrc.length; j++) {
     
                if (a = arrc[j]) {
     
                  _this.setData({
     
                    scenic_id: valu[j],
                    deviceid: _this.data.data[j].id
                  })
                  wx.request({
     
                    url: 'https://travel.qhynice.top/api/device/detaile',
                    data: {
     
                      placeid: valu[j],
                      scienceid: this.data.idid
                    },
                    success: (res) => {
     
                      console.log(res);
                      _this.setData({
     
                        science: res.data.science,
                        role: res.data.role
                      })
                    }
                  })
                }
              }
            } catch (e) {
     
              // Do something when catch error
            }
          });
          wx.getBeacons({
     
            success(getinfo) {
     
              console.log('getBeacons success', getinfo);
            },
          });
        },
        fail(res) {
     
          console.log('fail', res);
          wx.showToast({
     
            title: '请打开蓝牙', //弹框内容
            icon: 'success', //弹框模式
            duration: 2000 //弹框显示时间
          })
        },
        complete(res) {
     
          console.log('complete', res);
        },
      });
    }
  },

你可能感兴趣的:(蓝牙,蓝牙)