|
@@ -7,6 +7,23 @@ class bleManager {
|
|
|
that.hasPermission = false;
|
|
|
that.scanDevices = [];
|
|
|
that.publicDevice = null;
|
|
|
+ ///用来对比列表
|
|
|
+ that.compareList = [];
|
|
|
+ that.searching = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ ///获取比较的数据
|
|
|
+ getCompareList() {
|
|
|
+ return this.compareList;
|
|
|
+ }
|
|
|
+
|
|
|
+ ///处理当前正在连接中,对比暂停
|
|
|
+ setSearching(search) {
|
|
|
+ this.searching = search;
|
|
|
+ }
|
|
|
+
|
|
|
+ getSearching() {
|
|
|
+ return this.searching;
|
|
|
}
|
|
|
|
|
|
/// 监控蓝牙打开状态
|
|
@@ -18,32 +35,91 @@ class bleManager {
|
|
|
}
|
|
|
|
|
|
///监听搜索设备列表
|
|
|
- getBluetoothDevices(callBack) {
|
|
|
+ getBluetoothDevices(callBack, toCompare) {
|
|
|
var that = this;
|
|
|
wx.onBluetoothDeviceFound(function (res) {
|
|
|
///第一种情况
|
|
|
if (res.deviceId) {
|
|
|
- if (res.name == getApp().globalData.connectWillDevice.clientType) {
|
|
|
- res.advertisData = res.advertisData ? that.buf2hex(res.advertisData) : '';
|
|
|
- callBack(res);
|
|
|
+ if (!toCompare) {
|
|
|
+ if (res.name == getApp().globalData.connectWillDevice.clientType) {
|
|
|
+ res.advertisData = res.advertisData ? that.buf2hex(res.advertisData) : '';
|
|
|
+ callBack(res);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (res.name != "") {
|
|
|
+ if (that.compareList.length > 0) {
|
|
|
+ var has = false;
|
|
|
+ for (var i = 0; i < that.compareList.length; i++) {
|
|
|
+ if (res.deviceId == that.compareList[i].deviceId) {
|
|
|
+ has = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!has) {
|
|
|
+ that.compareList.push(res);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ that.compareList.push(res);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
///第二种情况
|
|
|
else if (res.devices) {
|
|
|
- for (var i = 0; i < res.devices.length; i++) {
|
|
|
- var temp = res.devices[i];
|
|
|
- if (temp.name == getApp().globalData.connectWillDevice.clientType) {
|
|
|
- temp.advertisData = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
|
|
|
- callBack(temp);
|
|
|
- break;
|
|
|
+ if (!toCompare) {
|
|
|
+ for (var i = 0; i < res.devices.length; i++) {
|
|
|
+ var temp = res.devices[i];
|
|
|
+ if (temp.name == getApp().globalData.connectWillDevice.clientType) {
|
|
|
+ temp.advertisData = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
|
|
|
+ callBack(temp);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (var i = 0; i < res.devices.length; i++) {
|
|
|
+ if (that.compareList.length > 0) {
|
|
|
+ var has = false;
|
|
|
+ for (var j = 0; j < that.compareList.length; j++) {
|
|
|
+ if (res.devices[i].name != "") {
|
|
|
+ if (res.devices[i].deviceId == that.compareList[j].deviceId) {
|
|
|
+ has = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!has) {
|
|
|
+ that.compareList.push(res.devices[i]);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ that.compareList.push(res.devices[i]);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
///第三种情况
|
|
|
else if (res[0]) {
|
|
|
- if (res[0].name == getApp().globalData.connectWillDevice.clientType) {
|
|
|
- res[0].advertisData = res[0].advertisData ? that.buf2hex(res[0].advertisData) : '';
|
|
|
- callBack(res[0]);
|
|
|
+ if (!toCompare) {
|
|
|
+ if (res[0].name == getApp().globalData.connectWillDevice.clientType) {
|
|
|
+ res[0].advertisData = res[0].advertisData ? that.buf2hex(res[0].advertisData) : '';
|
|
|
+ callBack(res[0]);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (res[0].name != "") {
|
|
|
+ if (that.compareList.length > 0) {
|
|
|
+ var has = false;
|
|
|
+ for (var i = 0; i < that.compareList.length; i++) {
|
|
|
+ if (res[0].deviceId == that.compareList[i].deviceId) {
|
|
|
+ has = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!has) {
|
|
|
+ that.compareList.push(res[0]);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ that.compareList.push(res[0]);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
});
|
|
@@ -56,6 +132,7 @@ class bleManager {
|
|
|
|
|
|
// 开始搜索蓝牙设备
|
|
|
startScan(boolean) {
|
|
|
+ var that = this;
|
|
|
wx.closeBluetoothAdapter({
|
|
|
complete: function (res) {
|
|
|
wx.openBluetoothAdapter({
|
|
@@ -70,6 +147,7 @@ class bleManager {
|
|
|
allowDuplicatesKey: false,
|
|
|
success: function (res) {
|
|
|
boolean(true);
|
|
|
+ that.compareList = [];
|
|
|
},
|
|
|
fail(err) {},
|
|
|
})
|