| | |
| | | import android.content.Context; |
| | | import android.net.wifi.WifiInfo; |
| | | import android.net.wifi.WifiManager; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | |
| | | import com.hdl.sdk.connect.config.HDLLinkConfig; |
| | | |
| | | import java.net.InetAddress; |
| | | import java.net.InterfaceAddress; |
| | |
| | | */ |
| | | public class IpUtils { |
| | | |
| | | /** |
| | | * @return 广播地址 |
| | | */ |
| | | public static String getBroadcastAddress() { |
| | | // /** |
| | | // * @return 广播地址 |
| | | // */ |
| | | /*public static String getBroadcastAddress() { |
| | | // try { |
| | | // for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); |
| | | // niEnum.hasMoreElements(); ) { |
| | |
| | | // } catch (SocketException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | return "255.255.255.255"; |
| | | }*/ |
| | | |
| | | |
| | | /** |
| | | * @return 广播地址 |
| | | */ |
| | | public static synchronized String getBroadcastAddress() { |
| | | try { |
| | | if (!TextUtils.isEmpty(HDLLinkConfig.getInstance().getNetworkName())) { |
| | | for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); |
| | | niEnum.hasMoreElements(); ) { |
| | | NetworkInterface ni = niEnum.nextElement(); |
| | | if (!ni.isLoopback() && ni.getName().equalsIgnoreCase(HDLLinkConfig.getInstance().getNetworkName())) { |
| | | for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) { |
| | | if (interfaceAddress.getBroadcast() != null) { |
| | | Log.d("TAG", "----->ni.getName()= " + ni.getName() + " ip= " + interfaceAddress.getBroadcast().toString().substring(1)); |
| | | return interfaceAddress.getBroadcast().toString().substring(1); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); |
| | | niEnum.hasMoreElements(); ) { |
| | | NetworkInterface ni = niEnum.nextElement(); |
| | | if (!ni.isLoopback()) { |
| | | for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) { |
| | | if (interfaceAddress.getBroadcast() != null) { |
| | | Log.d("TAG", "----->ip= " + interfaceAddress.getBroadcast().toString().substring(1)); |
| | | return interfaceAddress.getBroadcast().toString().substring(1); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } catch (SocketException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return "255.255.255.255"; |
| | | } |
| | | |
| | |
| | | ((i >> 16) & 0xFF) + "." + |
| | | (i >> 24 & 0xFF); |
| | | } |
| | | |
| | | private static String getWifiManagerAddress(Context context) { |
| | | try { |
| | | if (context != null) { |
| | | WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); |
| | | if (wifiManager != null && wifiManager.isWifiEnabled()) { |
| | | WifiInfo wifiInfo = wifiManager.getConnectionInfo(); |
| | | int ipAddress = wifiInfo.getIpAddress(); |
| | | return intToIp(ipAddress); |
| | | } |
| | | } |
| | | } catch (Exception ignored) { |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static String getWifiIpV4Address(Context context) { |
| | | try { |
| | | //直接从wifiManager获取 |
| | | String managerAddress = getWifiManagerAddress(context); |
| | | if (!TextUtils.isEmpty(managerAddress)) { |
| | | return managerAddress; |
| | | } |
| | | |
| | | Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); |
| | | while (enumeration.hasMoreElements()) { |
| | | NetworkInterface network = enumeration.nextElement(); |
| | | if (network == null || network.isVirtual() || !network.isUp() || network.isLoopback() || !network.getName().equalsIgnoreCase("eth0")) { |
| | | continue; |
| | | } else { |
| | | Enumeration<InetAddress> addresses = network.getInetAddresses(); |
| | | while (addresses.hasMoreElements()) { |
| | | InetAddress address = addresses.nextElement(); |
| | | if (!address.isLoopbackAddress() && !address.isAnyLocalAddress() |
| | | && !address.isLinkLocalAddress()) { |
| | | String hostAddress = address.getHostAddress(); |
| | | if (hostAddress != null) { |
| | | boolean isIPv4 = hostAddress.indexOf(':') < 0; |
| | | if (isIPv4) return hostAddress; |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | public static String getEth0IpV4Address() { |
| | | try { |
| | | Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); |
| | | while (enumeration.hasMoreElements()) { |
| | | NetworkInterface network = enumeration.nextElement(); |
| | | if (network == null || network.isVirtual() || !network.isUp() || network.isLoopback() || !network.getName().equalsIgnoreCase("eth0")) { |
| | | continue; |
| | | } else { |
| | | Enumeration<InetAddress> addresses = network.getInetAddresses(); |
| | | |
| | | while (addresses.hasMoreElements()) { |
| | | InetAddress address = addresses.nextElement(); |
| | | if (!address.isLoopbackAddress() && !address.isAnyLocalAddress() |
| | | && !address.isLinkLocalAddress()) { |
| | | String hostAddress = address.getHostAddress(); |
| | | if (hostAddress != null) { |
| | | boolean isIPv4 = hostAddress.indexOf(':') < 0; |
| | | if (isIPv4) return hostAddress; |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * @return 获取全部有效ip地址, 优先返回有线ip |
| | | */ |
| | | public static String getIpV4Address(Context context) { |
| | | |
| | | try { |
| | | String eth0IpV4Address = getEth0IpV4Address(); |
| | | if (!TextUtils.isEmpty(eth0IpV4Address)) { |
| | | return eth0IpV4Address; |
| | | } |
| | | String wifiIpV4Address = getWifiIpV4Address(context); |
| | | if (!TextUtils.isEmpty(wifiIpV4Address)) { |
| | | return wifiIpV4Address; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | } |