Prechádzať zdrojové kódy

feature: SR1蓝牙版本处理

Damon 8 mesiacov pred
rodič
commit
f1f1df6b9d

+ 14 - 3
.idea/workspace.xml

@@ -4,10 +4,13 @@
     <option name="autoReloadType" value="NONE" />
   </component>
   <component name="ChangeListManager">
-    <list default="true" id="420a40af-e4b9-41e7-b569-424cfc67e65d" name="Changes" comment="feature: 解决图片报错的问题">
+    <list default="true" id="420a40af-e4b9-41e7-b569-424cfc67e65d" name="Changes" comment="feature: 处理定位权限和搜索设备">
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/app.js" beforeDir="false" afterPath="$PROJECT_DIR$/app.js" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/devices/ble_manager.js" beforeDir="false" afterPath="$PROJECT_DIR$/devices/ble_manager.js" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/devices/bt_helper.js" beforeDir="false" afterPath="$PROJECT_DIR$/devices/bt_helper.js" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/pages/connectBle/connectBle.js" beforeDir="false" afterPath="$PROJECT_DIR$/pages/connectBle/connectBle.js" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/pages/deviceList/deviceList.js" beforeDir="false" afterPath="$PROJECT_DIR$/pages/deviceList/deviceList.js" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -186,7 +189,14 @@
       <option name="project" value="LOCAL" />
       <updated>1733127373473</updated>
     </task>
-    <option name="localTasksCounter" value="17" />
+    <task id="LOCAL-00017" summary="feature: 处理定位权限和搜索设备">
+      <created>1733132280507</created>
+      <option name="number" value="00017" />
+      <option name="presentableId" value="LOCAL-00017" />
+      <option name="project" value="LOCAL" />
+      <updated>1733132280507</updated>
+    </task>
+    <option name="localTasksCounter" value="18" />
     <servers />
   </component>
   <component name="Vcs.Log.History.Properties">
@@ -262,6 +272,7 @@
     <MESSAGE value="feature: 添加测试蓝牙类" />
     <MESSAGE value="feature: 处理外部类与内部类的问题" />
     <MESSAGE value="feature: 解决图片报错的问题" />
-    <option name="LAST_COMMIT_MESSAGE" value="feature: 解决图片报错的问题" />
+    <MESSAGE value="feature: 处理定位权限和搜索设备" />
+    <option name="LAST_COMMIT_MESSAGE" value="feature: 处理定位权限和搜索设备" />
   </component>
 </project>

+ 8 - 1
app.js

@@ -1,5 +1,7 @@
 // app.ts
-import { BtHelper } from './devices/bt_helper';
+import {
+  BtHelper
+} from './devices/bt_helper';
 import mqtt from './utils/mqtt';
 import update from './utils/update';
 import strings from './utils/strings';
@@ -24,6 +26,11 @@ App({
     is_debug: 2, // 1 测试环境 // 2正式环境
     client: null,
     oneInitBluetooth: true,
+
+    ///即将要连接的设备
+    connectWillDevice: {},
+
+
     mDeviceList: [],
     // 当前设备
     // device: {},

+ 40 - 45
devices/ble_manager.js

@@ -41,19 +41,22 @@ class bleManager {
   }
 
   // 开始搜索蓝牙设备
-  async startScan(success, fail) {
+  async startScan(callBack, succeed) {
     var that = this;
     var open = await that.getLocalSetting();
     if (!open) {
-      return false;
+      succeed(false);
+      return;
     }
+
     if (!that.isAvailable) {
       wx.showToast({
         title: '请打开蓝牙',
         icon: 'none',
         duration: 2000
       });
-      return false;
+      succeed(false);
+      return;
     }
 
     if (!that.hasPermission) {
@@ -62,40 +65,56 @@ class bleManager {
         icon: 'none',
         duration: 2000
       });
-      return false;
+      succeed(false);
+      return;
     }
 
+    // services: ["ffc0", "ab00", "ffe5"],
     wx.startBluetoothDevicesDiscovery({
       allowDuplicatesKey: false,
-      // services: ["ffc0", "ab00", "ffe5"],
       success: (res) => {
-        console.log('蓝牙设备搜索已启动:', res);
         if (res) {
           wx.onBluetoothDeviceFound((devicesRes) => {
-            devicesRes.devices.forEach(device => {
-              if (device.name) {
-                console.log("gadsfadfqwerqwerqr==aaa====" + device.name + "===" + device.deviceId);
+            for (var i = 0; i < devicesRes.devices.length; i++) {
+              if (devicesRes.devices[i].name === getApp().globalData.connectWillDevice.clientType) {
+                callBack(devicesRes.devices[i]);
+                break;
               }
-            })
+            }
           });
+        } else {
+          succeed(false);
         }
       },
       fail: (err) => {
-        if (fail) {
-          if (err.errno == 1509008) {
-            wx.showToast({
-              title: '搜索蓝牙需要先开启定位权限',
-              icon: 'none',
-              duration: 2000
-            });
-          }
-          fail(err);
+        if (err.errno == 1509008) {
+          wx.showToast({
+            title: '搜索蓝牙需要先开启定位权限',
+            icon: 'none',
+            duration: 2000
+          });
         }
-        console.log('启动蓝牙设备搜索失败', err ?? "err is null");
+        succeed(false);
       }
     });
   }
 
+  // 停止搜索
+  stopScan() {
+    return new Promise((resolve, reject) => {
+      wx.stopBluetoothDevicesDiscovery({
+        success: (res) => {
+          console.log('停止搜索成功:', res);
+          resolve(res);
+        },
+        fail: (err) => {
+          console.error('停止搜索失败:', err);
+          reject(new Error('停止搜索失败'));
+        }
+      });
+    });
+  }
+
   // 检查蓝牙权限
   async checkBluetoothPermission(callback) {
     // 获取蓝牙权限
@@ -148,7 +167,7 @@ class bleManager {
 
   closeBle() {
     console.log('关闭蓝牙了')
-    wx.closeBluetoothAdapter()
+    wx.closeBluetoothAdapter();
   }
 
   // 获取已连接的蓝牙设备
@@ -391,30 +410,6 @@ class bleManager {
     });
   }
 
-  // 停止搜索
-  stopScan() {
-    var that = this;
-    return new Promise((resolve, reject) => {
-      wx.stopBluetoothDevicesDiscovery({
-        success: (res) => {
-          console.log('停止搜索成功:', res);
-          resolve(res);
-        },
-        fail: (err) => {
-          console.error('停止搜索失败:', err);
-          reject(new Error('停止搜索失败'));
-        }
-      });
-    });
-  }
-  closeBle() {
-    wx.closeBluetoothAdapter({
-      success(res) {
-        console.log(res)
-      }
-    })
-  }
-
   // 发现服务
   discoverServices(deviceId) {
     var that = this;

+ 39 - 35
devices/bt_helper.js

@@ -42,10 +42,8 @@ class BtHelper {
   }
 
   static _instance = new BtHelper();
-
   static _isConnecting = false;
   static isDisConnectByOTA = false;
-
   // _helper;
 
   constructor() {
@@ -74,21 +72,26 @@ class BtHelper {
       if (device.connectType != 1 && device.state != "online") {
         return;
       }
-
       that.handleCommand(event, device);
-
     }, this)
   }
 
   ///搜索蓝牙设备
-  async search(stopCall) {
-    var that = this;
-    var res = await that.bleManager.startScan();
+  async search(callBack, connected) {
+    var that = this;
+    that.bleManager.startScan(
+      async function (c) {
+          callBack(c);
+        },
+        async function (c) {
+          connected(c);
+        },
+    );
     // console.log("gadsfadfqwerqwerqr==444====" + res);
     ///失败
-    if (!res) {
+    // if (!res) {
 
-    }
+    // }
     // that.timer = null;
     // that.timer = setTimeout(() => {
     //   that.stopSearch();
@@ -117,18 +120,18 @@ class BtHelper {
         mDevice.volume = volume;
         break;
 
-      ///电量
+        ///电量
       case EnumCmdEvent.battery:
         mDevice.kwh = event.kwh;
         break;
 
-      ///低时延模式  低时延模式开启 1:音乐 , 2: 游戏 , 3: movie
+        ///低时延模式  低时延模式开启 1:音乐 , 2: 游戏 , 3: movie
       case EnumCmdEvent.lowDelayMode:
         mDevice.lowDelayMode = event.lowDelayMode;
         mDevice.lowDelayModeOpen = event.lowDelayModeOpen;
         break;
 
-      ///电量 耳机电量
+        ///电量 耳机电量
       case EnumCmdEvent.batteryEarphone:
         mDevice.kwh = event.kwh;
         mDevice.kwhLeft = event.kwhLeft;
@@ -136,12 +139,12 @@ class BtHelper {
         mDevice.kwhBox = event.kwhBox;
         break;
 
-      ///是否支持TTS
+        ///是否支持TTS
       case EnumCmdEvent.enableTTS:
         mDevice.enableTTS = event.enableTTS;
         break;
 
-      ///切换设备连接模式 0:未知 1:低功耗蓝牙 2:wifi类型 3:经典蓝牙(不做任何操作) 4:4G类型
+        ///切换设备连接模式 0:未知 1:低功耗蓝牙 2:wifi类型 3:经典蓝牙(不做任何操作) 4:4G类型
       case EnumCmdEvent.switchDeviceMode:
         var deviceMode = event.deviceMode.index;
         if (deviceMode != null) {
@@ -150,14 +153,14 @@ class BtHelper {
         }
         break;
 
-      ///4G外插卡  4G虚拟卡 当前使用的sim卡
+        ///4G外插卡  4G虚拟卡 当前使用的sim卡
       case EnumCmdEvent.sim:
         mDevice.sim = event.sim;
         mDevice.eSim = event.eSim;
         mDevice.simIndex = event.simIndex;
         break;
 
-      ///闹钟是否开启  闹钟周期 闹钟唤醒时间
+        ///闹钟是否开启  闹钟周期 闹钟唤醒时间
       case EnumCmdEvent.wake:
         mDevice.wakeSwitch = event.wakeSwitch;
         mDevice.wakeCycle = event.wakeCycle;
@@ -165,14 +168,14 @@ class BtHelper {
         mDevice.wakeMinutes = event.wakeMinutes;
         break;
 
-      ///休眠是否开启 休眠时间
+        ///休眠是否开启 休眠时间
       case EnumCmdEvent.sleep:
         mDevice.sleepSwitch = event.sleepSwitch;
         mDevice.sleepHour = event.sleepHour;
         mDevice.sleepMinutes = event.sleepMinutes;
         break;
 
-      ///版本和型号
+        ///版本和型号
       case EnumCmdEvent.version:
         mDevice.version = event.version;
         var clientType = mDevice.clientType ?? "";
@@ -205,7 +208,7 @@ class BtHelper {
         // }
         break;
 
-      ///云小微授权
+        ///云小微授权
       case EnumCmdEvent.auth:
         var authInfo = event.authInfo;
         mDevice.authInfo = authInfo;
@@ -221,17 +224,17 @@ class BtHelper {
         // });
         break;
 
-      ///EQ音效
+        ///EQ音效
       case EnumCmdEvent.eq:
         mDevice.eqs = event.eqs;
         break;
 
-      ///payId 充流量使用
+        ///payId 充流量使用
       case EnumCmdEvent.payId:
         mDevice.payId = event.payId;
         break;
 
-      ///QQ音乐使用dsn授权
+        ///QQ音乐使用dsn授权
       case EnumCmdEvent.dsn:
         var dsn = event.dsn;
         mDevice.dsn = dsn;
@@ -240,13 +243,13 @@ class BtHelper {
         // ProviderUtil.twelvePublic.wifiDeviceConnected();
         break;
 
-      ///自动切换 0,1不能
-      ///
+        ///自动切换 0,1不能
+        ///
       case EnumCmdEvent.netModeAuto:
         mDevice.netModeAuto = event.netModeAuto;
         break;
 
-      ///解绑设备
+        ///解绑设备
       case EnumCmdEvent.unbind:
         //   let unbindAddress = event.item.address ?? "";
         //     if (unbindAddress != mDevice.address) {
@@ -262,7 +265,7 @@ class BtHelper {
 
         break;
 
-      ///解绑设备
+        ///解绑设备
       case EnumCmdEvent.ctrlStatus:
         // List < int > ctrlList = event.ctrlStatus;
         // if (ctrlList.length == 3) {
@@ -284,7 +287,7 @@ class BtHelper {
         notifyListeners();
         break;
 
-      ///设备信息
+        ///设备信息
       case EnumCmdEvent.getDeviceInfo:
         //   List list = [];
         //   String userId = ProviderUtil.user.userModel.uid ?? "";
@@ -346,12 +349,11 @@ class BtHelper {
   async _connectSuccess() {
     this.checkDevice()
     this.getDeviceInfo()
-
   }
 
   async connect(data, onChanged) {
     // await this._helper.connect({ data, onChanged, isClick });
-
+    var that = this;
     clearTimeout(this.timer);
     this.timer = null;
     this.bleManager.stopScan()
@@ -454,7 +456,6 @@ class BtHelper {
     var that = this;
     ///是否已打开蓝牙
     var adpter = await that.bleManager.initBluetoothAdapter((res) => {
-
       if (!res) {
         wx.showToast({
           title: '请开启蓝牙功能',
@@ -483,11 +484,6 @@ class BtHelper {
     });
   }
 
-  closeBle() {
-    var that = this;
-    that.bleManager.closeBle()
-  }
-
   async getConnectedDevices() {
     var that = this;
     try {
@@ -775,8 +771,16 @@ class BtHelper {
     var that = this;
     that.send(BtCmd.otaData(value));
   }
+
+  async closeBle() {
+    var that = this;
+    that.bleManager.closeBle();
+    await that.bleManager.stopScan();
+  }
+
 }
 
+
 // 导出 BtHelper 类
 // const btHelper = new BtHelper();
 

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 42 - 34
pages/connectBle/connectBle.js


+ 3 - 3
pages/deviceList/deviceList.js

@@ -43,9 +43,9 @@ Page({
       // console.log("设备列表", res);
       let devices = []
       res.forEach(element => {
-        if (element.applicationType.indexOf("0") >= 0) {
-          devices.push(element)
-        }
+        // if (element.applicationType.indexOf("0") >= 0) {
+        devices.push(element)
+        // }
       });
       console.log("设备列表2:", devices)
       app.globalData.classifyProducts = devices;