JTechnau
JTechnau

Reputation: 29

How to tell wifi or cellular connection using Java on Windows 10

I am working on a Java application, running on Windows 10 prof. on Microsoft Surface Pro Tablets with SIM card. This application provides several updating mechanisms to update a collection of EFB (Electronic Fligt Bag) Applications, generate reports, sends them to a remote Alfresco server, and provides a Doc-lifecycle to users and the company. There are small and big updates, meaning one update handles about 300MB, the other is 1.8 GB each. Now we are going to implement cellular updates as well, in cases where there is no wifi avail. Now, I spent a lot of time how to distinguish wifi and cellular connectivity from Java perspective. I found a .net - API to do exactly this, but I failed to find a corresponding Java method. Of course I can build a .net-binary and call it from Java to store a file with the answer, but this seems to be ugly. Anyone has experience on how to use Java only to distinguish between cellular and Wifi on Windows 10? Any Hint welcome.

Upvotes: 0

Views: 158

Answers (1)

JTechnau
JTechnau

Reputation: 29

This code uses java.net.InterfaceAddress to check for up interfaces. From this point on the connection type is easily detectable from the .getDisplayName()-method. I modified code from 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;
    }
}

Upvotes: 1

Related Questions