NetStatus.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using SC.XR.Unity;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Net.NetworkInformation;
  6. using System.Net.Sockets;
  7. using System.Runtime.CompilerServices;
  8. using UnityEngine;
  9. using UnityEngine.Events;
  10. namespace SC.XR.Unity
  11. {
  12. public class NetStatus : MonoBehaviour
  13. {
  14. private static NetStatus mInstant;
  15. public static NetStatus getInstance {
  16. get {
  17. return mInstant;
  18. }
  19. }
  20. AndroidJavaObject jo;
  21. string m_ReachabilityText = "";
  22. [HideInInspector]
  23. public string netSpeedStr = "";
  24. string netIP;
  25. string netmac;
  26. string netStatus = "";
  27. WIFISTREGTH wifiStrength = WIFISTREGTH.WIFI_NULL;
  28. int strength = -2;
  29. WIFISTREGTH _WIFIStrength;
  30. private event Action<NetworkReachability, NetworkReachability> NetWorkChangeCallBack;
  31. private NetworkReachability preNetWorkStatus = NetworkReachability.NotReachable;
  32. private void Awake()
  33. {
  34. if (mInstant != null)
  35. {
  36. DestroyImmediate(gameObject);
  37. return;
  38. }
  39. mInstant = this;
  40. DontDestroyOnLoad(gameObject);
  41. }
  42. private void Start()
  43. {
  44. jo = new AndroidJavaObject("com.example.netstatus.NetState");
  45. jo.Call("NetStart");
  46. StartCoroutine(GetNetSpeedAndstatus());
  47. SCGetNetStatus();
  48. preNetWorkStatus = Application.internetReachability;
  49. }
  50. void Update()
  51. {
  52. if (preNetWorkStatus != Application.internetReachability)
  53. {
  54. if (NetWorkChangeCallBack != null)
  55. {
  56. NetWorkChangeCallBack(preNetWorkStatus, Application.internetReachability);
  57. }
  58. preNetWorkStatus = Application.internetReachability;
  59. }
  60. }
  61. private void OnDestroy()
  62. {
  63. StopCoroutine(GetNetSpeedAndstatus());
  64. jo = null;
  65. m_ReachabilityText = null;
  66. netmac = null;
  67. netIP = null;
  68. netSpeedStr = null;
  69. }
  70. /// <summary>
  71. /// 注册网络状态变化时触发事件,第一个参数为变化前的网络状态
  72. /// 第二个参数为变化后的网络状态
  73. /// </summary>
  74. /// <param name="callback"></param>
  75. public void RegisterNetWorkChangeCallBack(Action<NetworkReachability, NetworkReachability> callback)
  76. {
  77. NetWorkChangeCallBack += callback;
  78. }
  79. public string SCGetNetStatus()//获取网络状态
  80. {
  81. //Check if the device cannot reach the internet
  82. if (Application.internetReachability == NetworkReachability.NotReachable)//网络无法连接
  83. {
  84. //Change the Text
  85. m_ReachabilityText = "NoNetWork";
  86. }
  87. //Check if the device can reach the internet via a carrier data network
  88. else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)//网络可通过由运营商数据网络访问
  89. {
  90. m_ReachabilityText = "3G/4GNetWork";
  91. }
  92. //Check if the device can reach the internet via a LAN
  93. else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)//网络可通过WIFI或电缆访问
  94. {
  95. m_ReachabilityText = "WifiOrCableNetWork";
  96. }
  97. //Output the network reachability to the console window
  98. // Debug.Log("Internet : " + m_ReachabilityText);
  99. return m_ReachabilityText;
  100. }
  101. public WIFISTREGTH SCGetWifiStrength()
  102. {
  103. strength = jo.Call<int>("GetWifiStrength", AndroidPluginBase.CurrentActivity);
  104. if (strength == 0)
  105. {
  106. _WIFIStrength = WIFISTREGTH.WIFI_NULL;
  107. }
  108. else if (strength == 1)
  109. {
  110. _WIFIStrength = WIFISTREGTH.WIFI_LOW;
  111. }
  112. else if (strength == 2)
  113. {
  114. _WIFIStrength = WIFISTREGTH.WIFI_MIDDLE;
  115. }
  116. else if (strength == 3)
  117. {
  118. _WIFIStrength = WIFISTREGTH.WIFI_HIGH;
  119. }
  120. else if (strength == 4)
  121. {
  122. _WIFIStrength = WIFISTREGTH.WIFI_HIGH;
  123. }
  124. else if (strength == -1)
  125. {
  126. _WIFIStrength = WIFISTREGTH.WIFI_UNKNOWN;
  127. }
  128. return _WIFIStrength;
  129. }
  130. public string SCGetMacAddress()//获取Mac地址
  131. {
  132. string physicalAddress = "";
  133. NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();
  134. foreach (NetworkInterface adaper in nice)
  135. {
  136. if (adaper.Description == "en0")
  137. {
  138. physicalAddress = adaper.GetPhysicalAddress().ToString();
  139. break;
  140. }
  141. else
  142. {
  143. physicalAddress = adaper.GetPhysicalAddress().ToString();
  144. if (physicalAddress != "")
  145. {
  146. break;
  147. };
  148. }
  149. }
  150. return physicalAddress;
  151. }
  152. public string SCGetIP()//获取本机IP地址
  153. {
  154. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  155. foreach (NetworkInterface adater in adapters)
  156. {
  157. if (adater.Supports(NetworkInterfaceComponent.IPv4))
  158. {
  159. UnicastIPAddressInformationCollection UniCast = adater.GetIPProperties().UnicastAddresses;
  160. if (UniCast.Count > 0)
  161. {
  162. foreach (UnicastIPAddressInformation uni in UniCast)
  163. {
  164. if (uni.Address.AddressFamily == AddressFamily.InterNetwork)
  165. {
  166. return uni.Address.ToString();
  167. }
  168. }
  169. }
  170. }
  171. }
  172. return null;
  173. }
  174. public string SCGetNetSpeed()
  175. {
  176. return jo.Call<string>("GetNetSpeed");
  177. }
  178. public string SCGetNetWorkInfo()//文本显示
  179. {
  180. return "本机IP地址为:" + netIP + "\n Mac地址为:" + netmac + "\n 当前网络状态为:" + netStatus
  181. + "\n 当前网速为:" + netSpeedStr + "\n 当前Wifi信号强度为:" + wifiStrength.ToString();
  182. }
  183. private IEnumerator GetNetSpeedAndstatus()//每经过1秒执行一次
  184. {
  185. while (true)
  186. {
  187. yield return new WaitForSecondsRealtime(1f);
  188. SCGetNetStatus();
  189. netIP = SCGetIP();
  190. netmac = SCGetMacAddress();
  191. netStatus = SCGetNetStatus();
  192. netSpeedStr = SCGetNetSpeed();
  193. wifiStrength = SCGetWifiStrength();
  194. }
  195. }
  196. public bool SCIsConnectWifi()
  197. {
  198. if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
  199. {
  200. return true;
  201. }
  202. else
  203. {
  204. return false;
  205. }
  206. }
  207. }
  208. }