基于ibeacon蓝牙定位(微信小程序)
前段時(shí)間做定位,查了很多資料,最后感覺ibeacon做定位比較簡單,主要微信小程序有對應(yīng)的接口所以非常簡單。需要的硬件ibeacon,這個(gè)硬件在淘寶都有,簡單的定位一個(gè)就可以,復(fù)雜就需要根據(jù)需求購買了。
ibeacon主要有三個(gè)參數(shù) UUID(universally unique idenTIfier)、Major、Minor。下面我以商場舉一個(gè)例子,帶大家理解ibeacon的主要三個(gè)參數(shù)。
假設(shè)現(xiàn)在有一個(gè)商場,uuid就是代表你在哪個(gè)商場,major就代表你在幾層樓,minor就是代表你在哪個(gè)商鋪里邊。uuid一般需要申請,但是你買回來的時(shí)候一般就有,其它的參數(shù)自己設(shè)置就可以,ibeacon參數(shù)的設(shè)置會在對應(yīng)的app設(shè)置就可以。
下面附代碼
開始打卡和結(jié)束打卡倆部分,以及向后臺發(fā)起請求,把打卡的數(shù)據(jù)傳到后臺,放進(jìn)數(shù)據(jù)庫:
// pages/scan/scan.js//按鈕打卡頁面var seconds = 10
var ing //定時(shí)器
Page({data: {encrypt: '',wxName: '',avatar: '',btnStr: '打卡',touchBled: false,},toList(){wx.startBeaconDiscovery({uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],success: function () {// wx.showToast({// title: '正在連接設(shè)備',// icon: 'success',// duration: 500// })console.log("開始掃描設(shè)備")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//打卡開始的時(shí)間var time=wx.getStorageSync("time")console.log(time+etime)wx.showToast({title: '打卡成功',icon: 'success',duration: 1500})//在打卡結(jié)束時(shí),將數(shù)據(jù)傳到后臺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("停止掃描設(shè)備!");}});}, 1* 1000); },toClock(){wx.startBeaconDiscovery({uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],success:function(){// wx.showToast({// title: '正在連接設(shè)備',// icon: 'success',// duration: 500// })console.log("開始掃描設(shè)備")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("開始打卡的時(shí)間"+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})}})// 超時(shí)停止掃描 setTimeout(function () {wx.stopBeaconDiscovery({success: function () {console.log("停止掃描設(shè)備!");}});}, 1 * 1000); },onLoad: function (options) {},onReady: function () {// 頁面渲染完成// this.getLocation()// this.getUserInfo()},onShow: function () {// 頁面顯示this.startTime()},onHide: function () {// 頁面隱藏},onUnload: function () {// 頁面關(guān)閉},
})
實(shí)現(xiàn)的效果可以掃描關(guān)注下方公眾號,在公眾號點(diǎn)擊其它,找到其它,點(diǎn)擊其它,里邊有項(xiàng)目代碼和實(shí)現(xiàn)效果的視頻。有需要觀看效果及源碼關(guān)注公眾號獲取。
總結(jié)
以上是生活随笔為你收集整理的基于ibeacon蓝牙定位(微信小程序)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ccf高速公路
- 下一篇: 苹果手机设置时间怎么设置