WifiUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.jld.vod.utils;
  2. import android.content.Context;
  3. import android.net.DhcpInfo;
  4. import android.net.wifi.WifiInfo;
  5. import android.net.wifi.WifiManager;
  6. import java.lang.reflect.Method;
  7. import java.net.InetAddress;
  8. import java.net.InterfaceAddress;
  9. import java.net.NetworkInterface;
  10. import java.util.Enumeration;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. /**
  14. * Created by melo on 2017/9/23.
  15. */
  16. public class WifiUtil {
  17. private static final String TAG = "LocationUtils";
  18. private static volatile WifiUtil instance = null;
  19. private WifiManager mWifiManager;
  20. private Context mContext;
  21. private WifiUtil(Context context) {
  22. mContext = context;
  23. mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  24. }
  25. public static WifiUtil getInstance(Context context) {
  26. if (instance == null) {
  27. synchronized (WifiUtil.class) {
  28. if (instance == null) {
  29. instance = new WifiUtil(context);
  30. }
  31. }
  32. }
  33. return instance;
  34. }
  35. public boolean isWifiApEnabled() {
  36. try {
  37. Method method = mWifiManager.getClass().getMethod("isWifiApEnabled");
  38. method.setAccessible(true);
  39. return (Boolean) method.invoke(mWifiManager);
  40. } catch (NoSuchMethodException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. return false;
  47. }
  48. public String getLocalIPAddress() {
  49. WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
  50. return intToIp(wifiInfo.getIpAddress());
  51. }
  52. public String getServerIPAddress() {
  53. DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
  54. return intToIp(mDhcpInfo.gateway);
  55. }
  56. private static String intToIp(int i) {
  57. return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."
  58. + ((i >> 24) & 0xFF);
  59. }
  60. /**
  61. * @return 优先获取网卡地址
  62. */
  63. public static String getBroadcastAddress() {
  64. // p2p
  65. String broadcast = getBroadcastAddress("eth0");
  66. if (broadcast == null) {
  67. return getBroadcastAddress("wlan0");
  68. }
  69. return broadcast;
  70. }
  71. /**
  72. * @param netCardName 网卡名称
  73. * @return 获取的广播地址
  74. */
  75. public static String getBroadcastAddress(String netCardName) {
  76. try {
  77. Enumeration<NetworkInterface> eni = NetworkInterface
  78. .getNetworkInterfaces();
  79. while (eni.hasMoreElements()) {
  80. NetworkInterface networkCard = eni.nextElement();
  81. if (networkCard.getDisplayName().startsWith(netCardName)) {
  82. List<InterfaceAddress> ncAddrList = networkCard
  83. .getInterfaceAddresses();
  84. Iterator<InterfaceAddress> ncAddrIterator = ncAddrList.iterator();
  85. while (ncAddrIterator.hasNext()) {
  86. InterfaceAddress networkCardAddress = ncAddrIterator.next();
  87. InetAddress address = networkCardAddress.getAddress();
  88. if (!address.isLoopbackAddress()) {
  89. String hostAddress = address.getHostAddress();
  90. if (hostAddress.indexOf(":") > 0) {
  91. // case : ipv6
  92. continue;
  93. } else {
  94. // case : ipv4
  95. String broadcastAddress = networkCardAddress.getBroadcast().getHostAddress();
  96. return broadcastAddress;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. return null;
  106. }
  107. }