|
|
|
@ -10,6 +10,7 @@ import android.net.ConnectivityManager; |
|
|
|
import android.net.NetworkInfo; |
|
|
|
import android.net.wifi.WifiInfo; |
|
|
|
import android.net.wifi.WifiManager; |
|
|
|
import android.os.Build; |
|
|
|
import android.os.Environment; |
|
|
|
import android.os.StatFs; |
|
|
|
import android.util.DisplayMetrics; |
|
|
|
@ -19,6 +20,7 @@ import java.io.File; |
|
|
|
import java.net.Inet4Address; |
|
|
|
import java.net.InetAddress; |
|
|
|
import java.net.NetworkInterface; |
|
|
|
import java.net.Socket; |
|
|
|
import java.net.SocketException; |
|
|
|
import java.util.Enumeration; |
|
|
|
import java.util.List; |
|
|
|
@ -45,7 +47,11 @@ public class DeviceUtil { |
|
|
|
try { |
|
|
|
if(Constant.androidBoardType.equals("nova")){ |
|
|
|
//诺瓦
|
|
|
|
return getEthIpAddress(); |
|
|
|
String ip = getEthIpAddress(); |
|
|
|
if(StringUtil.isEmpty(ip)){ |
|
|
|
ip = getLocalIPByWIFI(); |
|
|
|
} |
|
|
|
return ip; |
|
|
|
}else { |
|
|
|
String allIP = ""; |
|
|
|
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { |
|
|
|
@ -92,7 +98,7 @@ public class DeviceUtil { |
|
|
|
//获取有线网络ip
|
|
|
|
public static String getEthIpAddress() { |
|
|
|
String infaceName = "eth0"; |
|
|
|
String ip = "0.0.0.0"; |
|
|
|
String ip = ""; |
|
|
|
try { |
|
|
|
Enumeration<NetworkInterface> netInterface = NetworkInterface.getNetworkInterfaces(); |
|
|
|
while (netInterface.hasMoreElements()) { |
|
|
|
@ -100,12 +106,10 @@ public class DeviceUtil { |
|
|
|
if (!inface.isUp()) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// eth0 有线网络判断
|
|
|
|
if (!infaceName.equals(inface.getDisplayName())) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
Enumeration<InetAddress> netAddressList = inface.getInetAddresses(); |
|
|
|
while (netAddressList.hasMoreElements()) { |
|
|
|
InetAddress inetAddress = netAddressList.nextElement(); |
|
|
|
@ -136,27 +140,36 @@ public class DeviceUtil { |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static String getLocalIPByWIFI() { |
|
|
|
String ip=""; |
|
|
|
try { |
|
|
|
//获取wifi服务
|
|
|
|
WifiManager wifiManager = (WifiManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.WIFI_SERVICE); |
|
|
|
//判断wifi是否开启
|
|
|
|
if (!wifiManager.isWifiEnabled()) wifiManager.setWifiEnabled(true); |
|
|
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); |
|
|
|
int ipAddress = wifiInfo.getIpAddress(); |
|
|
|
return intToIp(ipAddress); |
|
|
|
} catch (Throwable t) { |
|
|
|
LoggerUtil.e("getLocalIPByWIFI", StringUtil.getThrowableStr(t)); |
|
|
|
return ""; |
|
|
|
Socket socket = new Socket("www.baidu.com",80); |
|
|
|
ip = socket.getLocalAddress().toString(); |
|
|
|
LoggerUtil.e("GETIP: ", ip); |
|
|
|
} catch (Exception e) { |
|
|
|
Log.e(TAG, "网络接口遍历获取IP失败", e); |
|
|
|
} |
|
|
|
if("".equals(ip)){ |
|
|
|
try { |
|
|
|
//获取wifi服务
|
|
|
|
WifiManager wifiManager = (WifiManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.WIFI_SERVICE); |
|
|
|
//判断wifi是否开启
|
|
|
|
if (!wifiManager.isWifiEnabled()) wifiManager.setWifiEnabled(true); |
|
|
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); |
|
|
|
int ipAddress = wifiInfo.getIpAddress(); |
|
|
|
ip = intToIp(ipAddress); |
|
|
|
} catch (Throwable t) { |
|
|
|
LoggerUtil.e("getLocalIPByWIFI", StringUtil.getThrowableStr(t)); |
|
|
|
return ""; |
|
|
|
} |
|
|
|
} |
|
|
|
return ip; |
|
|
|
} |
|
|
|
|
|
|
|
private static String intToIp(int i) { |
|
|
|
|
|
|
|
String first = String.valueOf((i & 0xFF)); |
|
|
|
String second = String.valueOf(((i >> 8) & 0xFF)); |
|
|
|
String third = String.valueOf(((i >> 16) & 0xFF)); |
|
|
|
String fourth = String.valueOf((i >> 24 & 0xFF)); |
|
|
|
|
|
|
|
return StringUtil.strSplice(first, ".", second, ".", third, ".", fourth); |
|
|
|
} |
|
|
|
|
|
|
|
@ -195,23 +208,38 @@ public class DeviceUtil { |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static String getLocalMacAddressFromIp() { |
|
|
|
String strMacAddr = null; |
|
|
|
String macAddress=""; |
|
|
|
WifiManager wifiManager = (WifiManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.WIFI_SERVICE); |
|
|
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); |
|
|
|
macAddress = wifiInfo.getBSSID(); |
|
|
|
if(StringUtil.isEmpty(macAddress)){ |
|
|
|
macAddress = getMacFromNetworkInterface(); |
|
|
|
} |
|
|
|
return macAddress; |
|
|
|
} |
|
|
|
/** |
|
|
|
* 通过网络接口获取MAC地址(备选方案) |
|
|
|
*/ |
|
|
|
private static String getMacFromNetworkInterface() { |
|
|
|
try { |
|
|
|
//获得IpD地址
|
|
|
|
InetAddress ip = getLocalInetAddress(); |
|
|
|
byte[] b = NetworkInterface.getByInetAddress(ip).getHardwareAddress(); |
|
|
|
StringBuffer buffer = new StringBuffer(); |
|
|
|
for (int i = 0; i < b.length; i++) { |
|
|
|
if (i != 0) { |
|
|
|
buffer.append(':'); |
|
|
|
} |
|
|
|
String str = Integer.toHexString(b[i] & 0xFF); |
|
|
|
buffer.append(str.length() == 1 ? 0 + str : str); |
|
|
|
java.net.NetworkInterface networkInterface = java.net.NetworkInterface.getByName("wlan0"); |
|
|
|
if (networkInterface == null) return ""; |
|
|
|
|
|
|
|
byte[] macBytes = networkInterface.getHardwareAddress(); |
|
|
|
if (macBytes == null) return ""; |
|
|
|
|
|
|
|
StringBuilder mac = new StringBuilder(); |
|
|
|
for (byte b : macBytes) { |
|
|
|
mac.append(String.format("%02X:", b)); |
|
|
|
} |
|
|
|
strMacAddr = buffer.toString().toUpperCase(); |
|
|
|
if (mac.length() > 0) { |
|
|
|
mac.deleteCharAt(mac.length() - 1); |
|
|
|
} |
|
|
|
return mac.toString(); |
|
|
|
} catch (Exception e) { |
|
|
|
Log.e(TAG, "通过网络接口获取MAC失败", e); |
|
|
|
return ""; |
|
|
|
} |
|
|
|
return strMacAddr; |
|
|
|
} |
|
|
|
/** |
|
|
|
* 获取移动设备本地IP |
|
|
|
|