using System; using System.Net.NetworkInformation; using System.Net.Sockets; using UnityEngine; namespace SC.XR.Unity { public class NetStatus : SingletonMono { AndroidJavaObject jo; string m_ReachabilityText = ""; int strength = -2; WIFISTREGTH _WIFIStrength; private event Action NetWorkChangeCallBack; private NetworkReachability preNetWorkStatus = NetworkReachability.NotReachable; private void Start() { jo = new AndroidJavaObject("com.example.netstatus.NetState"); jo.Call("NetStart"); SCGetNetStatus(); preNetWorkStatus = Application.internetReachability; } void Update() { if (preNetWorkStatus != Application.internetReachability) { NetWorkChangeCallBack?.Invoke(preNetWorkStatus, Application.internetReachability); preNetWorkStatus = Application.internetReachability; } } private void OnDestroy() { jo = null; m_ReachabilityText = null; } /// /// 注册网络状态变化时触发事件,第一个参数为变化前的网络状态 /// 第二个参数为变化后的网络状态 /// /// public void RegisterNetWorkChangeCallBack(Action callback) { NetWorkChangeCallBack += callback; } public string SCGetNetStatus()//获取网络状态 { //Check if the device cannot reach the internet if (Application.internetReachability == NetworkReachability.NotReachable)//网络无法连接 { //Change the Text m_ReachabilityText = "NoNetWork"; } //Check if the device can reach the internet via a carrier data network else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)//网络可通过由运营商数据网络访问 { m_ReachabilityText = "3G/4GNetWork"; } //Check if the device can reach the internet via a LAN else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)//网络可通过WIFI或电缆访问 { m_ReachabilityText = "WifiOrCableNetWork"; } //Output the network reachability to the console window // Debug.Log("Internet : " + m_ReachabilityText); return m_ReachabilityText; } public WIFISTREGTH SCGetWifiStrength() { strength = jo.Call("GetWifiStrength", AndroidPluginBase.CurrentActivity); if (strength == 0) { _WIFIStrength = WIFISTREGTH.WIFI_NULL; } else if (strength == 1) { _WIFIStrength = WIFISTREGTH.WIFI_LOW; } else if (strength == 2) { _WIFIStrength = WIFISTREGTH.WIFI_MIDDLE; } else if (strength == 3) { _WIFIStrength = WIFISTREGTH.WIFI_HIGH; } else if (strength == 4) { _WIFIStrength = WIFISTREGTH.WIFI_HIGH; } else if (strength == -1) { _WIFIStrength = WIFISTREGTH.WIFI_UNKNOWN; } return _WIFIStrength; } public string SCGetMacAddress()//获取Mac地址 { string physicalAddress = ""; NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adaper in nice) { if (adaper.Description == "en0") { physicalAddress = adaper.GetPhysicalAddress().ToString(); break; } else { physicalAddress = adaper.GetPhysicalAddress().ToString(); if (physicalAddress != "") { break; }; } } return physicalAddress; } public string SCGetIP()//获取本机IP地址 { NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adater in adapters) { if (adater.Supports(NetworkInterfaceComponent.IPv4)) { UnicastIPAddressInformationCollection UniCast = adater.GetIPProperties().UnicastAddresses; if (UniCast.Count > 0) { foreach (UnicastIPAddressInformation uni in UniCast) { if (uni.Address.AddressFamily == AddressFamily.InterNetwork) { return uni.Address.ToString(); } } } } } return null; } public string SCGetNetSpeed() { return jo.Call("GetNetSpeed"); } public bool SCIsConnectWifi() { if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork) { return true; } else { return false; } } } }