فهرست منبع

feature: 处理直接关掉蓝牙,离线所有蓝牙设备

Damon 7 ماه پیش
والد
کامیت
1717451e08
3فایلهای تغییر یافته به همراه54 افزوده شده و 27 حذف شده
  1. 14 12
      app.js
  2. 5 1
      devices/ble_manager.js
  3. 35 14
      pages/index/index.js

+ 14 - 12
app.js

@@ -92,7 +92,7 @@ App({
     // 设备连接
     that.globalData.client.on('connect', () => {
       var page = getCurrentPages()[getCurrentPages().length - 1];
-      if (page.mqttCallback) {
+      if (!strings.isEmpty(page.mqttCallback)) {
         page.mqttCallback("connect")
       };
     });
@@ -102,7 +102,7 @@ App({
       var page = getCurrentPages()[getCurrentPages().length - 1];
       if (topic.indexOf("status/onoffline") !== -1) {
         ///当前界面回调
-        if (page.mqttCallback) {
+        if (!strings.isEmpty(page.mqttCallback)) {
           page.mqttCallback("message_onoffline", {
             topic,
             payload
@@ -116,36 +116,38 @@ App({
           });
         }
       } else if (topic.indexOf("user/pub_response") !== -1) {
-        page.mqttCallback("message", {
-          topic,
-          payload
-        });
+        if (!strings.isEmpty(page.mqttCallback)) {
+          page.mqttCallback("message", {
+            topic,
+            payload
+          });
+        }
       }
     });
 
     // 重连
     that.globalData.client.on("reconnect", function (errr) {
-      console.log(" 服务器 reconnect的回调", errr);
+      console.log("reconnect的回调==" + JSON.stringify(errr))
       var page = getCurrentPages()[getCurrentPages().length - 1];
-      if (page.mqttCallback) {
+      if (!strings.isEmpty(page.mqttCallback)) {
         page.mqttCallback("reconnect")
       };
     });
 
     // 离线回调
     that.globalData.client.on("offline", function (errr) {
-      console.log(" 服务器 offline的回调", errr)
+      console.log("offline的回调==" + JSON.stringify(errr))
       var page = getCurrentPages()[getCurrentPages().length - 1];
-      if (page.mqttCallback) {
+      if (!strings.isEmpty(page.mqttCallback)) {
         page.mqttCallback("offline")
       };
     });
 
     // 错误回调
     that.globalData.client.on("error", function (error) {
-      console.log(" 服务器 error 的回调" + error)
+      console.log("错误码的回调==" + JSON.stringify(errr))
       var page = getCurrentPages()[getCurrentPages().length - 1];
-      if (page.mqttCallback) {
+      if (!strings.isEmpty(page.mqttCallback)) {
         page.mqttCallback("error", {})
       };
     });

+ 5 - 1
devices/ble_manager.js

@@ -54,6 +54,9 @@ class bleManager {
     // });
     wx.onBluetoothAdapterStateChange(function (res) {
       that.isAvailable = res.available;
+      if (!that.isAvailable) {
+        getCurrentPages()[0].updateDeviceList([], false, true);
+      }
     })
   }
 
@@ -178,6 +181,7 @@ class bleManager {
           that.dissmissDevice.push(result);
         }
       }
+
       // ///是否已配对
       // wx.isBluetoothDevicePaired({
       //   deviceId: result.deviceId,
@@ -591,7 +595,7 @@ class bleManager {
     });
   }
 
-  connect() { }
+  connect() {}
 
   // 发现特征值 read / write
   discoverCharacteristics(deviceId, serviceId) {

+ 35 - 14
pages/index/index.js

@@ -89,7 +89,7 @@ Page({
     BtHelper.getInstance().initBluetoothAdapter();
     var l = store.getStore("deviceList");
     if (!strings.isEmpty(l)) {
-      that.updateDeviceList(l, true);
+      that.updateDeviceList(l, true, false);
     }
 
     ///监听蓝牙设备
@@ -153,7 +153,7 @@ Page({
           }
 
           if (isChanged) {
-            that.updateDeviceList(deviceList, false);
+            that.updateDeviceList(deviceList, false, false);
           }
 
           var autoConnected = that.data.autoConnected;
@@ -217,7 +217,7 @@ Page({
           else if (payloads.type === "play" || payloads.type === "play_state") {
             var deviceList = that.data.deviceList;
             var deviceListSelect = that.data.deviceListSelect;
-            if (deviceList.length <= deviceListSelect) {
+            if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
               return;
             };
 
@@ -274,7 +274,7 @@ Page({
             }
             ///数据有更新
             if (isUpdate) {
-              that.updateDeviceList(deviceList, false);
+              that.updateDeviceList(deviceList, false, false);
             }
           } else if (payloads.type === "battery" && payloads.other) {
             that.setData({
@@ -339,7 +339,7 @@ Page({
 
     ///数据有更新
     if (isUpdate) {
-      that.updateDeviceList(deviceList, false);
+      that.updateDeviceList(deviceList, false, false);
     }
 
     deviceList = that.data.deviceList;
@@ -396,7 +396,7 @@ Page({
     };
 
     that.setData({
-      actionIndex: e.currentTarget.dataset.index
+      actionIndex: e.currentTarget.dataset.index,
     });
 
     var channelData = that.data.channelData;
@@ -475,6 +475,7 @@ Page({
     }
   },
 
+  // 关于我们
   goMeAbout() {
     wx.navigateTo({
       url: './../about/about',
@@ -593,15 +594,20 @@ Page({
       return;
     };
 
+    ///限制蓝牙设备和不在线wifi设备
+    var device = deviceList[index];
+    if (device.connectType != 3 || device.state != "online") {
+      return;
+    }
+
     // 取消订阅
     var thisDeviceMac = that.data.thisDeviceMac;
     if (thisDeviceMac !== null) {
       app.unsubscribe(`/${thisDeviceMac}/user/pub_response`);
     };
 
-    thisDeviceMac = `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`;
+    thisDeviceMac = `AIrSMArT_${device.deviceId.split("BLUFI_")[1]}`;
     that.setData({
-      autoConnected: true,
       deviceListSelect: index,
       thisDeviceMac: thisDeviceMac,
     });
@@ -650,7 +656,7 @@ Page({
     });
   },
 
-
+  // 去唤醒界面
   goWake() {
     var that = this;
     var deviceList = that.data.deviceList;
@@ -807,8 +813,9 @@ Page({
     });
 
     ///在线排序前面,wifi设备排序前面
-    that.updateDeviceList(deviceList, false);
+    that.updateDeviceList(deviceList, false, false);
     var indexPage = that.data.indexPage;
+    console.log("gadsfqwerqwerqrqr==ccc==");
     if (indexPage != 0) {
       that.setData({
         indexPage: 0,
@@ -841,13 +848,14 @@ Page({
       deviceId: newDevice.deviceId,
       name: "猫王小王子OTR-X",
       state: "online",
-      devName: "猫王小王子OTR-X",
+      devName: "",
+      // devName: "猫王小王子OTR-X",
       mac: newDevice.deviceId,
       image: "./../../img/min.png",
     });
 
     ///在线排序前面,wifi设备排序前面
-    that.updateDeviceList(deviceList, false);
+    that.updateDeviceList(deviceList, false, false);
     var indexPage = that.data.indexPage;
     if (indexPage != 0) {
       that.setData({
@@ -860,17 +868,30 @@ Page({
   },
 
   ///更新列表排序
-  updateDeviceList(deviceList, isInit) {
+  updateDeviceList(deviceList, isInit, closeAllBlue) {
+    var that = this;
+    if (strings.isEmpty(deviceList)) {
+      deviceList = that.data.deviceList;
+    }
+
     if (deviceList.length <= 0) {
       return;
     }
 
-    var that = this;
     var finalList = [];
     if (isInit) {
       deviceList[0].state = "offline";
     }
 
+    /// 让所有蓝牙设备离线
+    if (closeAllBlue) {
+      deviceList.forEach(element => {
+        if (element.connectType != 3) {
+          element.state = "offline";
+        }
+      });
+    }
+
     var isFirstOnline = false;
     if (deviceList[0].state == "online") {
       isFirstOnline = true;