1. 新增iOSWiFiHelper类实现异步获取WiFi SSID,自动请求位置权限 2. 新增iOS权限描述本地化处理器,实现中英文权限文案自动切换 3. 更新所有iOS权限描述为更清晰的英文/中文说明 4. 优化iOS编译后处理脚本优先级,避免被其他插件覆盖 5. 在连接设备页面和WiFi设置页面自动填充当前WiFi名称 6. 更新应用版本号与构建号
198 lines
6.6 KiB
C#
198 lines
6.6 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Kill.Bluetooth;
|
||
using Kill.Core;
|
||
using Kill.Managers;
|
||
using Kill.UI.Components;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
namespace Kill.UI.Pages
|
||
{
|
||
public class SetWifiPage : MonoBehaviour
|
||
{
|
||
public InputField ssidInput;
|
||
public InputField passwordInput;
|
||
public Button confirmButton;
|
||
public Button backButton;
|
||
Action<string> callback;
|
||
int comefrom=0;
|
||
public void Init(Action<string> wifiInfoCallback,int comefrom=0)
|
||
{
|
||
this.comefrom=comefrom;
|
||
callback = wifiInfoCallback;
|
||
UIManager.Instance.RegisterBackAction(Back);
|
||
backButton.onClick.RemoveAllListeners();
|
||
backButton.onClick.AddListener(Back);
|
||
confirmButton.onClick.RemoveAllListeners();
|
||
confirmButton.onClick.AddListener(OnWifiConfirmClick);
|
||
#if UNITY_IOS && !UNITY_EDITOR
|
||
// iOS:异步请求位置权限,授权后自动填入 WiFi 名称(无需重新打开页面)
|
||
ssidInput.text = "";
|
||
iOSWiFiHelper.RequestWiFiSSID((ssid) =>
|
||
{
|
||
if (!string.IsNullOrEmpty(ssid))
|
||
{
|
||
ssidInput.text = ssid;
|
||
CheckWifiNameAndPassword();
|
||
}
|
||
});
|
||
#else
|
||
string wifiName = GetCurrentWiFiName();
|
||
ssidInput.text = wifiName ?? "";
|
||
#endif
|
||
passwordInput.text = "";
|
||
CheckWifiNameAndPassword();
|
||
ssidInput.onEndEdit.RemoveAllListeners();
|
||
ssidInput.onEndEdit.AddListener(delegate { CheckWifiNameAndPassword(); });
|
||
passwordInput.onEndEdit.RemoveAllListeners();
|
||
passwordInput.onEndEdit.AddListener(delegate { CheckWifiNameAndPassword(); });
|
||
}
|
||
public void Back()
|
||
{
|
||
if(comefrom==0)
|
||
UIManager.Instance.RegisterBackAction(GetComponentInParent<DeviceInfoPage>().Back);
|
||
else
|
||
UIManager.Instance.ClearBackAction();
|
||
Destroy(gameObject);
|
||
}
|
||
|
||
public void CheckWifiNameAndPassword()
|
||
{
|
||
string ssid = ssidInput.text.Trim();
|
||
string password = passwordInput.text;
|
||
if (string.IsNullOrEmpty(ssid) || string.IsNullOrEmpty(password))
|
||
{
|
||
confirmButton.gameObject.SetActive(false);
|
||
}
|
||
else
|
||
{
|
||
confirmButton.gameObject.SetActive(true);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 确认配置 WiFi
|
||
/// </summary>
|
||
public void OnWifiConfirmClick()
|
||
{
|
||
// WiFi模式下不支持设置WiFi
|
||
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
|
||
{
|
||
ToastUI.Show("100293");
|
||
return;
|
||
}
|
||
|
||
string ssid = ssidInput.text.Trim();
|
||
string password = passwordInput.text;
|
||
|
||
if (string.IsNullOrEmpty(ssid))
|
||
{
|
||
ToastUI.ShowText(LanguageManager.Instance.GetLanguage("100082") ?? "请输入WiFi名称");
|
||
return;
|
||
}
|
||
|
||
// 创建 WiFi 配置
|
||
var wifiControl = new Bluetooth.Protocol.WIFIControl
|
||
{
|
||
Enable = true,
|
||
SSID = ssid,
|
||
Password = password
|
||
};
|
||
|
||
LoadingUI.Show();
|
||
|
||
// 发送 WiFi 配置到设备
|
||
BLECommunicationManager.Instance.WriteWIFIControl(wifiControl, (success) =>
|
||
{
|
||
Loom.QueueOnMainThread(() =>
|
||
{
|
||
LoadingUI.Hide();
|
||
if (success)
|
||
{
|
||
ToastUI.ShowText(LanguageManager.Instance.GetLanguage("100222") ?? "WiFi配置成功");
|
||
Back();
|
||
callback?.Invoke(ssid);
|
||
}
|
||
else
|
||
{
|
||
ToastUI.ShowText(LanguageManager.Instance.GetLanguage("100083") ?? "WiFi配置失败");
|
||
}
|
||
});
|
||
});
|
||
}
|
||
/// <summary>
|
||
/// 获取当前连接的 WiFi 名称
|
||
/// </summary>
|
||
private string GetCurrentWiFiName()
|
||
{
|
||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||
try
|
||
{
|
||
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
||
using (AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
|
||
using (AndroidJavaObject wifiManager = currentActivity.Call<AndroidJavaObject>("getSystemService", "wifi"))
|
||
{
|
||
if (wifiManager == null) return null;
|
||
|
||
using (AndroidJavaObject wifiInfo = wifiManager.Call<AndroidJavaObject>("getConnectionInfo"))
|
||
{
|
||
if (wifiInfo == null) return null;
|
||
|
||
string ssid = wifiInfo.Call<string>("getSSID");
|
||
if (!string.IsNullOrEmpty(ssid) && ssid != "<unknown ssid>")
|
||
{
|
||
return ssid.Trim('"');
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
Debug.LogError($"获取 WiFi 名称失败: {ex.Message}");
|
||
}
|
||
return null;
|
||
#elif UNITY_IOS && !UNITY_EDITOR
|
||
// iOS 调用原生插件
|
||
return GetWiFiNameIOS();
|
||
#else
|
||
return "Editor_WiFi"; // 编辑器返回模拟值
|
||
#endif
|
||
}
|
||
|
||
#if UNITY_IOS && !UNITY_EDITOR
|
||
[System.Runtime.InteropServices.DllImport("__Internal")]
|
||
private static extern System.IntPtr GetWiFiSSID();
|
||
|
||
[System.Runtime.InteropServices.DllImport("__Internal")]
|
||
private static extern void RequestLocationPermission();
|
||
|
||
/// <summary>
|
||
/// iOS 获取 WiFi 名称(先请求位置权限)
|
||
/// </summary>
|
||
private string GetWiFiNameIOS()
|
||
{
|
||
try
|
||
{
|
||
// iOS 14+ NEHotspotNetwork 需要位置权限
|
||
RequestLocationPermission();
|
||
|
||
System.IntPtr ptr = GetWiFiSSID();
|
||
if (ptr == System.IntPtr.Zero)
|
||
{
|
||
return null;
|
||
}
|
||
string ssid = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(ptr);
|
||
// 释放内存
|
||
System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr);
|
||
return ssid;
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
Debug.LogError($"iOS 获取 WiFi 名称失败: {ex.Message}");
|
||
return null;
|
||
}
|
||
}
|
||
#endif
|
||
}
|
||
}
|