|
@@ -10,6 +10,9 @@ class bleManager {
|
|
that.compareList = [];
|
|
that.compareList = [];
|
|
that.connectWillDevice = null;
|
|
that.connectWillDevice = null;
|
|
that.callBackConnect = null;
|
|
that.callBackConnect = null;
|
|
|
|
+ that.requestBlueTime = 0;
|
|
|
|
+ ///正在执行扫描中
|
|
|
|
+ that.doStartScaning = false;
|
|
}
|
|
}
|
|
|
|
|
|
///获取比较的数据
|
|
///获取比较的数据
|
|
@@ -17,10 +20,6 @@ class bleManager {
|
|
return this.compareList;
|
|
return this.compareList;
|
|
}
|
|
}
|
|
|
|
|
|
- getConnectWillDevice() {
|
|
|
|
- return this.connectWillDevice;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
setConnectWillDevice(connectWillDevice) {
|
|
setConnectWillDevice(connectWillDevice) {
|
|
this.connectWillDevice = connectWillDevice;
|
|
this.connectWillDevice = connectWillDevice;
|
|
}
|
|
}
|
|
@@ -137,26 +136,93 @@ class bleManager {
|
|
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
|
|
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ ///获取毫秒
|
|
|
|
+ getCurrentMills() {
|
|
|
|
+ var currentDate = new Date();
|
|
|
|
+ var currentTimeMillis = currentDate.getTime();
|
|
|
|
+ // return Math.floor(currentTimeMillis / 1000);
|
|
|
|
+ return currentTimeMillis;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 等待多少秒
|
|
|
|
+ delay(ms) {
|
|
|
|
+ return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
// 开始搜索蓝牙设备
|
|
// 开始搜索蓝牙设备
|
|
- startScan(boolean) {
|
|
|
|
|
|
+ async startScan(connectWillDevice, boolean, callBackConnect) {
|
|
|
|
+
|
|
|
|
+
|
|
var that = this;
|
|
var that = this;
|
|
|
|
+ const route_constant = require('../utils/route_constant');
|
|
|
|
+ const connectBleRoot = route_constant.connectBleRoot;
|
|
|
|
+ const route_util = require('../utils/route_util');
|
|
|
|
+ var lastPageRoute = route_util.getLastPageRoute();
|
|
|
|
+
|
|
|
|
+ ///蓝牙连接 做限制
|
|
|
|
+ if (callBackConnect == null && lastPageRoute != connectBleRoot) {
|
|
|
|
+ if (that.doStartScaning == true) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ that.doStartScaning = true;
|
|
|
|
+ var currentMill = that.getCurrentMills();
|
|
|
|
+ var waitMills = 0;
|
|
|
|
+ var reduce = currentMill - that.requestBlueTime;
|
|
|
|
+ const delayMiliis = 5 * 1000;
|
|
|
|
+ if (reduce > 0 && reduce < delayMiliis) {
|
|
|
|
+ waitMills = delayMiliis - reduce;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (callBackConnect == null && lastPageRoute == connectBleRoot) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (waitMills > 0) {
|
|
|
|
+ await that.delay(waitMills);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (callBackConnect == null && lastPageRoute == connectBleRoot) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
wx.closeBluetoothAdapter({
|
|
wx.closeBluetoothAdapter({
|
|
complete: function (res) {
|
|
complete: function (res) {
|
|
wx.openBluetoothAdapter({
|
|
wx.openBluetoothAdapter({
|
|
success: function (res) {
|
|
success: function (res) {
|
|
wx.getBluetoothAdapterState({
|
|
wx.getBluetoothAdapterState({
|
|
- success: function (res) {},
|
|
|
|
|
|
+ success: function (res) {
|
|
|
|
+ that.doStartScaning = false;
|
|
|
|
+ },
|
|
fail(err) {
|
|
fail(err) {
|
|
- boolean(false);
|
|
|
|
|
|
+ that.doStartScaning = false;
|
|
|
|
+ if (boolean != null) {
|
|
|
|
+ boolean(false);
|
|
|
|
+ }
|
|
},
|
|
},
|
|
})
|
|
})
|
|
wx.startBluetoothDevicesDiscovery({
|
|
wx.startBluetoothDevicesDiscovery({
|
|
allowDuplicatesKey: false,
|
|
allowDuplicatesKey: false,
|
|
success: function (res) {
|
|
success: function (res) {
|
|
- boolean(true);
|
|
|
|
|
|
+ that.doStartScaning = false;
|
|
|
|
+ that.requestBlueTime = that.getCurrentMills();
|
|
|
|
+ if (boolean != null) {
|
|
|
|
+ boolean(true);
|
|
|
|
+ }
|
|
|
|
+ that.setConnectWillDevice(connectWillDevice);
|
|
|
|
+ that.setCallBackConnect(callBackConnect);
|
|
that.compareList = [];
|
|
that.compareList = [];
|
|
},
|
|
},
|
|
- fail(err) {},
|
|
|
|
|
|
+ fail(err) {
|
|
|
|
+ that.doStartScaning = false;
|
|
|
|
+ that.requestBlueTime = that.getCurrentMills();
|
|
|
|
+ if (boolean != null) {
|
|
|
|
+ boolean(false);
|
|
|
|
+ }
|
|
|
|
+ },
|
|
})
|
|
})
|
|
},
|
|
},
|
|
fail: function (res) {
|
|
fail: function (res) {
|
|
@@ -166,7 +232,10 @@ class bleManager {
|
|
showCancel: false,
|
|
showCancel: false,
|
|
success: function (res) {}
|
|
success: function (res) {}
|
|
});
|
|
});
|
|
- boolean(false);
|
|
|
|
|
|
+ that.doStartScaning = false;
|
|
|
|
+ if (boolean != null) {
|
|
|
|
+ boolean(false);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
@@ -175,14 +244,15 @@ class bleManager {
|
|
|
|
|
|
// 停止搜索
|
|
// 停止搜索
|
|
stopSearch() {
|
|
stopSearch() {
|
|
|
|
+ var that = this;
|
|
|
|
+ that.setCallBackConnect(null);
|
|
|
|
+ that.setConnectWillDevice(null);
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
wx.stopBluetoothDevicesDiscovery({
|
|
wx.stopBluetoothDevicesDiscovery({
|
|
success: (res) => {
|
|
success: (res) => {
|
|
- console.log('停止搜索成功:', res);
|
|
|
|
resolve(res);
|
|
resolve(res);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
- console.error('停止搜索失败:', err);
|
|
|
|
reject(new Error('停止搜索失败'));
|
|
reject(new Error('停止搜索失败'));
|
|
}
|
|
}
|
|
});
|
|
});
|