|
@@ -1,15 +1,16 @@
|
|
|
// components/map-sense.js
|
|
|
|
|
|
let innerAudioContext = wx.createInnerAudioContext({
|
|
|
- useWebAudioImplement: true
|
|
|
+ useWebAudioImplement: true,
|
|
|
});
|
|
|
-import http from "../../utils/http";
|
|
|
import { promisify, BeaconUtils } from "../../utils/util";
|
|
|
+
|
|
|
+
|
|
|
let openBluetoothAdapter = promisify(wx.openBluetoothAdapter);
|
|
|
-let closeBluetoothAdapter = promisify(wx.closeBluetoothAdapter);
|
|
|
let startBeaconDiscovery = promisify(wx.startBeaconDiscovery);
|
|
|
let stopBeaconDiscovery = promisify(wx.stopBeaconDiscovery);
|
|
|
|
|
|
+let app = getApp();
|
|
|
|
|
|
import {
|
|
|
CDN_URL,
|
|
@@ -17,6 +18,7 @@ import {
|
|
|
STATUS_TEXT,
|
|
|
API_BASE_URL,
|
|
|
DISTRIBUTION,
|
|
|
+ AudioAddress,
|
|
|
} from "../../config/index";
|
|
|
|
|
|
const STATUS_PIC = {
|
|
@@ -26,12 +28,19 @@ const STATUS_PIC = {
|
|
|
3: "fail",
|
|
|
};
|
|
|
|
|
|
-let audioInterruptionCb = ()=>{}
|
|
|
-let bluetoothAdapterStateChangeCb = ()=>{}
|
|
|
+let audioInterruptionCb = () => {};
|
|
|
+let bluetoothAdapterStateChangeCb = () => {};
|
|
|
+
|
|
|
+let isCollect = false;
|
|
|
+let hadshowModal = false
|
|
|
+
|
|
|
+let checkCollect = null
|
|
|
+let checkTimeout = null
|
|
|
+let updateingBeacon = false
|
|
|
|
|
|
-let isCollect = false
|
|
|
+let updateTO = null
|
|
|
|
|
|
-let changeTO = null
|
|
|
+let changeTO = null;
|
|
|
|
|
|
// 选择6档
|
|
|
const TXPOWER = -55;
|
|
@@ -44,15 +53,13 @@ const R = 3.4; //设定感应范围半径
|
|
|
|
|
|
const RSSIVAL = 70; //rssi信号过滤临界点
|
|
|
|
|
|
-
|
|
|
let AveLength = 10;
|
|
|
|
|
|
let group = [
|
|
|
["10001", "10002"],
|
|
|
["10003"],
|
|
|
["10004"],
|
|
|
- ["10005"],
|
|
|
- ["10006"],
|
|
|
+ ["10005", "10006"],
|
|
|
["10007"],
|
|
|
["10008"],
|
|
|
["10009"],
|
|
@@ -80,30 +87,6 @@ let group = [
|
|
|
// 18:['10020','10021'],19:['10022'],
|
|
|
// 20:['10023'],21:['10024']
|
|
|
|
|
|
-let AudioAddress = {
|
|
|
- 0: "https://culture.4dage.com/demo/audio/1.2.mp3",
|
|
|
- 1: "https://culture.4dage.com/demo/audio/3.mp3",
|
|
|
- 2: "https://culture.4dage.com/demo/audio/4.mp3",
|
|
|
- 3: "https://culture.4dage.com/demo/audio/5.mp3",
|
|
|
- 4: "https://culture.4dage.com/demo/audio/6.mp3",
|
|
|
- 5: "https://culture.4dage.com/demo/audio/7.mp3",
|
|
|
- 6: "https://culture.4dage.com/demo/audio/8.mp3",
|
|
|
- 7: "https://culture.4dage.com/demo/audio/9.mp3",
|
|
|
- 8: "https://culture.4dage.com/demo/audio/10.mp3",
|
|
|
- 9: "https://culture.4dage.com/demo/audio/11.mp3",
|
|
|
- 10: "https://culture.4dage.com/demo/audio/12.13.mp3",
|
|
|
- 11: "https://culture.4dage.com/demo/audio/14.mp3",
|
|
|
- 12: "https://culture.4dage.com/demo/audio/15.mp3",
|
|
|
- 13: "https://culture.4dage.com/demo/audio/16.mp3",
|
|
|
- 14: "https://culture.4dage.com/demo/audio/17.mp3",
|
|
|
- 15: "https://culture.4dage.com/demo/audio/18.mp3",
|
|
|
- 16: "https://culture.4dage.com/demo/audio/19.mp3",
|
|
|
- 17: "https://culture.4dage.com/demo/audio/20.21.mp3",
|
|
|
- 18: "https://culture.4dage.com/demo/audio/22.mp3",
|
|
|
- 19: "https://culture.4dage.com/demo/audio/23.mp3",
|
|
|
- 20: "https://culture.4dage.com/demo/audio/24.mp3",
|
|
|
-};
|
|
|
-
|
|
|
Component({
|
|
|
/**
|
|
|
* 组件的属性列表
|
|
@@ -115,6 +98,7 @@ Component({
|
|
|
*/
|
|
|
data: {
|
|
|
status: "0",
|
|
|
+ isAndroid:app.globalData.userInfo.system.toLowerCase().indexOf("android")>-1,
|
|
|
cdn_url: CDN_URL,
|
|
|
connect_status: CONNECT_STATUS,
|
|
|
status_text: STATUS_TEXT,
|
|
@@ -140,11 +124,29 @@ Component({
|
|
|
|
|
|
openBluetoothAdapter().then(
|
|
|
(res) => {
|
|
|
- wx.hideLoading({fail(){}});
|
|
|
+ wx.hideLoading({ fail() {} });
|
|
|
cb(res);
|
|
|
+ // if (app.globalData.userInfo.system.toLowerCase().indexOf("android")>-1) {
|
|
|
+ // wx.getLocation({
|
|
|
+ // type: "gcj02", //返回可以用于wx.openLocation的经纬度
|
|
|
+ // success: () => {
|
|
|
+ // cb(res);
|
|
|
+ // },
|
|
|
+ // fail: (err) => {
|
|
|
+ // console.log(err);
|
|
|
+ // wx.showToast({
|
|
|
+ // title: "请打开手机定位服务",
|
|
|
+ // icon: "error",
|
|
|
+ // duration: 500,
|
|
|
+ // });
|
|
|
+ // },
|
|
|
+ // });
|
|
|
+ // } else {
|
|
|
+ // cb(res);
|
|
|
+ // }
|
|
|
},
|
|
|
() => {
|
|
|
- wx.hideLoading({fail(){}});
|
|
|
+ wx.hideLoading({ fail() {} });
|
|
|
wx.showToast({
|
|
|
title: "请打开蓝牙",
|
|
|
icon: "error",
|
|
@@ -169,25 +171,63 @@ Component({
|
|
|
uuids: ["FDA50693-A4E2-4FB1-AFCF-C6EB07647825"],
|
|
|
})
|
|
|
.then(() => {
|
|
|
- wx.hideLoading({fail(){}});
|
|
|
+ wx.hideLoading({ fail() {} });
|
|
|
this.setData({
|
|
|
status: "2",
|
|
|
});
|
|
|
- isCollect = true
|
|
|
+ isCollect = true;
|
|
|
+ updateingBeacon = false
|
|
|
+
|
|
|
+ if (this.data.isAndroid) {
|
|
|
+ checkCollect && clearInterval(checkCollect)
|
|
|
+ checkTimeout && clearTimeout(checkTimeout)
|
|
|
+ checkTimeout = setTimeout(() => {
|
|
|
+ checkCollect = setInterval(() => {
|
|
|
+ if (!updateingBeacon) {
|
|
|
+ if (!hadshowModal) {
|
|
|
+ hadshowModal = true
|
|
|
+ wx.showModal({
|
|
|
+ title: "定位功能已停止,请检查手机定位服务和蓝牙是否保持打开状态",
|
|
|
+ showCancel: false,
|
|
|
+ success:()=>{
|
|
|
+ this.resetPage()
|
|
|
+ checkCollect && clearInterval(checkCollect)
|
|
|
+ checkTimeout && clearTimeout(checkTimeout)
|
|
|
+ hadshowModal = false
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, 1000)
|
|
|
+ }, 2000);
|
|
|
+ }
|
|
|
+
|
|
|
wx.onBeaconUpdate((data) => {
|
|
|
+ console.log(data, "onBeaconUpdate");
|
|
|
if (!isCollect) {
|
|
|
- return
|
|
|
+ return;
|
|
|
}
|
|
|
- console.log('还在收集…………');
|
|
|
+ if (this.data.isAndroid) {
|
|
|
+ updateTO && clearTimeout(updateTO)
|
|
|
+
|
|
|
+ updateTO = setTimeout(() => {
|
|
|
+ console.log('重置updateingBeacon');
|
|
|
+ updateingBeacon = false
|
|
|
+ }, 3000);
|
|
|
+
|
|
|
+ updateingBeacon = true
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log("还在收集…………");
|
|
|
// 需要收集十组数据,索引号最大的组是最旧的
|
|
|
if (!aveArr.includes(data.beacons)) {
|
|
|
if (aveArr.length >= AveLength) {
|
|
|
//当超过十组时,应该将索引号大的组淘汰掉
|
|
|
- aveArr.pop()
|
|
|
+ aveArr.pop();
|
|
|
}
|
|
|
aveArr.unshift(BeaconUtils.clone(data.beacons)); //在队列前面插入
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
let all = []; //获取所有data.beacons数据
|
|
|
for (let i = 0; i < aveArr.length; i++) {
|
|
|
let ele = aveArr[i];
|
|
@@ -198,7 +238,7 @@ Component({
|
|
|
if (Math.abs(item.rssi) < RSSIVAL) {
|
|
|
// console.log(item.proximity,item.rssi);
|
|
|
if (!all.includes(item)) {
|
|
|
- all.push(BeaconUtils.clone(item))
|
|
|
+ all.push(BeaconUtils.clone(item));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -235,7 +275,6 @@ Component({
|
|
|
);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
//计算高斯模糊后的rssi值
|
|
|
let rssiArr = []; //模糊后的rssi数组
|
|
|
for (let i = 0; i < arr.length; ++i) {
|
|
@@ -250,8 +289,6 @@ Component({
|
|
|
rssiArr.push(sum);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
//时间加权后求rssi平均值
|
|
|
let aveOnTime = BeaconUtils.arrayAverage(
|
|
|
rssiArr
|
|
@@ -259,34 +296,24 @@ Component({
|
|
|
.concat(rssiArr)
|
|
|
);
|
|
|
|
|
|
-
|
|
|
-
|
|
|
//测距,根据时间加权后的rssi计算距离
|
|
|
// classfiy[key].forEach((item) => {
|
|
|
- let tmp = BeaconUtils.calculateAccuracy(
|
|
|
- TXPOWER,
|
|
|
- aveOnTime,
|
|
|
- N
|
|
|
- );
|
|
|
+ let tmp = BeaconUtils.calculateAccuracy(TXPOWER, aveOnTime, N);
|
|
|
|
|
|
-
|
|
|
- if (!accuracyList[key]) {
|
|
|
- //如果还没有对应的“信标号”
|
|
|
- accuracyList[key] = [tmp];
|
|
|
- }else{
|
|
|
- accuracyList[key].push(tmp);
|
|
|
- }
|
|
|
+ if (!accuracyList[key]) {
|
|
|
+ //如果还没有对应的“信标号”
|
|
|
+ accuracyList[key] = [tmp];
|
|
|
+ } else {
|
|
|
+ accuracyList[key].push(tmp);
|
|
|
+ }
|
|
|
// });
|
|
|
-
|
|
|
-
|
|
|
});
|
|
|
|
|
|
+ Object.keys(accuracyList).forEach((item) => {
|
|
|
+ console.log(accuracyList[item] + "---------" + item);
|
|
|
+ });
|
|
|
|
|
|
- // Object.keys(accuracyList).forEach(item=>{
|
|
|
- // console.log(accuracyList[item]+'---------'+item);
|
|
|
- // })
|
|
|
-
|
|
|
- // console.log('--------分隔--------');
|
|
|
+ console.log("--------分隔--------");
|
|
|
|
|
|
// 筛选器,筛选出配对的一组信标
|
|
|
let signSelect = new Array(21).fill(0); //记录各组的信标出现数量,投票选择最多的,依次是prologue,annexHall,mainHall
|
|
@@ -294,39 +321,37 @@ Component({
|
|
|
// const annexHall = ['10003','10004','10005','10006','10007'] //附厅
|
|
|
// const mainHall = ['10008'] //主厅
|
|
|
|
|
|
-
|
|
|
-
|
|
|
Object.keys(accuracyList).forEach((key) => {
|
|
|
let aveAccuracy = BeaconUtils.arrayAverage(accuracyList[key]);
|
|
|
|
|
|
if (aveAccuracy < R) {
|
|
|
for (let i = 0; i < group.length; i++) {
|
|
|
if (group[i].includes(key)) {
|
|
|
- signSelect[i] += 1/(aveAccuracy+0.01); //取距离的反比例函数值,距离越近,值越大,+0.01是为了防止除数为0
|
|
|
+ signSelect[i] += 1 / (aveAccuracy + 0.01); //取距离的反比例函数值,距离越近,值越大,+0.01是为了防止除数为0
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
- // console.log(signSelect);
|
|
|
+ console.log(signSelect);
|
|
|
//对小于预设半径的信号进行数量投票
|
|
|
result = BeaconUtils.maxIndex(signSelect);
|
|
|
|
|
|
- // console.log("result", result);
|
|
|
+ console.log("result", result);
|
|
|
if (result != null && result !== oldResult) {
|
|
|
- wx.hideLoading({fail(){}})
|
|
|
- this.playAudio(AudioAddress[result])
|
|
|
+ wx.hideLoading({ fail() {} });
|
|
|
+ this.playAudio(AudioAddress[result]);
|
|
|
oldResult = result;
|
|
|
}
|
|
|
- if (result === null && oldResult!=-1) {
|
|
|
+ if (result === null && oldResult != -1) {
|
|
|
wx.showLoading({
|
|
|
title: "音频切换中…",
|
|
|
mask: true,
|
|
|
});
|
|
|
- changeTO && clearTimeout(changeTO)
|
|
|
+ changeTO && clearTimeout(changeTO);
|
|
|
changeTO = setTimeout(() => {
|
|
|
- wx.hideLoading({fail(){}})
|
|
|
+ wx.hideLoading({ fail() {} });
|
|
|
}, 4000);
|
|
|
- this.stopAndDestroyAudio()
|
|
|
+ this.stopAndDestroyAudio();
|
|
|
}
|
|
|
|
|
|
let temp = DISTRIBUTION.find((item) => {
|
|
@@ -336,72 +361,81 @@ Component({
|
|
|
this.setData({
|
|
|
classfiy,
|
|
|
targetObj: temp || {},
|
|
|
- result
|
|
|
+ result,
|
|
|
});
|
|
|
});
|
|
|
})
|
|
|
- .catch(() => {
|
|
|
- wx.showToast({
|
|
|
- title: "连接失败",
|
|
|
- icon: "error",
|
|
|
- duration: 500,
|
|
|
- });
|
|
|
+ .catch((err) => {
|
|
|
+ console.log(err,'1111');
|
|
|
+ if (err.errCode == 11002) {
|
|
|
+ wx.showToast({
|
|
|
+ title: "请打开手机定位服务",
|
|
|
+ icon: "error",
|
|
|
+ duration: 500,
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ wx.showToast({
|
|
|
+ title: "连接失败",
|
|
|
+ icon: "error",
|
|
|
+ duration: 500,
|
|
|
+ });
|
|
|
+ }
|
|
|
});
|
|
|
});
|
|
|
},
|
|
|
|
|
|
stopAndDestroyAudio() {
|
|
|
- console.log(innerAudioContext,'innerAudioContext');
|
|
|
+ oldResult = -1;
|
|
|
if (!innerAudioContext) {
|
|
|
- return
|
|
|
+ return;
|
|
|
}
|
|
|
-
|
|
|
- innerAudioContext.pause()
|
|
|
+
|
|
|
+ innerAudioContext.pause();
|
|
|
innerAudioContext.stop();
|
|
|
- innerAudioContext.src = null
|
|
|
+ innerAudioContext.src = null;
|
|
|
innerAudioContext.destroy();
|
|
|
- innerAudioContext = null
|
|
|
- oldResult = -1
|
|
|
+ innerAudioContext = null;
|
|
|
},
|
|
|
|
|
|
-
|
|
|
playAudio(src) {
|
|
|
if (!innerAudioContext) {
|
|
|
innerAudioContext = wx.createInnerAudioContext({
|
|
|
- useWebAudioImplement: true
|
|
|
- })
|
|
|
+ useWebAudioImplement: true,
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
-
|
|
|
innerAudioContext.src = src;
|
|
|
innerAudioContext.loop = true;
|
|
|
- innerAudioContext.onCanplay(()=>{
|
|
|
+ innerAudioContext.onCanplay(() => {
|
|
|
innerAudioContext.play();
|
|
|
- })
|
|
|
+ });
|
|
|
// innerAudioContext.play();
|
|
|
},
|
|
|
|
|
|
- stopBeaconDiscovery(notips=null) {
|
|
|
+ stopBeaconDiscovery(notips = null) {
|
|
|
console.log("这是取消连接");
|
|
|
// 取消连接 停止播放音乐 目标对象置为{} 设置为第一次进入
|
|
|
-
|
|
|
+ checkCollect && clearInterval(checkCollect)
|
|
|
+ checkTimeout && clearTimeout(checkTimeout)
|
|
|
+ updateTO && clearTimeout(updateTO)
|
|
|
this.stopAndDestroyAudio();
|
|
|
this.reset();
|
|
|
- isCollect = false
|
|
|
- stopBeaconDiscovery().then(() => {
|
|
|
- !notips && wx.showToast({
|
|
|
- title: "取消连接成功",
|
|
|
- icon: "error",
|
|
|
- duration: 1000,
|
|
|
+ isCollect = false;
|
|
|
+ stopBeaconDiscovery()
|
|
|
+ .then(() => {
|
|
|
+ !notips &&
|
|
|
+ wx.showToast({
|
|
|
+ title: "取消连接成功",
|
|
|
+ icon: "error",
|
|
|
+ duration: 1000,
|
|
|
+ });
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ console.log(err);
|
|
|
});
|
|
|
-
|
|
|
- })
|
|
|
- .catch((err) => {
|
|
|
- console.log(err);
|
|
|
- });
|
|
|
},
|
|
|
|
|
|
- reset(){
|
|
|
+ reset() {
|
|
|
this.setData({
|
|
|
status: "0",
|
|
|
targetObj: {},
|
|
@@ -414,11 +448,11 @@ Component({
|
|
|
isScanPlay: true,
|
|
|
});
|
|
|
wx.scanCode({
|
|
|
- success: res=> {
|
|
|
- this.stopBeaconDiscovery(true)
|
|
|
- this.playAudio(res["result"])
|
|
|
+ success: (res) => {
|
|
|
+ this.stopBeaconDiscovery(true);
|
|
|
+ this.playAudio(res["result"]);
|
|
|
},
|
|
|
- fail: ()=> {
|
|
|
+ fail: () => {
|
|
|
console.log("fail innerAudioContext", innerAudioContext);
|
|
|
this.setData({
|
|
|
isScanPlay: false,
|
|
@@ -433,57 +467,41 @@ Component({
|
|
|
});
|
|
|
},
|
|
|
|
|
|
- resetPage(){
|
|
|
+ resetPage() {
|
|
|
this.stopBeaconDiscovery(true);
|
|
|
},
|
|
|
|
|
|
- getAudios() {
|
|
|
- http.get("/api/web/getAudioIndex").then((res) => {
|
|
|
- let { data } = res,
|
|
|
- target = {};
|
|
|
- data.forEach((item) => {
|
|
|
- switch (item.type) {
|
|
|
- case 1:
|
|
|
- target["10001"] = `${this.data.api_base_url}/data/${item.audio}`;
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- target["10002"] = `${this.data.api_base_url}/data/${item.audio}`;
|
|
|
- break;
|
|
|
- case 3:
|
|
|
- target["10003"] = `${this.data.api_base_url}/data/${item.audio}`;
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- });
|
|
|
- this.setData({
|
|
|
- audio_address: target,
|
|
|
- });
|
|
|
- });
|
|
|
- },
|
|
|
},
|
|
|
lifetimes: {
|
|
|
attached: function () {
|
|
|
- audioInterruptionCb = ()=>{
|
|
|
+ audioInterruptionCb = () => {
|
|
|
console.log(innerAudioContext);
|
|
|
if (innerAudioContext) {
|
|
|
- this.resetPage()
|
|
|
+ this.resetPage();
|
|
|
}
|
|
|
- }
|
|
|
- bluetoothAdapterStateChangeCb=(res)=>{
|
|
|
+ };
|
|
|
+ bluetoothAdapterStateChangeCb = (res) => {
|
|
|
if (!Boolean(res.available)) {
|
|
|
- this.resetPage()
|
|
|
+ this.resetPage();
|
|
|
}
|
|
|
- }
|
|
|
- wx.onAudioInterruptionEnd(audioInterruptionCb)
|
|
|
- wx.onBluetoothAdapterStateChange(bluetoothAdapterStateChangeCb)
|
|
|
- wx.onAppShow(audioInterruptionCb)
|
|
|
+ };
|
|
|
+
|
|
|
+ wx.onAudioInterruptionEnd(audioInterruptionCb);
|
|
|
+ wx.onBluetoothAdapterStateChange(bluetoothAdapterStateChangeCb);
|
|
|
+ wx.onAppShow(audioInterruptionCb);
|
|
|
+ app.events.on('network', this, (data) =>{
|
|
|
+ if (!data) {
|
|
|
+ this.resetPage();
|
|
|
+ }
|
|
|
+ })
|
|
|
},
|
|
|
detached: function () {
|
|
|
- this.resetPage()
|
|
|
- wx.offAudioInterruptionEnd(audioInterruptionCb)
|
|
|
- wx.offBluetoothAdapterStateChange(bluetoothAdapterStateChangeCb)
|
|
|
- wx.offAppShow(audioInterruptionCb)
|
|
|
- }
|
|
|
+ this.resetPage();
|
|
|
+ wx.offAudioInterruptionEnd(audioInterruptionCb);
|
|
|
+ wx.offBluetoothAdapterStateChange(bluetoothAdapterStateChangeCb);
|
|
|
+ wx.offAppShow(audioInterruptionCb);
|
|
|
+ app.events.remove('network',this)
|
|
|
+
|
|
|
+ },
|
|
|
},
|
|
|
});
|