Skip to content

Commit

Permalink
增加wifi的ip读取
Browse files Browse the repository at this point in the history
  • Loading branch information
ctj committed Nov 17, 2023
1 parent 0684324 commit bf1c90a
Showing 1 changed file with 47 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class NetUtils {

/**
* 设置dns列表
*
* @param dnsList xx.xx.xx.xx,xx.n.n.n.....
*/
public static void setDnsList(String[] dnsList) {
Expand Down Expand Up @@ -406,7 +407,7 @@ public static DnsBean checkNetWork() {
DnsBean dnsBean = NetUtils.ping(dns[0], 1, 1);
if (dnsBean.isPass) {
return dnsBean;
}else{
} else {
return checkNetWorkCallback();
}
} else {
Expand All @@ -415,7 +416,7 @@ public static DnsBean checkNetWork() {
}

// 检查外部互联网连接是否正常
public static boolean isInetAddressAvailable(int timeoutMillis,String host) {
public static boolean isInetAddressAvailable(int timeoutMillis, String host) {
try {
InetAddress address = InetAddress.getByName(host);
if (address != null) {
Expand All @@ -430,14 +431,14 @@ public static boolean isInetAddressAvailable(int timeoutMillis,String host) {
}

// 检查设备是否连接到外网
public static boolean isInternetAvailable(int timeout,String host) {
if (NetUtils.getNetWorkType()!=NETWORK_NO) {
public static boolean isInternetAvailable(int timeout, String host) {
if (NetUtils.getNetWorkType() != NETWORK_NO) {
try {
OkHttpClient client = new OkHttpClient.Builder()
.followRedirects(true)
.followSslRedirects(true)
.connectTimeout(timeout, TimeUnit.MILLISECONDS)
.readTimeout(timeout,TimeUnit.MILLISECONDS)
.readTimeout(timeout, TimeUnit.MILLISECONDS)
.callTimeout(timeout, TimeUnit.MILLISECONDS)
.writeTimeout(timeout, TimeUnit.MILLISECONDS)
.build();
Expand Down Expand Up @@ -492,7 +493,7 @@ public static DnsBean ping(String ip, int c, int w, int W) {
StringBuffer cbstr = new StringBuffer();
try {
//过滤http:// 或 https://
ip=ip.replaceFirst("https?://","");
ip = ip.replaceFirst("https?://", "");
cbstr.append("ping");
cbstr.append(c > 0 ? (" -c " + c) : (" -c 1"));
cbstr.append(w > 0 ? (" -w " + w) : (" -w 1"));
Expand Down Expand Up @@ -902,34 +903,57 @@ public static String getEthIPv4Address() {
*/
public static String getLteIpAddress() {
ConnectivityManager connectivityManager = (ConnectivityManager) BaseIotUtils.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);

if (connectivityManager != null) {
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();

if (activeNetwork != null) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI || activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
Enumeration<InetAddress> addresses = iface.getInetAddresses();

while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
if (!addr.isLoopbackAddress() && addr.getAddress().length == 4) {
return addr.getHostAddress();
}
if (activeNetwork != null && activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
Enumeration<InetAddress> addresses = iface.getInetAddresses();

while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
if (!addr.isLoopbackAddress() && addr.getAddress().length == 4) {
return addr.getHostAddress();
}
}
} catch (Exception e) {
Log.e("NetworkUtils", "Error getting IP address: " + e.getMessage());
}
} catch (Exception e) {
Log.e("NetworkUtils", "Error getting IP address: " + e.getMessage());
}
}
}
return null;
}

/**
* 获取wifi的ipv4地址
*/
public static String getWifiIpAddress() {
WifiManager wifiManager = (WifiManager) BaseIotUtils.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);

if (wifiManager != null && wifiManager.isWifiEnabled()) {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();

// Convert the IP address to a human-readable format
String ipAddressString = formatIpAddress(ipAddress);

Log.d("WifiUtils", "WiFi IP Address: " + ipAddressString);
return ipAddressString;
}

return null;
}

private static String formatIpAddress(int ipAddress) {
return (ipAddress & 0xFF) + "." +
((ipAddress >> 8) & 0xFF) + "." +
((ipAddress >> 16) & 0xFF) + "." +
(ipAddress >> 24 & 0xFF);
}

/**
* sim卡ccid
*
Expand Down

0 comments on commit bf1c90a

Please sign in to comment.