Selaa lähdekoodia

feature: 处理连接中的问题

Damon 7 kuukautta sitten
vanhempi
commit
cd8863b9f1
2 muutettua tiedostoa jossa 125 lisäystä ja 123 poistoa
  1. 6 5
      pages/index/index.js
  2. 119 118
      utils/blufi/xBlufi-wx-impl.js

+ 6 - 5
pages/index/index.js

@@ -192,12 +192,14 @@ Page({
     switch (type) {
       ///先走订阅
       case "connect":
-        that.subscribeDevicesStatus();
+        that.subscribeDevicesStatus()
         break;
         ///再走有在线
       case "message_onoffline":
         that.onlineDevice(payloads);
         break;
+
+        // PlayState:  ///播放状态:0默认状态,1播放状态,2暂停状态, 3停止状态,4缓冲状态。
       case "message":
         ///获取频道数据
         ///锁定播放哪一个频道
@@ -371,7 +373,8 @@ Page({
     var deviceList = that.data.deviceList;
     if (!strings.isEmpty(deviceList)) {
       for (var i = 0; i < deviceList.length; i++) {
-        if (deviceList[i].connectType == 3 && device.state == "online") {
+        var device = deviceList[i];
+        if (device.connectType == 3) {
           let topic = `/AIrSMArT_${deviceList[i].deviceId.split("BLUFI_")[1]}/status/onoffline`;
           app.subscribe(topic);
         }
@@ -670,7 +673,7 @@ Page({
     };
 
     var device = deviceList[deviceListSelect];
-    console.log("gasdfasdfqweqrwq====" + device.ProdModel);
+    console.log("gasdfqwerqwewqrqr====" + JSON.stringify(device));
     wx.navigateTo({
       url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
     });
@@ -820,7 +823,6 @@ Page({
     ///在线排序前面,wifi设备排序前面
     that.updateDeviceList(deviceList, false, false);
     var indexPage = that.data.indexPage;
-    console.log("gadsfqwerqwerqrqr==ccc==");
     if (indexPage != 0) {
       that.setData({
         indexPage: 0,
@@ -845,7 +847,6 @@ Page({
       deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
     };
 
-    console.log("添加Wifi设备:" + JSON.stringify(newDevice));
     // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
     deviceList.unshift({
       /// Wifi连接

+ 119 - 118
utils/blufi/xBlufi-wx-impl.js

@@ -1,5 +1,9 @@
-const {isCN} = require('./../util.js');
-const {TextEncoder} = require('text-encoding');
+const {
+  isCN
+} = require('./../util.js');
+const {
+  TextEncoder
+} = require('text-encoding');
 let tempTimer = 0;
 let client = null;
 let util = null
@@ -70,7 +74,7 @@ function getCharCodeat(str) {
   const encoder = new TextEncoder('utf8');
   console.log(encoder);
   for (var i = 0; i < str.length; i++) {
-    if(isCN(str[i])) {
+    if (isCN(str[i])) {
       const t = encoder.encode(str[i]);
       for (var j = 0; j < t.length; j++) {
         list.push(t[j]);
@@ -153,12 +157,12 @@ function getSecret(deviceId, serviceId, characteristicId, client, kBytes, pBytes
     serviceId: serviceId,
     characteristicId: characteristicId,
     value: typedArray.buffer,
-    success: function(res) {
+    success: function (res) {
       if (obj.flag) {
         getSecret(deviceId, serviceId, characteristicId, client, kBytes, pBytes, gBytes, obj.laveData);
       }
     },
-    fail: function(res) {}
+    fail: function (res) {}
   })
 }
 
@@ -181,15 +185,14 @@ function writeDeviceRouterInfoStart(deviceId, serviceId, characteristicId, data)
     serviceId: serviceId,
     characteristicId: characteristicId,
     value: typedArray.buffer,
-    success: function(res) {
+    success: function (res) {
       if (obj.flag) {
         writeDeviceRouterInfoStart(deviceId, serviceId, characteristicId, obj.laveData);
       } else {
         writeRouterSsid(deviceId, serviceId, characteristicId, null);
       }
     },
-    fail: function(res) {
-    }
+    fail: function (res) {}
   })
 }
 
@@ -213,12 +216,12 @@ function writeCutomsData(deviceId, serviceId, characteristicId, data) {
     serviceId: serviceId,
     characteristicId: characteristicId,
     value: typedArray.buffer,
-    success: function(res) {
+    success: function (res) {
       if (obj.flag) {
         writeCutomsData(deviceId, serviceId, characteristicId, obj.laveData);
       }
     },
-    fail: function(res) {
+    fail: function (res) {
       //console.log(257);
     }
   })
@@ -247,14 +250,14 @@ function writeRouterSsid(deviceId, serviceId, characteristicId, data) {
     serviceId: serviceId,
     characteristicId: characteristicId,
     value: typedArray.buffer,
-    success: function(res) {
+    success: function (res) {
       if (obj.flag) {
         writeRouterSsid(deviceId, serviceId, characteristicId, obj.laveData);
       } else {
         writeDevicePwd(deviceId, serviceId, characteristicId, null);
       }
     },
-    fail: function(res) {
+    fail: function (res) {
       //console.log(257);
     }
   })
@@ -280,14 +283,14 @@ function writeDevicePwd(deviceId, serviceId, characteristicId, data) {
     serviceId: serviceId,
     characteristicId: characteristicId,
     value: typedArray.buffer,
-    success: function(res) {
+    success: function (res) {
       if (obj.flag) {
         writeDevicePwd(deviceId, serviceId, characteristicId, obj.laveData);
       } else {
         writeDeviceEnd(deviceId, serviceId, characteristicId, null);
       }
     },
-    fail: function(res) {}
+    fail: function (res) {}
   })
 }
 
@@ -301,10 +304,10 @@ function writeDeviceEnd(deviceId, serviceId, characteristicId) {
     serviceId: serviceId,
     characteristicId: characteristicId,
     value: typedArray.buffer,
-    success: function(res) {
+    success: function (res) {
 
     },
-    fail: function(res) {
+    fail: function (res) {
 
     }
   })
@@ -320,7 +323,7 @@ function init() {
   md5 = require('../../utils/blufi/crypto/md5.min.js');
   aesjs = require('../../utils/blufi/crypto/aes.js');
 
-  wx.onBLEConnectionStateChange(function(res) {
+  wx.onBLEConnectionStateChange(function (res) {
     let obj = {
       'type': mDeviceEvent.XBLUFI_TYPE.TYPE_STATUS_CONNECTED,
       'result': res.connected,
@@ -329,109 +332,109 @@ function init() {
     mDeviceEvent.notifyDeviceMsgEvent(obj);
   })
 
-  mDeviceEvent.listenStartDiscoverBle(true, function(options) {
+  mDeviceEvent.listenStartDiscoverBle(true, function (options) {
 
     if (options.isStart) {
       //第一步检查蓝牙适配器是否可用
-      wx.onBluetoothAdapterStateChange(function(res) {
+      wx.onBluetoothAdapterStateChange(function (res) {
         if (!res.available) {
 
         }
       });
-      if(!itue) {
+      if (!itue) {
         return;
       };
       itue = false;
       //第二步关闭适配器,重新来搜索
       wx.closeBluetoothAdapter({
-        complete: function(res) {
+        complete: function (res) {
           wx.openBluetoothAdapter({
-            success: function(res) {
+            success: function (res) {
               wx.getBluetoothAdapterState({
-                success: function(res) {
+                success: function (res) {
                   wx.stopBluetoothDevicesDiscovery({
-                    success: function(res) {
+                    success: function (res) {
                       itue = true;
-                        let devicesList = [];
-                        let countsTimes = 0;
-                        wx.onBluetoothDeviceFound(function(devices) {
-                          //剔除重复设备,兼容不同设备API的不同返回值
-                          var isnotexist = true;
-                          if (devices.deviceId) {
-                            if (devices.advertisData) {
-                              devices.advertisData = buf2hex(devices.advertisData)
-                            } else {
-                              devices.advertisData = ''
-                            }
-                            for (var i = 0; i < devicesList.length; i++) {
-                              if (devices.deviceId === devicesList[i].deviceId) {
-                                isnotexist = false
-                              }
-                            }
-                            if (isnotexist) {
-                              devicesList.push(devices)
-                            }
-                          } else if (devices.devices) {
-                            if (devices.devices[0].advertisData) {
-                              devices.devices[0].advertisData = buf2hex(devices.devices[0].advertisData)
-                            } else {
-                              devices.devices[0].advertisData = ''
-                            }
-                            for (var i = 0; i < devicesList.length; i++) {
-                              if (devices.devices[0].deviceId == devicesList[i].deviceId) {
-                                isnotexist = false
-                              }
-                            }
-                            if (isnotexist) {
-                              devicesList.push(devices.devices[0])
-                            }
-                          } else if (devices[0]) {
-                            if (devices[0].advertisData) {
-                              devices[0].advertisData = buf2hex(devices[0].advertisData)
-                            } else {
-                              devices[0].advertisData = ''
+                      let devicesList = [];
+                      let countsTimes = 0;
+                      wx.onBluetoothDeviceFound(function (devices) {
+                        //剔除重复设备,兼容不同设备API的不同返回值
+                        var isnotexist = true;
+                        if (devices.deviceId) {
+                          if (devices.advertisData) {
+                            devices.advertisData = buf2hex(devices.advertisData)
+                          } else {
+                            devices.advertisData = ''
+                          }
+                          for (var i = 0; i < devicesList.length; i++) {
+                            if (devices.deviceId === devicesList[i].deviceId) {
+                              isnotexist = false
                             }
-                            for (var i = 0; i < devices_list.length; i++) {
-                              if (devices[0].deviceId == devicesList[i].deviceId) {
-                                isnotexist = false
-                              }
+                          }
+                          if (isnotexist) {
+                            devicesList.push(devices)
+                          }
+                        } else if (devices.devices) {
+                          if (devices.devices[0].advertisData) {
+                            devices.devices[0].advertisData = buf2hex(devices.devices[0].advertisData)
+                          } else {
+                            devices.devices[0].advertisData = ''
+                          }
+                          for (var i = 0; i < devicesList.length; i++) {
+                            if (devices.devices[0].deviceId == devicesList[i].deviceId) {
+                              isnotexist = false
                             }
-                            if (isnotexist) {
-                              devicesList.push(devices[0])
+                          }
+                          if (isnotexist) {
+                            devicesList.push(devices.devices[0])
+                          }
+                        } else if (devices[0]) {
+                          if (devices[0].advertisData) {
+                            devices[0].advertisData = buf2hex(devices[0].advertisData)
+                          } else {
+                            devices[0].advertisData = ''
+                          }
+                          for (var i = 0; i < devices_list.length; i++) {
+                            if (devices[0].deviceId == devicesList[i].deviceId) {
+                              isnotexist = false
                             }
                           }
-
+                          if (isnotexist) {
+                            devicesList.push(devices[0])
+                          }
+                        }
+
+                        let obj = {
+                          'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS,
+                          'result': true,
+                          'data': devicesList
+                        }
+                        mDeviceEvent.notifyDeviceMsgEvent(obj);
+                      })
+                      wx.startBluetoothDevicesDiscovery({
+                        allowDuplicatesKey: true,
+                        success: function (res) {
                           let obj = {
-                            'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS,
+                            'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
                             'result': true,
-                            'data': devicesList
+                            'data': res
                           }
                           mDeviceEvent.notifyDeviceMsgEvent(obj);
-                        })
-                        wx.startBluetoothDevicesDiscovery({
-                          allowDuplicatesKey: true,
-                          success: function(res) {
-                            let obj = {
-                              'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
-                              'result': true,
-                              'data': res
-                            }
-                            mDeviceEvent.notifyDeviceMsgEvent(obj);
-                            //开始扫码,清空列表
-                            devicesList.length = 0;
-                            
-                          },
-                          fail: function(res) {
-                            let obj = {
-                              'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
-                              'result': false,
-                              'data': res
-                            }
-                            mDeviceEvent.notifyDeviceMsgEvent(obj);
+                          //开始扫码,清空列表
+                          devicesList.length = 0;
+
+                        },
+                        fail: function (res) {
+                          let obj = {
+                            'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
+                            'result': false,
+                            'data': res
                           }
-                        });
+                          mDeviceEvent.notifyDeviceMsgEvent(obj);
+                        }
+                      });
                     },
-                    fail: function(res) {
+                    fail: function (res) {
                       itue = true;
                       let obj = {
                         'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
@@ -442,7 +445,7 @@ function init() {
                     }
                   });
                 },
-                fail: function(res) {
+                fail: function (res) {
                   itue = true;
                   let obj = {
                     'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
@@ -453,7 +456,7 @@ function init() {
                 }
               });
             },
-            fail: function(res) {
+            fail: function (res) {
               itue = true;
               let obj = {
                 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START,
@@ -467,7 +470,7 @@ function init() {
       });
     } else {
       wx.stopBluetoothDevicesDiscovery({
-        success: function(res) {
+        success: function (res) {
           clearInterval(tempTimer);
           let obj = {
             'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP,
@@ -476,7 +479,7 @@ function init() {
           }
           mDeviceEvent.notifyDeviceMsgEvent(obj);
         },
-        fail: function(res) {
+        fail: function (res) {
           let obj = {
             'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP,
             'result': false,
@@ -488,14 +491,12 @@ function init() {
     }
   })
 
-
-  mDeviceEvent.listenConnectBle(true, function(options) {
-    // console.log("我要连接?", (options.isStart))
-    
+  mDeviceEvent.listenConnectBle(true, function (options) {
+    console.log("我要连接====" + options.isStart);
     if (options.isStart)
       wx.createBLEConnection({
         deviceId: options.deviceId,
-        success: function(res) {
+        success: function (res) {
           self.data.deviceId = options.deviceId
           mDeviceEvent.notifyDeviceMsgEvent({
             'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CONNECTED,
@@ -506,7 +507,7 @@ function init() {
             },
           });
         },
-        fail: function(res) {
+        fail: function (res) {
           self.data.deviceId = null
           mDeviceEvent.notifyDeviceMsgEvent({
             'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CONNECTED,
@@ -517,7 +518,7 @@ function init() {
       });
     else wx.closeBLEConnection({
       deviceId: options.deviceId,
-      success: function(res) {
+      success: function (res) {
         console.log('断开成功')
         self.data.deviceId = null
         mDeviceEvent.notifyDeviceMsgEvent({
@@ -529,7 +530,7 @@ function init() {
           }
         });
       },
-      fail: function(res) {
+      fail: function (res) {
         self.data.deviceId = null
         mDeviceEvent.notifyDeviceMsgEvent({
           'type': mDeviceEvent.XBLUFI_TYPE.TYPE_CLOSE_CONNECTED,
@@ -540,7 +541,7 @@ function init() {
     })
   })
 
-  mDeviceEvent.listenInitBleEsp32(true, function(options) {
+  mDeviceEvent.listenInitBleEsp32(true, function (options) {
     sequenceControl = 0;
     sequenceNumber = -1;
     self = null
@@ -578,7 +579,7 @@ function init() {
     wx.getBLEDeviceServices({
       // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
       deviceId: deviceId,
-      success: function(res) {
+      success: function (res) {
         var services = res.services;
         if (services.length > 0) {
           for (var i = 0; i < services.length; i++) {
@@ -588,7 +589,7 @@ function init() {
                 // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
                 deviceId: deviceId,
                 serviceId: serviceId,
-                success: function(res) {
+                success: function (res) {
                   var list = res.characteristics;
                   if (list.length > 0) {
                     for (var i = 0; i < list.length; i++) {
@@ -601,7 +602,7 @@ function init() {
                           deviceId: deviceId,
                           serviceId: serviceId,
                           characteristicId: list[1].uuid,
-                          success: function(res) {
+                          success: function (res) {
                             let characteristicId = self.data.characteristic_write_uuid
                             //通知设备交互方式(是否加密) start
                             client = util.blueDH(util.DH_P, util.DH_G, crypto);
@@ -623,10 +624,10 @@ function init() {
                               serviceId: serviceId,
                               characteristicId: characteristicId,
                               value: typedArray.buffer,
-                              success: function(res) {
+                              success: function (res) {
                                 getSecret(deviceId, serviceId, characteristicId, client, kBytes, pBytes, gBytes, null);
                               },
-                              fail: function(res) {
+                              fail: function (res) {
                                 let obj = {
                                   'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
                                   'result': false,
@@ -636,7 +637,7 @@ function init() {
                               }
                             })
                             //通知设备交互方式(是否加密) end
-                            wx.onBLECharacteristicValueChange(function(res) {
+                            wx.onBLECharacteristicValueChange(function (res) {
                               let list2 = (util.ab2hex(res.value));
                               // start
                               let result = self.data.result;
@@ -733,7 +734,7 @@ function init() {
                             })
 
                           },
-                          fail: function(res) {
+                          fail: function (res) {
                             let obj = {
                               'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
                               'result': false,
@@ -746,7 +747,7 @@ function init() {
                     }
                   }
                 },
-                fail: function(res) {
+                fail: function (res) {
                   let obj = {
                     'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
                     'result': false,
@@ -761,7 +762,7 @@ function init() {
           }
         }
       },
-      fail: function(res) {
+      fail: function (res) {
         let obj = {
           'type': mDeviceEvent.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT,
           'result': false,
@@ -773,14 +774,14 @@ function init() {
     })
   })
 
-  mDeviceEvent.listenSendRouterSsidAndPassword(true, function(options) {
+  mDeviceEvent.listenSendRouterSsidAndPassword(true, function (options) {
     self.data.password = options.password
     self.data.ssid = options.ssid
     writeDeviceRouterInfoStart(self.data.deviceId, self.data.service_uuid, self.data.characteristic_write_uuid, null);
   })
 
 
-  mDeviceEvent.listenSendCustomData(true, function(options) {
+  mDeviceEvent.listenSendCustomData(true, function (options) {
     self.data.customData = options.customData
     writeCutomsData(self.data.deviceId, self.data.service_uuid, self.data.characteristic_write_uuid, null);
   })