1. Download the free trial version

Get local (LAN) IP address

Discussion in 'Questions' started by Kevin, Aug 1, 2011.

  1. Kevin

    Kevin Well-Known Member Licensed 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?
     
  2. Gary Miyakawa

    Gary Miyakawa Active Member Licensed User

    Do some reading in the ABWifi library... I think this will help you out...


    Gary M
     
  3. ZJP

    ZJP Active Member Licensed User

    It's possible

    Code:
    '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: Aug 2, 2011
  4. Kevin

    Kevin Well-Known Member Licensed User

    Thanks. I will look into this tomorrow.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    The above code is correct. It is better to only initialize MyLan when FirstTime is True.
     
  6. ZJP

    ZJP Active Member Licensed User

    Indeed.
    The post has been updated. ;)

    JP
     
  7. peacemaker

    peacemaker Well-Known Member Licensed User

    What's wrong

    Code:
    '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:

    Code:
    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 **
    ....
    ?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    You are missing the Internet permission. I guess that you are using a custom manifest file. Open AndroidManifest-Example.xml to see the permissions.
     
  9. peacemaker

    peacemaker Well-Known Member Licensed 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: Oct 5, 2011
  10. Erel

    Erel Administrator Staff Member Licensed User

    You should use a port number larger than 1024. Lower ports are restricted by the system.
     
  11. peacemaker

    peacemaker Well-Known Member Licensed User

    Whoooo, it has helped, thanks, Erel !
    I guess, it's (or will be) documented for B4A also.
     

Share This Page

Loading...