From 1847d74de27290cc1facc3dfef89bc6262467200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E8=99=9E=E6=B8=A0=E6=88=90=E2=80=9D?= <“yuqucheng2006@qq.com”> Date: Mon, 6 Jul 2026 15:40:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E5=A4=9A=E9=A1=B9?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E8=BF=AD=E4=BB=A3=E4=B8=8E=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 新增WiFi连接UI预制体与多语言文案 2. 重构BLE通信通知处理逻辑 3. 调整数据库总工作时长类型为float 4. 更新项目与Android构建配置 5. 主页添加消杀数据上传进度提示 --- Assets/Editor/iOSWiFiBuildPostProcessor.cs | 38 +++++++++++++++-- Assets/Plugins/Android/mainTemplate.gradle | 20 ++++----- .../Plugins/Android/settingsTemplate.gradle | 2 +- Assets/Res/language/language.json | 5 +++ .../选择wifi.prefab | 0 .../选择wifi.prefab.meta | 0 .../Bluetooth/BLECommunicationManager.cs | 41 ++++++++++++------- Assets/Scripts/Managers/DataBase.cs | 2 +- .../Scripts/UI/Pages/HomePage/HomePageCtrl.cs | 5 +++ ProjectSettings/ProjectSettings.asset | 4 +- 10 files changed, 85 insertions(+), 32 deletions(-) rename Assets/Res/ui/{deviceinfopage => common}/选择wifi.prefab (100%) rename Assets/Res/ui/{deviceinfopage => common}/选择wifi.prefab.meta (100%) diff --git a/Assets/Editor/iOSWiFiBuildPostProcessor.cs b/Assets/Editor/iOSWiFiBuildPostProcessor.cs index 8f8463a..e2fde88 100644 --- a/Assets/Editor/iOSWiFiBuildPostProcessor.cs +++ b/Assets/Editor/iOSWiFiBuildPostProcessor.cs @@ -6,18 +6,21 @@ using System.IO; public class iOSWiFiBuildPostProcessor { + private const string WIFI_INFO_ENTITLEMENT = "com.apple.developer.networking.wifi-info"; + [PostProcessBuild] public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject) { if (target != BuildTarget.iOS) return; + // ===== 1. 配置 Info.plist ===== string plistPath = Path.Combine(pathToBuiltProject, "Info.plist"); PlistDocument plist = new PlistDocument(); plist.ReadFromString(File.ReadAllText(plistPath)); PlistElementDict rootDict = plist.root; - // 添加位置权限描述(iOS 获取 WiFi 需要) + // 添加位置权限描述(iOS 13+ 获取 WiFi 需要) rootDict.SetString("NSLocationWhenInUseUsageDescription", "需要位置权限来获取当前 WiFi 信息"); // 添加蓝牙权限描述 @@ -30,15 +33,44 @@ public class iOSWiFiBuildPostProcessor bgModes.AddString("bluetooth-central"); File.WriteAllText(plistPath, plist.WriteToString()); - - // 添加 CoreBluetooth.framework + + // ===== 2. 配置 Xcode 项目 ===== string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject); PBXProject proj = new PBXProject(); proj.ReadFromString(File.ReadAllText(projPath)); string targetGuid = proj.GetUnityMainTargetGuid(); proj.AddFrameworkToProject(targetGuid, "CoreBluetooth.framework", false); + + // ===== 3. 添加 Access WiFi Information 权限(iOS 14+ 必需)===== + string entitlementsFileName = "app.entitlements"; + string entitlementsPath = Path.Combine(pathToBuiltProject, entitlementsFileName); + + // 创建或读取 entitlements 文件 + PlistDocument entitlements = new PlistDocument(); + if (File.Exists(entitlementsPath)) + { + entitlements.ReadFromString(File.ReadAllText(entitlementsPath)); + } + else + { + entitlements.root.SetString("aps-environment", "development"); + } + // 添加 WiFi 信息访问权限(iOS 14+) + entitlements.root.SetBoolean(WIFI_INFO_ENTITLEMENT, true); + File.WriteAllText(entitlementsPath, entitlements.WriteToString()); + + // 设置 entitlements 文件到 Xcode 项目 + proj.SetBuildProperty(targetGuid, "CODE_SIGN_ENTITLEMENTS", entitlementsFileName); + + // 同时设置 framework target + string frameworkTargetGuid = proj.GetUnityFrameworkTargetGuid(); + if (!string.IsNullOrEmpty(frameworkTargetGuid)) + { + proj.SetBuildProperty(frameworkTargetGuid, "CODE_SIGN_ENTITLEMENTS", entitlementsFileName); + } + File.WriteAllText(projPath, proj.WriteToString()); } } diff --git a/Assets/Plugins/Android/mainTemplate.gradle b/Assets/Plugins/Android/mainTemplate.gradle index e9453e1..17c6379 100644 --- a/Assets/Plugins/Android/mainTemplate.gradle +++ b/Assets/Plugins/Android/mainTemplate.gradle @@ -4,16 +4,16 @@ apply plugin: 'com.android.library' dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) // Android Resolver Dependencies Start - implementation 'com.google.android.gms:play-services-base:18.10.0' // Assets/Firebase/Editor/DatabaseDependencies.xml:20 - // implementation 'com.google.firebase:firebase-analytics:23.0.0' // Assets/Firebase/Editor/AnalyticsDependencies.xml:13 - implementation 'com.google.firebase:firebase-analytics:23.2.0' // Assets/Firebase/Editor/DatabaseDependencies.xml:18 - implementation 'com.google.firebase:firebase-analytics-unity:13.5.0' // Assets/Firebase/Editor/AnalyticsDependencies.xml:18 - implementation 'com.google.firebase:firebase-app-unity:13.12.0' // Assets/Firebase/Editor/AppDependencies.xml:25 - implementation 'com.google.firebase:firebase-auth:24.1.0' // Assets/Firebase/Editor/AuthDependencies.xml:16 - implementation 'com.google.firebase:firebase-auth-unity:13.12.0' // Assets/Firebase/Editor/AuthDependencies.xml:23 - implementation 'com.google.firebase:firebase-common:22.0.1' // Assets/Firebase/Editor/AppDependencies.xml:16 - implementation 'com.google.firebase:firebase-database:22.0.1' // Assets/Firebase/Editor/DatabaseDependencies.xml:16 - implementation 'com.google.firebase:firebase-database-unity:13.12.0' // Assets/Firebase/Editor/DatabaseDependencies.xml:25 + implementation 'com.google.android.gms:play-services-base:18.10.0' // Assets/Firebase/Editor/DatabaseDependencies.xml:17 + // implementation 'com.google.firebase:firebase-analytics:23.0.0' // Assets/Firebase/Editor/AnalyticsDependencies.xml:11 + implementation 'com.google.firebase:firebase-analytics:23.2.0' // Assets/Firebase/Editor/DatabaseDependencies.xml:15 + implementation 'com.google.firebase:firebase-analytics-unity:13.5.0' // Assets/Firebase/Editor/AnalyticsDependencies.xml:16 + implementation 'com.google.firebase:firebase-app-unity:13.12.0' // Assets/Firebase/Editor/AppDependencies.xml:22 + implementation 'com.google.firebase:firebase-auth:24.1.0' // Assets/Firebase/Editor/AuthDependencies.xml:13 + implementation 'com.google.firebase:firebase-auth-unity:13.12.0' // Assets/Firebase/Editor/AuthDependencies.xml:20 + implementation 'com.google.firebase:firebase-common:22.0.1' // Assets/Firebase/Editor/AppDependencies.xml:13 + implementation 'com.google.firebase:firebase-database:22.0.1' // Assets/Firebase/Editor/DatabaseDependencies.xml:13 + implementation 'com.google.firebase:firebase-database-unity:13.12.0' // Assets/Firebase/Editor/DatabaseDependencies.xml:22 // Android Resolver Dependencies End **DEPS**} diff --git a/Assets/Plugins/Android/settingsTemplate.gradle b/Assets/Plugins/Android/settingsTemplate.gradle index 8399e55..3fe251e 100644 --- a/Assets/Plugins/Android/settingsTemplate.gradle +++ b/Assets/Plugins/Android/settingsTemplate.gradle @@ -19,7 +19,7 @@ dependencyResolutionManagement { // Android Resolver Repos Start def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/") maven { - url (unityProjectPath + "/Assets/GeneratedLocalRepo/Firebase/m2repository") // Assets/Firebase/Editor/AnalyticsDependencies.xml:18, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/AuthDependencies.xml:23, Assets/Firebase/Editor/DatabaseDependencies.xml:25 + url (unityProjectPath + "/Assets/GeneratedLocalRepo/Firebase/m2repository") // Assets/Firebase/Editor/AnalyticsDependencies.xml:16, Assets/Firebase/Editor/AppDependencies.xml:22, Assets/Firebase/Editor/AuthDependencies.xml:20, Assets/Firebase/Editor/DatabaseDependencies.xml:22 } mavenLocal() // Android Resolver Repos End diff --git a/Assets/Res/language/language.json b/Assets/Res/language/language.json index 22777fb..50d1831 100644 --- a/Assets/Res/language/language.json +++ b/Assets/Res/language/language.json @@ -1508,6 +1508,11 @@ "key": "100302", "zh": "恢复出厂设置失败,请稍后重试", "en": "Reset failed, please try again later" + }, + { + "key": "100303", + "zh": "获取到{0}条消杀数据,正在上传,请稍后", + "en": "Uploading {0} elimination records, please wait..." } diff --git a/Assets/Res/ui/deviceinfopage/选择wifi.prefab b/Assets/Res/ui/common/选择wifi.prefab similarity index 100% rename from Assets/Res/ui/deviceinfopage/选择wifi.prefab rename to Assets/Res/ui/common/选择wifi.prefab diff --git a/Assets/Res/ui/deviceinfopage/选择wifi.prefab.meta b/Assets/Res/ui/common/选择wifi.prefab.meta similarity index 100% rename from Assets/Res/ui/deviceinfopage/选择wifi.prefab.meta rename to Assets/Res/ui/common/选择wifi.prefab.meta diff --git a/Assets/Scripts/Bluetooth/BLECommunicationManager.cs b/Assets/Scripts/Bluetooth/BLECommunicationManager.cs index 8350c6c..c90539b 100644 --- a/Assets/Scripts/Bluetooth/BLECommunicationManager.cs +++ b/Assets/Scripts/Bluetooth/BLECommunicationManager.cs @@ -50,7 +50,6 @@ namespace Kill.Bluetooth public event Action OnStatisticsDataReceived; // 收到统计数据 public event Action OnSensorDataReceived; // 收到传感器数据 public event Action OnMosquitoDataReceived; // 收到蚊虫数据通知 - public event Action OnDataReceived; // 收到原始数据 public event Action OnRawDataReceived; // 收到原始数据(十六进制字符串) public event Action OnRawDataSent; // 发送原始数据(十六进制字符串) public event Action OnCommunicationError; // 通信错误 @@ -1954,7 +1953,6 @@ namespace Kill.Bluetooth if (success) { mosquitoDataCount=(int)count; - OnDataReceived+=HandleMosquitoDataNotification; Log($"蚊虫数据请求成功,期望接收 {count} 条数据"); } @@ -1965,15 +1963,12 @@ namespace Kill.Bluetooth } public int mosquitoDataCount=0; /// - /// 处理蚊虫数据通知 (通知类型 0x0B) + /// 处理蚊虫数据通知 /// - public void HandleMosquitoDataNotification(byte[] notifyData) + private void HandleMosquitoDataNotification(byte[] notifyData) { if(mosquitoDataCount<=0) - { - OnDataReceived-=HandleMosquitoDataNotification; return; - } if (notifyData == null || notifyData.Length < 20) { LogError("蚊虫数据通知格式错误,数据长度不足20字节"); @@ -2421,10 +2416,15 @@ namespace Kill.Bluetooth private void HandleReceivedFrame(BLEFrame frame) { bool wasWaitingResponse = IsWaitingResponse; - - // 检查是否是对等待命令的响应 - if (IsWaitingResponse && frame.Command == LastCommand) + + // 优先处理设备主动通知,避免被等待响应的流程拦截 + if (frame.ReadWrite == BLEConstants.RW_NOTIFY) { + HandleNotification(frame.Command, frame.Data); + } + else if (IsWaitingResponse && frame.Command == LastCommand) + { + // 匹配到等待的响应 IsWaitingResponse = false; _responseTimer = 0; @@ -2432,11 +2432,6 @@ namespace Kill.Bluetooth _pendingCallback?.Invoke(response); _pendingCallback = null; } - else if (frame.ReadWrite == BLEConstants.RW_NOTIFY) - { - // 处理设备主动通知 - OnDataReceived?.Invoke(frame.Data); - } else if (IsWaitingResponse) { // 尝试处理非匹配的响应,可能是硬件响应延迟 @@ -2455,6 +2450,22 @@ namespace Kill.Bluetooth } } + /// + /// 通用的设备通知分发器,按命令类型路由到对应处理逻辑 + /// + private void HandleNotification(byte command, byte[] data) + { + switch (command) + { + case BLEConstants.NOTIFY_MOSQUITO_DATA: + HandleMosquitoDataNotification(data); + break; + default: + Log($"未处理的设备通知 0x{command:X2}"); + break; + } + } + /// /// 响应超时处理 /// diff --git a/Assets/Scripts/Managers/DataBase.cs b/Assets/Scripts/Managers/DataBase.cs index 9af9211..b271272 100644 --- a/Assets/Scripts/Managers/DataBase.cs +++ b/Assets/Scripts/Managers/DataBase.cs @@ -270,7 +270,7 @@ namespace Kill.Managers public string bind_time; public string device_model; public string firmware_version; - public int? total_work_hours; + public float? total_work_hours; public int? total_mosquitoes_killed; public string location; public string last_online_time; diff --git a/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs b/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs index d582c9e..8fb201e 100644 --- a/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs +++ b/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs @@ -1400,6 +1400,8 @@ namespace Kill.UI.Pages Coroutine waitToPostMosquitoDatas=null; private void OnMosquitoDataReceived(MosquitoData mosquitoData) { + if(mosquitoDatas==null) + return; DateTime now=DateTime.Now; mosquitoDatas.Add(mosquitoData); if(waitToPostMosquitoDatas!=null) @@ -1469,6 +1471,9 @@ namespace Kill.UI.Pages { if (success) { + string tip=LanguageManager.Instance.GetLanguage("100303"); + tip=tip.Replace("{0}",count.ToString()); + ToastUI.ShowText(tip); Debug.Log($"已请求 {count.Count} 条蚊虫数据,等待接收..."); mosquitoDatas=new List(); aimMosquitoDataCount=(int)count.Count; diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index c057d03..5068649 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -13,7 +13,7 @@ PlayerSettings: useOnDemandResources: 0 accelerometerFrequency: 100 companyName: photonmatrix - productName: photonmatrix + productName: Photon Matrix defaultCursor: {fileID: 0} cursorHotspot: {x: 0, y: 0} m_SplashScreenBackgroundColor: {r: 0.08627451, g: 0.08627451, b: 0.08627451, a: 1} @@ -176,7 +176,7 @@ PlayerSettings: VisionOS: 0 iPhone: 0 tvOS: 0 - overrideDefaultApplicationIdentifier: 0 + overrideDefaultApplicationIdentifier: 1 AndroidBundleVersionCode: 1 AndroidMinSdkVersion: 33 AndroidTargetSdkVersion: 0