小言_互联网的博客

基于ibeacon蓝牙定位(微信小程序)

333人阅读  评论(0)

前段时间做定位,查了很多资料,最后感觉ibeacon做定位比较简单,主要微信小程序有对应的接口所以非常简单。需要的硬件ibeacon,这个硬件在淘宝都有,简单的定位一个就可以,复杂就需要根据需求购买了。
ibeacon主要有三个参数 UUID(universally unique idenTIfier)、Major、Minor。下面我以商场举一个例子,带大家理解ibeacon的主要三个参数。
假设现在有一个商场,uuid就是代表你在哪个商场,major就代表你在几层楼,minor就是代表你在哪个商铺里边。uuid一般需要申请,但是你买回来的时候一般就有,其它的参数自己设置就可以,ibeacon参数的设置会在对应的app设置就可以。
下面附代码
开始打卡和结束打卡俩部分,以及向后台发起请求,把打卡的数据传到后台,放进数据库:

// pages/scan/scan.js

//按钮打卡页面


var seconds = 10
var ing //定时器
Page({
  data: {
    encrypt: '',
    wxName: '',
    avatar: '',
    btnStr: '打卡',
    touchBled: false,
  },
  toList(){
    wx.startBeaconDiscovery({
      uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],
      success: function () {
        // wx.showToast({
        //   title: '正在连接设备',
        //   icon: 'success',
        //   duration: 500
        // })
        console.log("开始扫描设备")
        wx.onBeaconUpdate(function (res) {
          console.log(res)
          console.log(res.beacons.length)
          if (res && res.beacons && res.beacons.length > 0) {
            var stunum=wx.getStorageSync("name")
            var today = new Date();
            var year = today.getFullYear();
            var m1 = today.getMonth();
            var month = m1 + 1
            var day = today.getDate();
            var h = today.getHours();
            var m = today.getMinutes();
            var etime = year + "-" + month + "-" + day + " " + h + ":" + m
            //打卡开始的时间
            var time=wx.getStorageSync("time")
            console.log(time+etime)
            wx.showToast({
              title: '打卡成功',
              icon: 'success',
              duration: 1500
            })
            //在打卡结束时,将数据传到后台
            wx.request({
              url: 'https://www.lined5530.top/lg/scanController/scan',
              data:{
                stunum:stunum,
                time:time,
                etime:etime
              }
            })
            console.log(res.beacons[0].uuid)
          }
          // if (res && res.beacons && res.beacons.length > 0) {
          //   console.log(res.beacons[0].uuid)
          // } 
        })
      },
      fail:function(){
        wx.showToast({
          title: '打卡失败',
          icon: 'success',
          duration: 500
        })
      }
    }),
      setTimeout(function () {
        wx.stopBeaconDiscovery({
          success: function () {
            console.log("停止扫描设备!");
          }
        });
      }, 1* 1000); 

  },

  toClock(){
    wx.startBeaconDiscovery({
      uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],
      success:function(){
        // wx.showToast({
        //   title: '正在连接设备',
        //   icon: 'success',
        //   duration: 500
        // })
        console.log("开始扫描设备")
        wx.onBeaconUpdate(function(res){
          console.log(res)
          console.log(res.beacons.length)
          if (res && res.beacons && res.beacons.length > 0) { 
            var today = new Date();
            var year=today.getFullYear();
            var m1 = today.getMonth();
            var month = m1 + 1
            var day = today.getDate();
            var h = today.getHours();
            var m = today.getMinutes();
            var time=year+"-"+month+"-"+day+" "+h+":"+m
            wx.setStorageSync("time", time)
            console.log("开始打卡的时间"+time)
            wx.showToast({
              title: '打卡成功',
              icon:'success',
              duration:1500
            })
            console.log(res.beacons[0].uuid)
          } 
          // if (res && res.beacons && res.beacons.length > 0) {
          //   console.log(res.beacons[0].uuid)
          // } 
        })
      },
      fail: function () {
        wx.showToast({
          title: '打卡失败',
          icon: 'success',
          duration: 500
        })
      }
    })
    // 超时停止扫描  
    setTimeout(function () {
      wx.stopBeaconDiscovery({
        success: function () {
          console.log("停止扫描设备!");
        }
      });
    }, 1 * 1000); 
  },
  onLoad: function (options) {
  },
  onReady: function () {
    // 页面渲染完成
    // this.getLocation()
    // this.getUserInfo()
  },
  onShow: function () {
    // 页面显示
    this.startTime()
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
})

承接微信小程序开发、ssm开发、javaweb开发和树莓派开发、毕业设计,有需求可以
+vx 1029179954


转载:https://blog.csdn.net/baidu_38978508/article/details/101559518
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场