当前位置:   article > 正文

Android Studio WiFi 之 获取 WiFi 名称、IP、Mac_android studio 获取wifi名称

android studio 获取wifi名称

Android 获取连接的WIFI 的一些数据(后期封装成aar 包给Unity 调用)

封装成AAR包,获取 Android 7.0 以上的 手机 mac 地址

一、Android 代码

  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.net.ConnectivityManager;
  4. import android.net.NetworkInfo;
  5. import android.net.wifi.WifiInfo;
  6. import android.net.wifi.WifiManager;
  7. import android.text.format.Formatter;
  8. import java.net.InetAddress;
  9. import java.net.NetworkInterface;
  10. import java.net.SocketException;
  11. import java.util.Enumeration;
  12. import static android.content.Context.WIFI_SERVICE;
  13. public class WifiMgr {
  14. /*
  15. *获取Mac地址
  16. *
  17. * android 7.0 以上使用*/
  18. public static String getMacAddress() {
  19. String strMacAddr = null;
  20. try {
  21. // 获得IpD地址
  22. InetAddress ip = getLocalInetAddress();
  23. byte[] b = NetworkInterface.getByInetAddress(ip)
  24. .getHardwareAddress();
  25. StringBuffer buffer = new StringBuffer();
  26. for (int i = 0; i < b.length; i++) {
  27. if (i != 0) {
  28. buffer.append(':');
  29. }
  30. String str = Integer.toHexString(b[i] & 0xFF);
  31. buffer.append(str.length() == 1 ? 0 + str : str);
  32. }
  33. strMacAddr = buffer.toString().toUpperCase();
  34. } catch (Exception e) {
  35. }
  36. return strMacAddr;
  37. }
  38. /**
  39. * 获取移动设备本地IP
  40. *
  41. * @return
  42. */
  43. private static InetAddress getLocalInetAddress() {
  44. InetAddress ip = null;
  45. try {
  46. // 列举
  47. Enumeration<NetworkInterface> en_netInterface = NetworkInterface
  48. .getNetworkInterfaces();
  49. while (en_netInterface.hasMoreElements()) {// 是否还有元素
  50. NetworkInterface ni = (NetworkInterface) en_netInterface
  51. .nextElement();// 得到下一个元素
  52. Enumeration<InetAddress> en_ip = ni.getInetAddresses();// 得到一个ip地址的列举
  53. while (en_ip.hasMoreElements()) {
  54. ip = en_ip.nextElement();
  55. if (!ip.isLoopbackAddress()
  56. && ip.getHostAddress().indexOf(":") == -1)
  57. break;
  58. else
  59. ip = null;
  60. }
  61. if (ip != null) {
  62. break;
  63. }
  64. }
  65. } catch (SocketException e) {
  66. e.printStackTrace();
  67. }
  68. return ip;
  69. }

二、AndroidMainifest.xml 添加权限

  1. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  2. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  3. <uses-permission android:name="android.permission.INTERNET"/>

三、Unity 中调用

  1. using UnityEngine;
  2. /// <summary>
  3. /// 通过IP,获取机器Mac地址
  4. /// </summary>
  5. public class GetWifiData
  6. {
  7. // 安卓的接口文档
  8. private AndroidJavaObject _androidJavaObject;
  9. public AndroidJavaObject mAndroidJavaObject {
  10. get {
  11. if (_androidJavaObject != null) {
  12. _androidJavaObject = new AndroidJavaObject("com.wifimacforarrow.pacificfuture.wifidataforarrow.WifiMgr");
  13. }
  14. return _androidJavaObject;
  15. }
  16. }
  17. /// <summary>
  18. /// 构造函数
  19. /// </summary>
  20. public GetWifiData() {
  21. _androidJavaObject = new AndroidJavaObject("com.wifimacforarrow.pacificfuture.wifidataforarrow.WifiMgr");
  22. }
  23. /// <summary>
  24. /// 获取设别的 Mac 地址
  25. /// </summary>
  26. /// <returns></returns>
  27. public string GetWifiMac() {
  28. #if UNITY_EDITOR
  29. return "00:00:00:00:00:00";
  30. #else
  31. return mAndroidJavaObject.CallStatic<string>("getMacAddress");
  32. #endif
  33. }
  34. }

 

 

 

 

=======================================================================================

=======================================================================================

Android Studio  WiFi 之 获取 WiFi 名称、IP、Mac(Android 7.0)

注意添加相关权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
  1. import android.content.Context;
  2. import android.net.ConnectivityManager;
  3. import android.net.NetworkInfo;
  4. import android.net.wifi.WifiInfo;
  5. import android.net.wifi.WifiManager;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.text.format.Formatter;
  9. import android.widget.TextView;
  10. import java.net.NetworkInterface;
  11. import java.net.SocketException;
  12. public class MainActivity extends AppCompatActivity {
  13. private TextView tv;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. // 获取 textView 组件,把结果显示在UI上
  19. tv = findViewById(R.id.tv);
  20. tv.setText("ip "+getWifiIp() + " " +getWiFiName()+ " " +getWifiMacAddress());
  21. }
  22. /*
  23. * 获取 WIFI 的名称
  24. * */
  25. public String getWiFiName() {
  26. WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
  27. if (wm != null) {
  28. WifiInfo winfo = wm.getConnectionInfo();
  29. if (winfo != null) {
  30. String s = winfo.getSSID();
  31. if (s.length() > 2 && s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"') {
  32. return s.substring(1, s.length() - 1);
  33. }
  34. }
  35. }
  36. return "Wifi 未获取到";
  37. }
  38. /*
  39. * 获取 WiFi 的 IP 地址
  40. * */
  41. public String getWifiIp() {
  42. Context myContext = getApplicationContext();
  43. if (myContext == null) {
  44. throw new NullPointerException("上下文 context is null");
  45. }
  46. WifiManager wifiMgr = (WifiManager) myContext.getSystemService(Context.WIFI_SERVICE);
  47. if (isWifiEnabled()) {
  48. int ipAsInt = wifiMgr.getConnectionInfo().getIpAddress();
  49. String ip = Formatter.formatIpAddress(ipAsInt);
  50. if (ipAsInt == 0) {
  51. return "未能获取到IP地址";
  52. } else {
  53. return ip;
  54. }
  55. } else {
  56. return "WiFi 未连接";
  57. }
  58. }
  59. /*
  60. * 判断当前 WIFI 是否连接
  61. * */
  62. public boolean isWifiEnabled() {
  63. Context myContext = getApplicationContext();
  64. if (myContext == null) {
  65. throw new NullPointerException("上下文 context is null");
  66. }
  67. WifiManager wifiMgr = (WifiManager) myContext.getSystemService(Context.WIFI_SERVICE);
  68. if (wifiMgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
  69. ConnectivityManager connManager = (ConnectivityManager) myContext
  70. .getSystemService(Context.CONNECTIVITY_SERVICE);
  71. NetworkInfo wifiInfo = connManager
  72. .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  73. return wifiInfo.isConnected();
  74. } else {
  75. return false;
  76. }
  77. }
  78. /*
  79. * 获取 WiFi 的 Mac 地址
  80. *
  81. * */
  82. public String getWifiMacAddress(){
  83. Context myContext = getApplicationContext();
  84. if (myContext == null) {
  85. throw new NullPointerException("上下文 context is null");
  86. }
  87. WifiManager wifiMgr = (WifiManager) myContext.getSystemService(Context.WIFI_SERVICE);
  88. if (isWifiEnabled()) {
  89. // 该接口只能获得 02:00:00:00:00:02
  90. //String macAddress = wifiMgr.getConnectionInfo().getMacAddress();
  91. String macAddress = null;
  92. StringBuffer buf = new StringBuffer();
  93. NetworkInterface networkInterface = null;
  94. try {
  95. networkInterface = NetworkInterface.getByName("eth1");
  96. if (networkInterface == null) {
  97. networkInterface = NetworkInterface.getByName("wlan0");
  98. }
  99. if (networkInterface == null) {
  100. return "02:00:00:00:00:02";
  101. }
  102. byte[] addr = networkInterface.getHardwareAddress();
  103. for (byte b : addr) {
  104. buf.append(String.format("%02X:", b));
  105. }
  106. if (buf.length() > 0) {
  107. buf.deleteCharAt(buf.length() - 1);
  108. }
  109. macAddress = buf.toString();
  110. } catch (SocketException e) {
  111. e.printStackTrace();
  112. return "02:00:00:00:00:02";
  113. }
  114. return macAddress;
  115. } else {
  116. return "WiFi 未连接";
  117. }
  118. }
  119. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/300752
推荐阅读
相关标签
  

闽ICP备14008679号