NetStatus.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 
  2. using System;
  3. using System.Net.NetworkInformation;
  4. using System.Net.Sockets;
  5. using UnityEngine;
  6. namespace SC.XR.Unity
  7. {
  8. public class NetStatus : SingletonMono<NetStatus>
  9. {
  10. AndroidJavaObject jo;
  11. string m_ReachabilityText = "";
  12. int strength = -2;
  13. WIFISTREGTH _WIFIStrength;
  14. private event Action<NetworkReachability, NetworkReachability> NetWorkChangeCallBack;
  15. private NetworkReachability preNetWorkStatus = NetworkReachability.NotReachable;
  16. private void Start()
  17. {
  18. jo = new AndroidJavaObject("com.example.netstatus.NetState");
  19. jo.Call("NetStart");
  20. SCGetNetStatus();
  21. preNetWorkStatus = Application.internetReachability;
  22. }
  23. void Update()
  24. {
  25. if (preNetWorkStatus != Application.internetReachability)
  26. {
  27. NetWorkChangeCallBack?.Invoke(preNetWorkStatus, Application.internetReachability);
  28. preNetWorkStatus = Application.internetReachability;
  29. }
  30. }
  31. private void OnDestroy()
  32. {
  33. jo = null;
  34. m_ReachabilityText = null;
  35. }
  36. /// <summary>
  37. /// 注册网络状态变化时触发事件,第一个参数为变化前的网络状态
  38. /// 第二个参数为变化后的网络状态
  39. /// </summary>
  40. /// <param name="callback"></param>
  41. public void RegisterNetWorkChangeCallBack(Action<NetworkReachability, NetworkReachability> callback)
  42. {
  43. NetWorkChangeCallBack += callback;
  44. }
  45. public string SCGetNetStatus()//获取网络状态
  46. {
  47. //Check if the device cannot reach the internet
  48. if (Application.internetReachability == NetworkReachability.NotReachable)//网络无法连接
  49. {
  50. //Change the Text
  51. m_ReachabilityText = "NoNetWork";
  52. }
  53. //Check if the device can reach the internet via a carrier data network
  54. else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)//网络可通过由运营商数据网络访问
  55. {
  56. m_ReachabilityText = "3G/4GNetWork";
  57. }
  58. //Check if the device can reach the internet via a LAN
  59. else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)//网络可通过WIFI或电缆访问
  60. {
  61. m_ReachabilityText = "WifiOrCableNetWork";
  62. }
  63. //Output the network reachability to the console window
  64. // Debug.Log("Internet : " + m_ReachabilityText);
  65. return m_ReachabilityText;
  66. }
  67. public WIFISTREGTH SCGetWifiStrength()
  68. {
  69. strength = jo.Call<int>("GetWifiStrength", AndroidPluginBase.CurrentActivity);
  70. if (strength == 0)
  71. {
  72. _WIFIStrength = WIFISTREGTH.WIFI_NULL;
  73. }
  74. else if (strength == 1)
  75. {
  76. _WIFIStrength = WIFISTREGTH.WIFI_LOW;
  77. }
  78. else if (strength == 2)
  79. {
  80. _WIFIStrength = WIFISTREGTH.WIFI_MIDDLE;
  81. }
  82. else if (strength == 3)
  83. {
  84. _WIFIStrength = WIFISTREGTH.WIFI_HIGH;
  85. }
  86. else if (strength == 4)
  87. {
  88. _WIFIStrength = WIFISTREGTH.WIFI_HIGH;
  89. }
  90. else if (strength == -1)
  91. {
  92. _WIFIStrength = WIFISTREGTH.WIFI_UNKNOWN;
  93. }
  94. return _WIFIStrength;
  95. }
  96. public string SCGetMacAddress()//获取Mac地址
  97. {
  98. string physicalAddress = "";
  99. NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();
  100. foreach (NetworkInterface adaper in nice)
  101. {
  102. if (adaper.Description == "en0")
  103. {
  104. physicalAddress = adaper.GetPhysicalAddress().ToString();
  105. break;
  106. }
  107. else
  108. {
  109. physicalAddress = adaper.GetPhysicalAddress().ToString();
  110. if (physicalAddress != "")
  111. {
  112. break;
  113. };
  114. }
  115. }
  116. return physicalAddress;
  117. }
  118. public string SCGetIP()//获取本机IP地址
  119. {
  120. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  121. foreach (NetworkInterface adater in adapters)
  122. {
  123. if (adater.Supports(NetworkInterfaceComponent.IPv4))
  124. {
  125. UnicastIPAddressInformationCollection UniCast = adater.GetIPProperties().UnicastAddresses;
  126. if (UniCast.Count > 0)
  127. {
  128. foreach (UnicastIPAddressInformation uni in UniCast)
  129. {
  130. if (uni.Address.AddressFamily == AddressFamily.InterNetwork)
  131. {
  132. return uni.Address.ToString();
  133. }
  134. }
  135. }
  136. }
  137. }
  138. return null;
  139. }
  140. public string SCGetNetSpeed()
  141. {
  142. return jo.Call<string>("GetNetSpeed");
  143. }
  144. public bool SCIsConnectWifi()
  145. {
  146. if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
  147. {
  148. return true;
  149. }
  150. else
  151. {
  152. return false;
  153. }
  154. }
  155. }
  156. }