如何在Windows 10上使用Java识别wifi或蜂窝连接

时间:2019-03-18 16:05:29

标签: java windows wifi cellular-network

我正在研究在Windows 10 prof上运行的Java应用程序。带有SIM卡的Microsoft Surface Pro平板电脑上。该应用程序提供了几种更新机制来更新EFB(电子集尘袋)应用程序的集合,生成报告,将其发送到远程Alfresco服务器,并为用户和公司提供Doc-lifecycle。有大小更新,这意味着一个更新处理大约300MB,另一个更新处理每个1.8 GB。现在,在没有wifi的情况下,我们还将实施蜂窝更新。现在,我花了很多时间从Java角度区分wifi和蜂窝连接。我找到了一个.net-API来完成此操作,但是我找不到对应的Java方法。当然,我可以构建一个.net-binary并从Java调用它以存储带有答案的文件,但这似乎很丑陋。任何人都具有在Windows 10上如何仅使用Java区分蜂窝和Wifi的经验?任何提示。

1 个答案:

答案 0 :(得分:1)

此代码使用java.net.InterfaceAddress检查up接口。从这一点开始,可以从.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;
    }
}