123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
-
- using System;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- using UnityEngine;
- namespace SC.XR.Unity
- {
- public class NetStatus : SingletonMono<NetStatus>
- {
- AndroidJavaObject jo;
- string m_ReachabilityText = "";
-
- int strength = -2;
- WIFISTREGTH _WIFIStrength;
- private event Action<NetworkReachability, NetworkReachability> 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;
- }
- /// <summary>
- /// 注册网络状态变化时触发事件,第一个参数为变化前的网络状态
- /// 第二个参数为变化后的网络状态
- /// </summary>
- /// <param name="callback"></param>
- public void RegisterNetWorkChangeCallBack(Action<NetworkReachability, NetworkReachability> 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<int>("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<string>("GetNetSpeed");
- }
- public bool SCIsConnectWifi()
- {
- if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
|