0

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.

4
  • There is no way to use Java natively. You may be able to call into powershell or something using exec. But thats gonna be a very messy solution. My recommendation is to do it in c and call that using the JNI.
    – Dylan
    Mar 18, 2019 at 16:19
  • Not a full answer because I would have no idea how to implement what you want, but the java.net.NetworkInterface class can provide info about the interfaces (their names, whether they are active ,etc) and if you have control (if they are in-house) over the devices (hence know the interfaces's name), you might be able to hack something around it without using a .NET binary Mar 18, 2019 at 16:22
  • @FrenchFigaro this seems to work...I can actually filter existing interfaces for those being up...and wifi and cellular interfaces are never up simultationously in the way our windows is configured. I`ll provide the sources soon
    – JTechnau
    Mar 19, 2019 at 16:40
  • thanks to both of you, much appreciated !
    – JTechnau
    Mar 19, 2019 at 16:41

1 Answer 1

1

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.