Android Question LAN Ip only ever returns 127.0.0.0

Falcon

Member
Licensed User
Hi Guys,

I am trying to get the IP Address assigned to my device for my local wifi network, but all I ever get is 127.0.0.0 even though I can see my device is connected to the wifi if I look in my device's android settings.

I use this code:

B4X:
Dim Sock as ServerSocket
Sock.Initialize(0,"")
Dim IP as string = Sock.GetMyWifiIP

Any ideas?

Thanks in advance,
Jacques.
 

Falcon

Member
Licensed User
Hi Erel,

Thanks for the reply.
My device is real, it is a Xiaomi Redmi Note 7 cellphone running Android Version 9.

I removed the 'Initialize' line of code, but I still get 127.0.0.0

Here is my Manifest (if it will help you):
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.CHANGE_WIFI_STATE)
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.WAKE_LOCK)

Regards,
Jacques.
 
Upvote 0

Falcon

Member
Licensed User
Erel if I go to on the phone to Settings --> About Phone --> All Specs --> Status the phone has an assigned IP Address of 10.0.0.8 and I have a Wi-fi Mac Address also.
The phone is also showing in the router as connected.
 
Upvote 0

Falcon

Member
Licensed User
Yes I have B4A Bridge installed, it shows 10.0.0.8, and yes it does work, it is what I use for my development all the time.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I've had a quick look at the B4A_Bridge source code
B4A-Bridge uses ServerSocket.GetMyWifiIP, it doesn't even bother to Initialize it as it only uses it to get the IP address, so it seems to be working OK there.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I've just noticed in post #3 that you have manually added a lot of permissions to your manifest. That is not needed, B4A will add most permissions if a library needs them. B4A-Bridge has none of those you have added in its manifest, try removing them. You can see the permissions added by B4A by pressing the List Permissions button at the bottom of the Log pane.
 
Upvote 0

Falcon

Member
Licensed User
Hmmm that's interesting Agraham since that is exactly what I am doing as you know :rolleyes:
Oh well I guess I must just keep on trying.

By the way, while I am speaking to yourself & erel, I would like a simple way to connect my android app to a pc on my local LAN.
I want to have a server program listening on this PC which I will create in VB.Net. I would just basically like to send simple plain text commands to the pc.

What is the way you would reccommend I could achieve this?

Thanks for all the help so far, much appreciated!
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
What is the way you would reccommend I could achieve this?
If you are willing to use B4J there is a ready made cross-platform example from Erel that you could probably build on here

Have you seen my permission post above?
 
Upvote 0

Falcon

Member
Licensed User
Ahh I just saw your last reply, I removed 'android.permission.CHANGE_WIFI_STATE' from the manifest and now I get the IP! Thank you so much Agraham! :)
 
Upvote 0

Falcon

Member
Licensed User
Hi Erel,

The weird thing is now all of a sudden if I don't add that permission my app crashes when I try to connect to Wifi!
So at the moment I am forced to add it, but in doing so again I end up with 127.0.0.0 :oops:
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
FWIW, on my pixel 3a, os 11, b4a 10.2, sdk 29, jdk1.8.0_211 i get my wifi address with op's 3 lines of code with or without change_wifi_state permission. manifest permissions are only those generated by the server socket in the network library.

in addition, GetMyWifiIp method in network library makes a call to wifimanager.getConnectionInfo(). according to android docs, ACCESS_FINE_LOCATION is required. this permission appears in op's manifest, but not in mine (objects folder). and yet the test returns the same wifi address shown in my system settings.
 
Last edited:
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
I had this problem years ago.

I found that I could not get the IP address of the device in the same routine that I initialized the ServerSocket.

My solution was to get the IP address in the Phone Events ConnectivityChanged event.
 
Upvote 0

Falcon

Member
Licensed User
Thank you for all the replies guys, much appreciated.

Canalrun I am a noob with B4A development as you may have gathered, how do I get the IP Address from the ConnectivityChanged event? ?

So far I have this (In my Class Module):
B4X:
Sub Class_Globals
      Private PE As PhoneEvents
End Sub

Public Sub Initialize(Activity As Activity,strEventName As String)
     PE.Initialize("PE")
End Sub

Sub PE_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
      'No clue what to put here to get IP???
End Sub
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Here is some code that is quite a few years old. There is some extraneous stuff, but I've highlighted the important code.
NetSvr is type ServerSocket
pe is type PhoneEvents

Whoops! It looks like highlighting doesn't work inside a code block. The stuff between (B) and (/B) is supposed to be highlighted.

B4X:
Sub Service_Create
  'start listening for wifi connections
  [B]NetSvr.Initialize(NetPort, "NetSvr")[/B]

  Try
    NetSvr.Listen
  Catch
      Log("NetSvr.Listen: " & LastException.Message)
  End Try

  [B]pe.Initialize("pe")
  pe_ConnectivityChanged("", "", Null)[/B]

  UDPSoc.Initialize("UDPSvr", UDPPort, 128)

  BCAddr = Utl.GetBroadcastAddress

'  Log("BCAddr: " & BCAddr)

  mConnections.Initialize
  bConnecting = False

  MyIP = "N/A"
End Sub

Sub pe_ConnectivityChanged(NetworkType As String, State As String, Intent As Intent)
'  Log("ConnectivityChanged: " & NetworkType & ", "  & "State" & ", " & Intent.ExtrasToString)

  ' MyIP = NetSvr.GetMyWifiIP
  [B]MyIP = NetSvr.GetMyIP[/B]

  UpdateUI
End Sub
 
Upvote 0
Top