Android Question How to get non Lan IP address

scottie

Member
Licensed User
Longtime User
I've been searching and searching. Info is probably somewhere, but I could not find it.

How do I get the current IP address of device when NOT connected to my Lan? Cellular Network IP address.
I can get the Lan IP address all day long, but when device is on cell network, that's my problem.
ssocket.GetMyWifiIP only works for Lan connections

Any help would be appreciated. Thanks a bunch
-Scott
 

scottie

Member
Licensed User
Longtime User
Hi Erel,
yeah, I tried that.

Dim ssocket2 As ServerSocket
ToastMessageShow("Ip address: " & ssocket2.GetMyIP,True)

but this only reports 192.0.0.4 when on cell network.

I want my client to be able to see his current IP address anytime without him having to do anything.
this is an service
 
Last edited:
Upvote 0

sfsameer

Well-Known Member
Licensed User
Longtime User
Hi Erel,
yeah, I tried that.

Dim ssocket2 As ServerSocket
ToastMessageShow("Ip address: " & ssocket2.GetMyIP,True)

but this only reports 192.0.0.4 when on cell network.

I want my client to be able to see his current IP address anytime without him having to do anything.
this is an service
Hello,

Below are the steps :
1- Download the following jar from from our website and copy & paste it to your B4A additional jar folder :

2- After you copied and pasted the jar file to your B4A additional Jar folder, add the following to your B4A Project Attributes :
B4X:
#AdditionalJar : httpclient-4.0-alpha3.jar

Example :

1626095566595.png


1626095271900.png


3- Add the following inline java code to your desired activity :

B4X:
#if java
import java.util.List;
import java.util.Collections;
import java.net.NetworkInterface;
import java.net.InetAddress;
import org.apache.http.conn.util.InetAddressUtils;

public static String getDeviceIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface networkInterface : networkInterfaces) {
            List<InetAddress> inetAddresses = Collections.list(networkInterface.getInetAddresses());
            for (InetAddress inetAddress : inetAddresses) {
                if (!inetAddress.isLoopbackAddress()) {
                    String sAddr = inetAddress.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            // drop ip6 port suffix
                            int delim = sAddr.indexOf('%');
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return "";
}
#End If

4- Add the following B4A code to your desired sub :
B4X:
Dim jo As JavaObject
    jo.InitializeContext
    Dim str As String = jo.RunMethod("getDeviceIPAddress",Array(True))
    Log(str)


5- Final results :
Mobile Data Local IP :
localIP.jpeg




Thank you,
Saif
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
but this only reports 192.0.0.4 when on cell network.
That might be your cell network IP. Many cell providers hand out private IP numbers and handle the NATing on the back end
 
Upvote 0

scottie

Member
Licensed User
Longtime User
Wow, After all that, I still get 192.0.0.4.
I even tried starting my program on cell network.
I disabled Netguard(which I hate to do) and started program.
My Client has Netguard too. (everyone should)
no change.
 
Upvote 0

sfsameer

Well-Known Member
Licensed User
Longtime User
Wow, After all that, I still get 192.0.0.4.
I even tried starting my program on cell network.
I disabled Netguard(which I hate to do) and started program.
My Client has Netguard too. (everyone should)
no change.
Well that means this is your local IP address :)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

scottie

Member
Licensed User
Longtime User
Thanks for your help. To keep code down, i just have the app connect to my server which reports IP address.
It was the simplest / low code way I could do. This way I also test for internet connectivity
-Scott
 
Upvote 0
Top