문제

The code I'm using to get the IP address of my device is:

public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); 
                    if (useIPv4) {
                        if (isIPv4) 
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim<0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) { } // for now eat exceptions
    return "";
}

The issue being the code opts for the 10.xx.xx.xx IP instead of the 192.xxx.xxx.xxx address that I want it to. How can I force it to use the 192 address?

I'm using a Samsung Galaxy Express. It seems to be using Cell rather than WiFi for some tasks which is causing problems. I have a Samsung tablet, that has no Cell signal, and this opts for the 192 address (the only option I know) but the app overall works, where as the Express isn't as it seems to be using Cell rather than WiFi.

I've tried turning "mobile networks" off but that hasn't helped.

As an extra question, any idea how I fully disable the mobile network on my phone without having to take the sim card out?

Thanks

도움이 되었습니까?

해결책

The issue being the code opts for the 10.xx.xx.xx IP instead of the 192.xxx.xxx.xxx address that I want it to. How can I force it to use the 192 address?

It seems like your problem is Weak wifi connectivity that leads your phone to choose mobile data over wifi. You cannot force 192.xxx address (i.e. Wifi) from application, its a system attribute.

As an extra question, any idea how I fully disable the mobile network on my phone without having to take the sim card out?

Ideally you should have option to disable data in power menu (long press power button on samsung). If that doesn't help, do following go to settings > more mobile networks > access points > delete all APNs you see.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top