瀏覽代碼

feature:修改多个设备的上下线,切换问题

zeng.chen 6 月之前
父節點
當前提交
16aa2529b4

+ 24 - 21
devices/ble_manager.js

@@ -104,14 +104,8 @@ class bleManager {
             if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
               temp.mac = temp.advertisData ? hex_util.buf2hex(temp.advertisData) : '';
               temp.mac2 = that.ab2hex(temp.advertisData ?? "")
-              // if (that.callBackConnect != null) {
-              // C8:96:D9:51:37:98
-              // 1A:B4:E0:40:22:8B
-              // if (that.callBackConnect != null) {
-              //  || temp.deviceId != "1A:B4:E0:40:22:8B"
-              // A3:7B:97:45:CC:22
-              // DA:D5:FB:DC:91:30
-              if ((temp.deviceId === "1A:B4:E0:40:22:8B")) {
+
+              if (that.testData(temp)) {
                 return
               }
               console.log("2222:", temp.mac, ":", temp.mac2, ":", temp.deviceId, temp, temp.connectable);
@@ -256,20 +250,19 @@ class bleManager {
                 console.log("搜索到的" + JSON.stringify(temp));
               }
               // if (that.callBackConnect != null) {
-              if (that.callBackConnect != null) {
-                if ((temp.deviceId === "1A:B4:E0:40:22:8B")) {
-                  return
-                }
-                console.log("444:" + JSON.stringify(temp));
+              if (that.testData(temp)) {
+                return
+              }
+              console.log("444:" + JSON.stringify(temp));
 
-                // if ((temp.deviceId === "07:F6:F4:66:FB:FA")) {
-                //   that.callBackConnect(temp);
-                // }
-                that.callBackConnect(temp);
+              // if ((temp.deviceId === "07:F6:F4:66:FB:FA")) {
+              //   that.callBackConnect(temp);
+              // }
+              that.callBackConnect(temp);
 
-              }
-              break;
             }
+            break;
+
           }
         } else {
           for (var i = 0; i < res.devices.length; i++) {
@@ -301,6 +294,15 @@ class bleManager {
     });
   }
 
+  testData(temp) {
+    if ((temp.deviceId === "1A:B4:E0:40:22:8B"
+      || temp.deviceId === "F3:55:1F:8D:26:A0"
+      || temp.deviceId === "3F2F112F-ACB5-3EB3-30DA-93DE6A86228A")) {
+      return true
+    }
+    return false
+  }
+
   // 开始搜索蓝牙设备
   async startScan(connectWillDevice, boolean, callBackConnect) {
     var that = this;
@@ -431,7 +433,8 @@ class bleManager {
   disconnect(mDevice) {
     var that = this;
     const strings = require('../utils/strings');
-    let device = that.publicDevice;
+    // 没有参数就默认断开现在连接的
+    let device = mDevice;
     if (mDevice != null) {
       device = mDevice;
     }
@@ -522,7 +525,7 @@ class bleManager {
       });
       // }
       // let logData = new Uint8Array(buffer);
-      console.log('开始发送数据:', buffer);
+      // console.log('开始发送数据:', buffer);
       wx.writeBLECharacteristicValue({
         deviceId: that.publicDevice.deviceId,
         serviceId: that.publicDevice.serviceId,

+ 0 - 1
devices/bluetooth/bt_cmd.js

@@ -298,7 +298,6 @@ class BtCmd {
                 result.splice(i, 0, pwdList[j++]);
             }
         } else {
-            p++
             result.splice(++p, 0, BtCmd.intToHex(pwdList.length));
         }
 

+ 3 - 4
devices/bt_helper.js

@@ -367,14 +367,14 @@ class BtHelper {
     this.getDeviceInfo()
   }
 
-  async connect(device, onChanged) {
+  async connect(device, onChanged, disPreDev) {
     var that = this;
     // todo 暂时不掉线
-    await that.disconnect(device);
+    await that.disconnect(disPreDev ? null : device);
     clearTimeout(that.timer);
     that.timer = null;
     that.bleManager.stopSearch()
-    console.log("停止搜索1");
+    console.log("停止搜索1",);
 
     var res = await that.bleManager.connectToDevice(device);
     if (res != true) {
@@ -408,7 +408,6 @@ class BtHelper {
       }
       return
     }
-    console.log('device特征值:', characteristics)
     let uuids = ["AB01", "FFC1"];
     for (let i = 0; i < characteristics.length; i++) {
       let charc = characteristics[i];

+ 76 - 4
pages/components/image-cropper/image-cropper.js

@@ -312,6 +312,7 @@ Component({
                     quality: this.data.quality,
                     canvasId: this.data.el,
                     success: (res) => {
+                        console.log("裁剪图片1", res.tempFilePath);
                         getCallback({
                             url: res.tempFilePath,
                             width: this.data.width * this.data.export_scale,
@@ -320,9 +321,10 @@ Component({
                     }
                 }, this)
             });
-        },  /**
-         * 返回图片数据data
-         */
+        },
+        /**
+       * 返回图片数据data
+       */
         getImgData(getCallback) {
             this._draw(() => {
                 wx.canvasGetImageData({
@@ -345,6 +347,74 @@ Component({
                 }, this)
             });
         },
+        convertToStandardRGB(getCallback) {
+            try {
+                this.getImgData((res) => {
+                    console.log("获取像素数据成功", res.data);
+                    const rgbData = this._drawRGB565(res.data, res.width, res.height);
+                    console.log("转换后的 RGB 数据", rgbData.length, rgbData.byteLength);
+
+                    // 将转换后的 RGB 数据绘制回 Canvas
+                    wx.canvasPutImageData({
+                        canvasId: this.data.el,
+                        data: rgbData,
+                        x: 0,
+                        y: 0,
+                        width: res.width,
+                        height: res.height,
+                        success: (res2) => {
+                            console.log("绘制 RGB 数据成功", res2);
+                            wx.canvasToTempFilePath({
+                                width: res.width,
+                                height: res.height,
+                                destWidth: res.width,
+                                destHeight: res.height,
+                                fileType: 'jpg',
+                                quality: this.data.quality,
+                                canvasId: this.data.el,
+                                success: (res) => {
+                                    console.log("转换后的图片路径", res.tempFilePath);
+                                    getCallback({
+                                        url: res.tempFilePath,
+                                        width: res.width,
+                                        height: res.height
+                                    });
+                                },
+                                fail: (err) => {
+                                    console.error('转换失败:', err);
+                                }
+                            }, this);
+                        },
+                        fail: (err) => {
+                            console.error('绘制 RGB 数据失败:', err);
+                        }
+                    }, this);
+                });
+            } catch (e) {
+                console.log(e)
+            }
+        },
+        // 将 16 位 RGB 565 数据绘制回 Canvas
+        _drawRGB565(data, width, height, callback) {
+            const ctx = wx.createCanvasContext(this.data.el, this);
+            const imageData = ctx.createImageData(width, height);
+            console.log("转换前的 RGB 数据", data.length, data.byteLength);
+            for (let i = 0, j = 0; i < data.length; i++) {
+                const pixel = data[i];
+                const r = (pixel >> 11) & 0x1F;
+                const g = (pixel >> 5) & 0x3F;
+                const b = pixel & 0x1F;
+
+                imageData.data[j++] = (r << 3) | (r >> 2); // R
+                imageData.data[j++] = (g << 2) | (g >> 4); // G
+                imageData.data[j++] = (b << 3) | (b >> 2); // B
+                imageData.data[j++] = 255; // Alpha 设置为 255,确保没有透明通道
+            }
+            console.log("转换后的 RGB 数据", imageData.length, imageData.byteLength);
+
+            ctx.putImageData(imageData, 0, 0);
+            ctx.draw(false, callback);
+        },
         /**
          * 设置图片动画
          * {
@@ -553,6 +623,7 @@ Component({
         _initCanvas() {
             //初始化canvas
             if (!this.data.ctx) {
+                console.log("创建canvas0");
                 this.data.ctx = wx.createCanvasContext("image-cropper", this);
             }
         },
@@ -869,10 +940,11 @@ Component({
                         height: Math.round(this.data.height * this.data.export_scale),
                         destWidth: this.data.width * this.data.export_scale,
                         destHeight: Math.round(this.data.height) * this.data.export_scale,
-                        fileType: 'png',
+                        fileType: 'jpg',
                         quality: this.data.quality,
                         canvasId: this.data.el,
                         success: (res) => {
+                            console.log("裁剪图片", res.tempFilePath);
                             this.triggerEvent('tapcut', {
                                 url: res.tempFilePath,
                                 width: this.data.width * this.data.export_scale,

+ 1 - 1
pages/connectBle/connectBle.js

@@ -84,7 +84,7 @@ Page({
         getCurrentPages()[0].addBlueDevice(device);
         route_util.goBackRoute(route_constant.indexRoot);
       }
-    });
+    }, true);
   },
 
   ///连接设备按钮

+ 87 - 23
pages/index/index.js

@@ -244,6 +244,15 @@ Page({
   },
   startBleTimer(isFirst) {
     let that = this;
+
+
+    let deviceList = that.getDeviceList()
+    let findWifi = deviceList.find(item => {
+      return item.connectType != 1
+    })
+    if (findWifi) {
+      return;
+    }
     ///再秒再对比一次
     that.stopIntervalId1();
     let count = 0;
@@ -254,7 +263,8 @@ Page({
       // if (count > 4) {
       if (isFirst) {
         that.stopIntervalId1();
-        that.startBleTimer(false);
+        var newIsFirst = false
+        that.startBleTimer(newIsFirst);
       }
       // } else {
       //   count++;
@@ -291,15 +301,16 @@ Page({
     if (v) {
       var device = that.data._willConnectBle;
       console.log("开始连接设备0", device);
-      if (device.state === 'online' || device.connectType != 1) {
-        return;
-      }
-      // });
+      // if (device.state === 'online' || device.connectType != 1) {
+      //   return;
+      // }
+
       BtHelper.getInstance().connect(device, function (isConnected, d) {
+        console.log("自动连接成功", isConnected)
         if (isConnected) {
           device.state = 'online';
           that.addBlueDevice(device);
-          // that.stopIntervalId1();
+          that.stopIntervalId1();
         } else {
           device.state = 'offline';
         }
@@ -583,6 +594,8 @@ Page({
       if (index === deviceListSelect) {
         that.goWake();
       } else {
+        // 现断线第一个
+        // that.disconnectFirst()
         that.addWifiDevice(device);
       }
       return;
@@ -602,9 +615,11 @@ Page({
           deviceListSelect: null,
         });
         var currentDevice = deviceList[deviceListSelect];
-        await BtHelper.getInstance().disconnect(currentDevice);
-        // await time_util.delayMills(500);
+        console.log("断开连接0", currentDevice)
+        // 现断线第一个
+        // that.disconnectFirst()
       }
+      console.log("断开连接2", device)
 
       device.name = device.devName;
       BtHelper.getInstance().connect(device, function (isConnected, d) {
@@ -621,7 +636,11 @@ Page({
           deviceListSelect: null,
         });
         var currentDevice = deviceList[deviceListSelect];
-        await BtHelper.getInstance().disconnect(currentDevice);
+        if (currentDevice.connectType === 1) {
+          console.log("断开连接1", currentDevice)
+          await BtHelper.getInstance().disconnect(currentDevice);
+
+        }
         // await time_util.delayMills(500);
       }
 
@@ -635,6 +654,22 @@ Page({
     }
   },
 
+  disconnectFirst() {
+    let that = this;
+    let deviceList = that.getDeviceList()
+    let first = deviceList[0] ?? {}
+    if (first.state == "online") {
+      if (first.connectType === 1) {
+        BtHelper.getInstance().disconnect(first)
+        first.state == "offline"
+      } else {
+        // 取消订阅
+        // var pubResponse = lexin_util.getResponseByDeviceId(first.deviceId);
+        // app.unsubscribe(pubResponse);
+      }
+    }
+  },
+
   // 去唤醒界面
   goWake() {
     var that = this;
@@ -661,7 +696,7 @@ Page({
           if (deviceList[index].connectType == 3) {
             that.cancelWifi(index, false);
           } else {
-            console.log("确定删除", index)
+            console.log("确定删除0", index, deviceList[index].deviceId)
             that.cancelBlue(index, false);
           }
         }
@@ -712,18 +747,18 @@ Page({
 
     ///是否删除的当前播放这个
     var isCurrentIndex = false;
-    deviceListSelect = deviceListSelect ?? 0;
     if (deviceListSelect != null && deviceList.length > deviceListSelect) {
       isCurrentIndex = (deviceListSelect == index);
     };
 
+    let device = deviceList[index];
     ///删除当前设备
-    var deviceId = deviceList[index].deviceId;
+    var deviceId = device.deviceId;
     deviceList = deviceList.filter((item, i) => {
       return deviceId !== item.deviceId;
     });
     store.setStore("deviceList", deviceList);
-
+    console.log("确定删除1", isCurrentIndex, deviceListSelect)
     var indexPage = that.data.indexPage;
     that.setData({
       deviceList: deviceList,
@@ -732,10 +767,12 @@ Page({
     });
 
     console.log("最终确定删除", deviceId, JSON.stringify(deviceList), deviceList.length)
+    if (device.state === "online") {
+      BtHelper.getInstance().disconnect({
+        "deviceId": deviceId
+      });
+    }
 
-    BtHelper.getInstance().disconnect({
-      "deviceId": deviceId
-    });
   },
 
   /// 断开连接wifi
@@ -782,11 +819,21 @@ Page({
   addBlueDevice(device) {
     var that = this;
     var addDeviceList = lexin_add.addBlueDevice(device);
+    let hasOnline = 0
+    addDeviceList.forEach(item => {
+      // 蓝牙设备下线。wifi不动
+      if (item.deviceId != device.deviceId && item.connectType === 1) {
+        item.state = "offline"
+      }
+    })
+
+    console.log("新添加蓝牙设备", addDeviceList)
     that.updateDeviceList(addDeviceList, false, false);
     that.setData({
       deviceListSelect: 0,
       autoConnected: true,
     });
+
     that.addNotification()
   },
 
@@ -794,8 +841,9 @@ Page({
     let that = this;
     event_bus.removeNotification(CmdEvent.eventName, that);
     event_bus.addNotification(CmdEvent.eventName, function (event) {
+      let deviceList = that.getDeviceList()
+
       if (event.cmdEvent == EnumCmdEvent.btMac) {
-        let deviceList = that.getDeviceList()
         let disDevice = deviceList.find(item => {
           return item.state == "online" && item.connectType === 1
         })
@@ -807,31 +855,43 @@ Page({
         let isFirst = false;
         // 有下线就开启监听扫描
         if (event.commonValue == "online") {
+          console.log("有蓝牙设备上线")
           that.stopIntervalId1();
         } else {
-          that.startBleTimer(isFirst);
+          let disDevice = deviceList.find(item => {
+            return item.state == "online" && item.connectType === 3
+          })
+          if (!disDevice) {
+            that.startBleTimer(isFirst);
+          }
         }
 
-        console.log("首页ble通知:", event.deviceId)
-        let disDevice = that.data.deviceList.find(item => {
-          return item.deviceId == event.deviceId
+        console.log("首页ble通知:", event.deviceId, event.commonValue)
+        let disDevice = deviceList.find(item => {
+          return item.deviceId == event.deviceId && item.connectType == 1
         })
         // that.data.deviceList.forEach(element => {
         //   if (item.deviceId == tmp.deviceId) {
         //   }
         // });
         if (!disDevice) {
+          console.log("没有在线的")
           return;
         }
-        console.log("首页设备状态变化", disDevice)
         disDevice.state = event.commonValue;
+        console.log("首页设备状态变化", disDevice.state, disDevice.deviceId)
+
+        let hasOnline = deviceList.find(item => {
+          return item.state == "online" && item.connectType == 1
+        })
+        console.log("发现有在线的", hasOnline)
 
         if (event.commonValue == "online") {
           // BtHelper.getInstance().connect()
           console.log("首页上线连接", disDevice)
         } else {
           console.log("首页断开连接", disDevice)
-          that.updateDeviceList(that.data.deviceList, false, false);
+          that.updateDeviceList(deviceList, false, false);
         }
       }
     }, that);
@@ -848,9 +908,11 @@ Page({
       }, 500);
       return;
     };
+    console.log("有新的wifi设备0", device)
 
     var deviceId = device.deviceId;
     var addDeviceList = lexin_add.addWifiDevice(device);
+    console.log("有新的wifi设备", addDeviceList)
     that.updateDeviceList(addDeviceList, false, false);
 
     var indexPage = that.data.indexPage;
@@ -864,7 +926,9 @@ Page({
 
   refreshCurrentDevice(device) {
     var that = this;
+    console.log("刷新列表0:", device)
     var addDeviceList = lexin_add.addWifiDevice(device);
+    console.log("刷新列表1:", addDeviceList)
     that.updateDeviceList(addDeviceList, false, false);
     that.setData({
       autoConnected: true,

+ 1 - 0
pages/ota/ota.js

@@ -516,6 +516,7 @@ Page({
     this.addNotification()
   },
   onUnload() {
+    console.log("移除OTA通知")
     EventManager.removeNotification(CmdEvent.eventName, this)
   },
 

+ 2 - 2
pages/setWifi/setWifi.js

@@ -139,9 +139,9 @@ Page({
   sendWiFiInfo(wifiName, pwd) {
     // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
 
-    if (!wifiName) {
+    if (!wifiName || !pwd) {
       wx.showToast({
-        title: '请输入正确的账号',
+        title: '请输入正确的账号密码',
       })
       wx.hideLoading()
       return;

+ 4 - 4
utils/event_bus.js

@@ -23,9 +23,9 @@ var isDebug = true;
 function addNotification(name, selector, observer) {
     if (name && selector) {
         if (!observer) {
-            console.log("addNotification Warning: no observer will can't remove notice");
+            // console.log("addNotification Warning: no observer will can't remove notice");
         }
-        console.log("addNotification:" + name);
+        // console.log("addNotification:" + name);
         var newNotice = {
             name: name,
             selector: selector,
@@ -35,7 +35,7 @@ function addNotification(name, selector, observer) {
         addNotices(newNotice);
 
     } else {
-        console.log("addNotification error: no selector or name");
+        // console.log("addNotification error: no selector or name");
     }
 }
 
@@ -93,7 +93,7 @@ function addNotices(newNotice) {
  */
 
 function removeNotification(name, observer) {
-    console.log("removeNotification:" + name);
+    // console.log("removeNotification:" + name);
     for (var i = 0; i < __notices.length; i++) {
         var notice = __notices[i];
         if (notice.name === name) {

文件差異過大導致無法顯示
+ 7 - 1
utils/lexin/message.js


+ 16 - 4
utils/lexin/scan.js

@@ -1,8 +1,13 @@
+const {
+  BtHelper
+} = require('../../devices/bt_helper.js');
+
 module.exports = {
   compareList: compareList,
   compareList2: compareList2,
 }
 
+var deviceListIndex = 0
 async function compareList2(deviceList, callback) {
   let isHasWifi = deviceList.find(item => {
     return item.connectType == 3;
@@ -34,12 +39,16 @@ async function compareList2(deviceList, callback) {
     return;
   }
 
-  let index = 0;
-  _startScan(index, deviceList, callback);
+  callback(deviceList[deviceListIndex])
+  deviceListIndex++
+
+  // _startScan(deviceListIndex, deviceList, callback);
 }
 
 function _startScan(index, deviceList, callback) {
+  console.log("_startScan", index);
   if (index >= deviceList.length) {
+    deviceListIndex = 0
     callback()
     return
   }
@@ -52,13 +61,16 @@ function _startScan(index, deviceList, callback) {
         element = deviceList[index];
         // _startScan(index, deviceList, callback);
       } else {
+        deviceListIndex = 0
         // callback()
         // return
       }
     }
   }, function (res) {
-    console.log("自动连接上的", res);
-    callback(res)
+    deviceListIndex = 0
+    console.log("自动连接上的", element.deviceId, res.deviceId);
+    BtHelper.getInstance().stopSearch();
+    callback(element)
   });
 }