Get local (LAN) IP address

Kevin

Well-Known Member
Licensed User
Longtime User
I tried looking through the help and forum last night, but unless I missed it, I didn't see any way to get the local (LAN) IP address assigned to the device.

Is there any way to do this? I would like my app to try scanning the local network for something specific, but in order to do that I need to know more about the network they are on.

I could assume 192.168.1.x, (where x is 1-255) but not all LANs are alike. :)

As a bonus... is there any way to ensure that the device is on a WiFi connection? I didn't seem to see anythinng specific to that either?
 

ZJP

Active Member
Licensed User
Longtime User
It's possible

B4X:
'Activity module
Sub Process_Globals
    Dim MyLan As ServerSocket  ' Lib Network
End Sub
'
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        MyLan.Initialize(0, "")
    End If
    Log(MyLan.GetMyIP) ' my ip local or not
    ' 
    If MyLan.GetMyIP = "127.0.0.1" Then  '
        ToastMessageShow("You are not connected", True)
        'ExitApplication
        'Return
    End If
    '
End Sub
'
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
What's wrong

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim Server1 As ServerSocket
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then   
   Server1.Initialize(80, "Server1")   'for testing the internet connection
End If
....

gives error:

B4X:
java.net.BindException: Permission denied
   at org.apache.harmony.luni.platform.OSNetworkSystem.socketBindImpl(Native Method)
   at org.apache.harmony.luni.platform.OSNetworkSystem.bind(OSNetworkSystem.java:107)
   at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:184)
   at java.net.ServerSocket.<init>(ServerSocket.java:138)
   at java.net.ServerSocket.<init>(ServerSocket.java:89)
   at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:161)
   at peacemaker.mobilecourier.status._activity_create(status.java:260)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
   at peacemaker.mobilecourier.status.afterFirstLayout(status.java:84)
   at peacemaker.mobilecourier.status.access$100(status.java:16)
   at peacemaker.mobilecourier.status$WaitForLayout.run(status.java:72)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   at dalvik.system.NativeStart.main(Native Method)
java.net.BindException: Permission denied
** Activity (status) Resume **
** Service (gps) Create **
....

?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
<uses-permission android:name="android.permission.INTERNET"/>

exists. And i never used "do not overwrite manifest file". Debugger works OK via b4a Bridge.
Network lib is v.1.11.

But strange that GetMyIP works OK, but the debugger is paused on the line "Server1.Initialize(80, "Server1") 'for testing the internet connection".
Internet is really accesible at this moment in emulator , and on the testing real device.
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Whoooo, it has helped, thanks, Erel !
I guess, it's (or will be) documented for B4A also.
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
I tried looking through the help and forum last night, but unless I missed it, I didn't see any way to get the local (LAN) IP address assigned to the device.

Is there any way to do this? I would like my app to try scanning the local network for something specific, but in order to do that I need to know more about the network they are on.

I could assume 192.168.1.x, (where x is 1-255) but not all LANs are alike. :)

As a bonus... is there any way to ensure that the device is on a WiFi connection? I didn't seem to see anythinng specific to that either?

Hi Kevin! Im wondering how you got on with this? I too would like to scan my network for something specific! im wondering how you got on, and if you have any tips!

Basically Im looking for a ftp server, I cant add something to the server to announce its presence, im using code that detects the devices IP, then searches the range for active IP's, then trying to connect to the ftp, but its not working for me!

Thanks in advance

Aidy
 
Upvote 0
Top