基于ibeacon蓝牙定位(微信小程序)
生活随笔
收集整理的這篇文章主要介紹了
基于ibeacon蓝牙定位(微信小程序)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前段時間做定位,查了很多資料,最后感覺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 + 1var 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 + 1var day = today.getDate();var h = today.getHours();var m = today.getMinutes();var time=year+"-"+month+"-"+day+" "+h+":"+mwx.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 () {// 頁面關閉},
})
實現的效果可以掃描關注下方公眾號,在公眾號點擊其它,找到其它,點擊其它,里邊有項目代碼和實現效果的視頻。有需要觀看效果及源碼關注公眾號獲取。
總結
以上是生活随笔為你收集整理的基于ibeacon蓝牙定位(微信小程序)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ccf高速公路
- 下一篇: 苹果手机设置时间怎么设置