gpt4 book ai didi

java - 如何在 Windows 10 上使用 Java 判断 wifi 或蜂窝连接

转载 作者:行者123 更新时间:2023-12-02 10:07:41 25 4
gpt4 key购买 nike

我正在开发一个 Java 应用程序,在 Windows 10 上运行。在带有 SIM 卡的 Microsoft Surface Pro 平板电脑上。该应用程序提供了多种更新机制来更新 EFB(电子飞行包)应用程序集合、生成报告、将其发送到远程 Alfresco 服务器,并向用户和公司提供文档生命周期。有小更新和大更新之分,这意味着一个更新处理大约 300MB,另一个更新各处理 1.8 GB。现在,我们还将在没有 wifi 的情况下实现蜂窝更新。现在,我花了很多时间如何从 Java 角度区分 wifi 和蜂窝连接。我找到了一个 .net - API 来完成此操作,但我未能找到相应的 Java 方法。当然,我可以构建一个 .net-binary 并从 Java 调用它来存储包含答案的文件,但这似乎很难看。有人知道如何在 Windows 10 上仅使用 Java 来区分蜂窝网络和 Wifi 吗?欢迎任何提示。

最佳答案

此代码使用 java.net.InterfaceAddress 来检查接口(interface)。从此时起,可以通过 .getDisplayName() 方法轻松检测连接类型。我修改了代码 https://examples.javacodegeeks.com/core-java/net/networkinterface/java-net-networkinterface-example/

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.commons.lang.StringUtils;

public class networkConnectionTeller {

public static boolean isNetworkRunningViaCellular() throws SocketException {
String s = "";
// NetworkInterface implements a static method that returns all the
//interfaces on the PC,
// which we add on a list in order to iterate over them.
ArrayList interfaces =
Collections.list(NetworkInterface.getNetworkInterfaces());

s += ("Printing information about the available interfaces...\n");
for (Object ifaceO : interfaces) {
NetworkInterface iface = (NetworkInterface) ifaceO;
// Due to the amount of the interfaces, we will only print info
// about the interfaces that are actually online.
if (iface.isUp() &&
!StringUtils.containsIgnoreCase(iface.getDisplayName(), "loopback")) {
//Don`t want to see software loopback interfaces

// Display name
s += ("Interface name: " + iface.getDisplayName() + "\n");

// Interface addresses of the network interface
s += ("\tInterface addresses: ");
for (InterfaceAddress addr : iface.getInterfaceAddresses()) {
s += ("\t\t" + addr.getAddress().toString() + "\n");
}

// MTU (Maximum Transmission Unit)
s += ("\tMTU: " + iface.getMTU() + "\n");

// Subinterfaces
s += ("\tSubinterfaces: " +
Collections.list(iface.getSubInterfaces()) + "\n");

// Check other information regarding the interface
s += ("\tis loopback: " + iface.isLoopback() + "\n");
s += ("\tis virtual: " + iface.isVirtual() + "\n");
s += ("\tis point to point: " + iface.isPointToPoint() + "\n");
System.out.println(s);

if (iface.getDisplayName().contains("Broadband")) {
return true;
}
}
}
return false;
}
}

关于java - 如何在 Windows 10 上使用 Java 判断 wifi 或蜂窝连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55225505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com