[Project Management] Wechat applet uses Beacon device to realize sign-in function

[Project Management] Wechat applet uses Beacon device to realize sign-in function

insert image description here insert image description here

WeChatDevTools : WeChatDevTools Stable 1.06.2303220

WeChat : 8.0.35

Debug base library: 2.32.0

1. Project introduction

iCheckIn is a WeChat applet project. By calling the mobile phone’s Bluetooth function to search for Beacon devices, it realizes the function of course sign-in. I only completed the implementation of the WeChat applet in this project. The process design and prototype design refer to the samples given by our team leader. , redesigned. Here is the link to his GitHub homepage , if you are interested, you can click and follow

2. Process Design

insert image description here

3. Prototyping

Ink knife prototype design link

The following is a screenshot of the prototype design
insert image description here

4. Key code

Here we use the wx.startBeaconDiscovery method to find nearby Beacon devices. uuids is a filter list. We only need to query the Beacon devices corresponding to these uuids. After the search is successful, we can call the Beacon device information for sign-in processing.

  //开始寻找信标设备
  startFindDevices: function () {
    
    
    let that = this;
    timer = setTimeout(function () {
    
    
      that.stopFindDevices('false', "搜索设备超时")
    }, 10000) //搜索超时 停止扫描设备
    wx.startBeaconDiscovery({
    
    
      //设置ibeacons的参数
      //000FFFF-0000-1000-8000-00805F9B34FB,这是一个通用的 UUID,可以用于扫描大多数 Beacon 设备
      uuids: ['0000FFFF-0000-1000-8000-00805F9B34FB', 'FDA50693-A4E2-4FB1-AFCF-C6EB07647801', 'FDA50693-A4E2-4FB1-AFCF-C6EB07647802', 'FDA50693-A4E2-4FB1-AFCF-C6EB07647803', 'FDA50693-A4E2-4FB1-AFCF-C6EB07647804', 'FDA50693-A4E2-4FB1-AFCF-C6EB07647805', 'FDA50693-A4E2-4FB1-AFCF-C6EB07647806'],
      //连接成功
      success: function () {
    
    
        that.doSignIn();
      },
      fail: function (res) {
    
    
        console.log(res);
        if(res.errCode == '11003'){
    
    
          that.doSignIn();//如果已经打开了扫描设备,则直接进行签到
        } else {
    
    
          that.stopFindDevices('false', res.errMsg); //停止扫描设备,返回结果
        }
      },
    })
  },

Here, the found UUID of the Beacon device and the user's student number are sent to the background for sign-in processing

//进行签到
  doSignIn: function () {
    
    
    let that = this;
    //监听iBeacon设备的更新事件
    wx.onBeaconUpdate(function (res) {
    
    
      wx.getBeacons({
    
    
        success: (result) => {
    
    
          if (res.beacons) {
    
    
            let count = res.beacons.length;
            if (count >= 1) {
    
     //显示正在签到
              clearTimeout(timer); //扫描信标成功后,清除定时器
              if (!hasSignIn) {
    
     //如果没有签到,则进行签到
                hasSignIn = true; //标识已签到
                that.setData({
    
    
                  promptInfo: '正在签到......'
                });
                let selectIndex = 0;
                for (let i = 0; i < count; i++) {
    
    
                  if (Math.abs(res.beacons[i].RSSI) < Math.abs(res.beacons[selectIndex].RSSI)) {
    
    
                    selectIndex = i; //如果RSSI绝对值更小,取更小的这个beacon
                  }
                }
                let ibeacon1 = res.beacons[selectIndex]
                let uuid = ibeacon1.uuid; //uuid
                let rssi = Math.abs(ibeacon1.rssi); //rssi
                let stu_name = wx.getStorageSync('stu_name'); //姓名
                let stu_number = wx.getStorageSync('stu_number'); //学号
                let postHeader = {
    
    
                  'content-type': 'application/json',
                };
                let postData = {
    
    
                  "studentNo": stu_number,
                  "studentName": stu_name,
                  "beacons": [{
    
    
                    "beaconName": uuid,
                    "rssi": rssi
                  }]
                };

                let result = '';
                let message = '';
                wx.request({
    
    
                  url: 'https://sjtu.yangzezhi.com/api/checkin',
                  header: postHeader,
                  method: 'POST',
                  data: JSON.stringify(postData),
                  success(res) {
    
    
                    if (res.data.success) {
    
    
                      result = res.data.success;
                      message = res.data.data.classroom;
                    } else {
    
    
                      if (res.data.message) {
    
    
                        result = res.success;
                        message = res.data.message;
                      } else {
    
    
                        result = false;
                        message = res.data.status + " " + res.data.error;
                      }
                    }
                  },
                  fail(res) {
    
    
                    result = false;
                    message = res.errMsg;
                  },
                  complete() {
    
    
                    that.stopFindDevices(result, message);
                  }
                })
              }
            }
          }
        },
      })
    })
  },

5. Open source address

Giteehttps://gitee.com/lijinjiang01/iCheckIn
Githubhttps://github.com/lijinjiang01/iCheckIn

Guess you like

Origin blog.csdn.net/qq_35132089/article/details/131134523