小程序獲取周圍IBeacon設(shè)備的方法
本文實(shí)例為大家分享了小程序獲取周圍IBeacon設(shè)備的具體代碼,供大家參考,具體內(nèi)容如下
該功能實(shí)現(xiàn)需要使用以下API:
wx.startBeaconDiscovery(OBJECT):開始搜索附近的iBeacon設(shè)備
wx.stopBeaconDiscovery(OBJECT):停止搜索附近的iBeacon設(shè)備
wx.onBeaconUpdate(CALLBACK):監(jiān)聽 iBeacon 設(shè)備的更新事件
wx.openBluetoothAdapter(OBJECT):監(jiān)聽藍(lán)牙狀態(tài)
wx.onBluetoothDeviceFound(CALLBACK):監(jiān)聽藍(lán)牙狀態(tài)切換
具體參數(shù)以及回調(diào)函數(shù)請參考官方API
實(shí)現(xiàn)邏輯:
實(shí)現(xiàn)代碼 index.js:
onShow : function(){ var that = this; //監(jiān)測藍(lán)牙狀態(tài)的改變 wx.onBluetoothAdapterStateChange(function (res) { if (res.available) {//如果用戶打開藍(lán)牙,開始搜索IBeacon searchBeacon(); } }) //搜索beacons searchBeacon(); //搜索函數(shù) function searchBeacon() { //檢測藍(lán)牙狀態(tài) wx.openBluetoothAdapter({ success: function (res) {//藍(lán)牙狀態(tài):打開 wx.startBeaconDiscovery({//開始搜索附近的iBeacon設(shè)備 uuids: ['FDA50693-A4E2-4FB1-AFCF-C6EB07647825'],//參數(shù)uuid success: function (res) { wx.onBeaconUpdate(function (res) {//監(jiān)聽 iBeacon 設(shè)備的更新事件 //封裝請求數(shù)據(jù) var beacons = res.beacons; var reqContent = {}; var bleArray = []; for (var i = 0; i < beacons.length; i++) { var bleObj = {}; bleObj.distance = beacons[i].accuracy; bleObj.rssi = beacons[i].rssi; bleObj.mac = beacons[i].major + ":" + beacons[i].minor; bleArray.push(bleObj); } reqContent.ble = bleArray; //請求后臺向redis插入數(shù)據(jù) redisSave(reqContent); }); }, fail: function (res) { //先關(guān)閉搜索再重新開啟搜索,這一步操作是防止重復(fù)wx.startBeaconDiscovery導(dǎo)致失敗 stopSearchBeacom(); } }) }, fail: function (res) {//藍(lán)牙狀態(tài):關(guān)閉 wx.showToast({ title: "請打開藍(lán)牙", icon: "none", duration: 2000 }) } }) } function redisSave(reqContent) { wx.request({ url: "https://map.intmote.com/LocateServer/location.action", data: JSON.stringify(reqContent), method: 'POST', header: { 'Content-type': 'application/json' }, success: function (res) { // wx.showToast({ title: "seccess" }) }, fail: function (res) { // wx.showToast({ title: "1" }) } }); } //關(guān)閉成功后開啟搜索 function stopSearchBeacom() { wx.stopBeaconDiscovery({ success: function () { searchBeacon(); } }) } },
介紹小程序的頁面生命周期函數(shù)之一:onShow
監(jiān)聽頁面顯示:即每次打開頁面都會調(diào)用一次。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript+html5+css3自定義彈出窗口效果
這篇文章主要為大家詳細(xì)介紹了javascript+html5+css3自定義彈出窗口效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10通過JavaScript實(shí)現(xiàn)動態(tài)圣誕樹詳解
這篇文章主要為大家介紹幾個好看的基于HTML+CSS+JS的圣誕樹,希望圣誕節(jié)那天圣誕老爺爺能把我喜歡的你塞到我床上。感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12JS實(shí)現(xiàn)多物體運(yùn)動的方法詳解
這篇文章主要介紹了JS實(shí)現(xiàn)多物體運(yùn)動的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了javascript實(shí)現(xiàn)多物體運(yùn)動的原理與相關(guān)操作技巧,需要的朋友可以參考下2018-01-01