Get current IP address of device

196 views
Skip to first unread message

t...@pixelsedge.ca

unread,
Feb 17, 2015, 10:41:12 AM2/17/15
to codenameone...@googlegroups.com
I have the need to capture the users IP address and pass that along as part of a call. What I used was

import java.net.InetAddress;

text
+= ":" + InetAddress.getLocalHost().getHostAddress();

but that doesn't seem to be supported by the platform... Can someone suggest one that will work?

Thanks

Shai Almog

unread,
Feb 17, 2015, 1:49:47 PM2/17/15
to codenameone...@googlegroups.com, t...@pixelsedge.ca
This is supported as part of the Socket API in com.codename1.io:
Socket.getHostOrIP().

t...@pixelsedge.ca

unread,
Feb 17, 2015, 2:36:01 PM2/17/15
to codenameone...@googlegroups.com, t...@pixelsedge.ca
Awesome! Thats exactly what I needed. Thanks :)

mark....@comcast.net

unread,
Jul 1, 2017, 8:27:23 PM7/1/17
to CodenameOne Discussions, t...@pixelsedge.ca
I have run com.codename1.io.Socket.getHostOrIP() on Galaxy 4 and getting  (localhost 127.0.0.1) back
I need the IP address assigned to my wifi connection like 192.168.1.123

Please advise.

Shai Almog

unread,
Jul 2, 2017, 12:38:31 AM7/2/17
to CodenameOne Discussions, t...@pixelsedge.ca, mark....@comcast.net
Did you run this on the device?

shop.servic...@gmail.com

unread,
Jul 2, 2017, 8:22:34 AM7/2/17
to CodenameOne Discussions, t...@pixelsedge.ca, mark....@comcast.net
Yes, this ran on Samsung model sch-1545, android version 5.0.1

Shai Almog

unread,
Jul 3, 2017, 12:58:14 AM7/3/17
to CodenameOne Discussions, t...@pixelsedge.ca, mark....@comcast.net, shop.servic...@gmail.com
OK, I see an issue. We'll fix it for the next update

shop.servic...@gmail.com

unread,
Jul 3, 2017, 11:22:08 AM7/3/17
to CodenameOne Discussions, t...@pixelsedge.ca, mark....@comcast.net, shop.servic...@gmail.com
Thanks,

No Pressure, roughly can we expect this update?


On Monday, July 3, 2017 at 12:58:14 AM UTC-4, Shai Almog wrote:

Shai Almog

unread,
Jul 4, 2017, 1:24:16 AM7/4/17
to CodenameOne Discussions, t...@pixelsedge.ca, mark....@comcast.net, shop.servic...@gmail.com
Check out the git project. I already committed it yesterday. It should be up by Friday.

shop.servic...@gmail.com

unread,
Jul 4, 2017, 5:53:14 PM7/4/17
to CodenameOne Discussions, t...@pixelsedge.ca, mark....@comcast.net, shop.servic...@gmail.com
Thanks, Appreciate your looking at this.

I don't understand "It should be up by Friday"

How do these updates work?


On Tuesday, July 4, 2017 at 1:24:16 AM UTC-4, Shai Almog wrote:

Shai Almog

unread,
Jul 5, 2017, 12:13:36 AM7/5/17
to CodenameOne Discussions, t...@pixelsedge.ca, mark....@comcast.net, shop.servic...@gmail.com
Seamlessly.
When you send a build a new version will be used and you will receive updated libraries.

shop.servic...@gmail.com

unread,
Jul 5, 2017, 8:41:23 AM7/5/17
to CodenameOne Discussions, t...@pixelsedge.ca, mark....@comcast.net, shop.servic...@gmail.com
Thanks.

shop.servic...@gmail.com

unread,
Jul 8, 2017, 1:07:51 PM7/8/17
to CodenameOne Discussions, t...@pixelsedge.ca, mark....@comcast.net, shop.servic...@gmail.com
I have tested this today:
deviceIP.setText(com.codename1.io.Socket.getHostOrIP());
is returning "localhost"
Should be returningthe 192.168.1.114 IP address assigned to the wireless network.

Thoughts?

Shai Almog

unread,
Jul 9, 2017, 12:46:18 AM7/9/17
to CodenameOne Discussions, t...@pixelsedge.ca, mark....@comcast.net, shop.servic...@gmail.com
I guess that means the fix failed in which case we'll need to re-examine it. Please file an issue in git.

Mark Bolduc

unread,
Jul 9, 2017, 11:41:54 AM7/9/17
to Shai Almog, CodenameOne Discussions, t...@pixelsedge.ca, shop.servic...@gmail.com

Thanks.

 

Done.

 

Regards

Mark Bolduc

shop.servic...@gmail.com

unread,
Aug 3, 2017, 9:44:21 AM8/3/17
to CodenameOne Discussions, shai....@gmail.com, t...@pixelsedge.ca, shop.servic...@gmail.com, mark....@comcast.net
com.codename1.io.Socket.getHostOrIP() on Android is now returning what appears to be an IPV6 address.
"fe80::ce3a:61ff:fe06:7346%wlan0"

Thoughts?

Shai Almog

unread,
Aug 4, 2017, 1:56:57 AM8/4/17
to CodenameOne Discussions, shai....@gmail.com, t...@pixelsedge.ca, shop.servic...@gmail.com, mark....@comcast.net
It looks like a valid operator hostname. Keep in mind that a device has more than one network interface (cell, wifi, roam...).

This is the code that implements this feature https://github.com/codenameone/CodenameOne/blob/master/Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java#L7373-L7396

You can try and fix it. Then submit a pull request https://www.codenameone.com/blog/how-to-use-the-codename-one-sources.html

Or you can use a native interface to get the result you want: https://www.codenameone.com/how-do-i---access-native-device-functionality-invoke-native-interfaces.html

shop.servic...@gmail.com

unread,
Aug 4, 2017, 8:38:50 AM8/4/17
to CodenameOne Discussions, shai....@gmail.com, t...@pixelsedge.ca, shop.servic...@gmail.com, mark....@comcast.net

Thanks,
I would prefer to use all Codenameone generic interfaces/translations so I don't lock myself into a specific device/OS

Can we get the enumeration of all interfaces and decide which ones we want?
Curious, I don't even know what to do with this return string "fe80::ce3a:61ff:fe06:7346%wlan0".
Is there a way to translate this to an xxx.xxx.xxx.xxx net address?

Shai Almog

unread,
Aug 5, 2017, 2:11:14 AM8/5/17
to CodenameOne Discussions, shai....@gmail.com, t...@pixelsedge.ca, shop.servic...@gmail.com, mark....@comcast.net
Currently we don't have such an API. It gets pretty hairy even in native Java SE/Android as you can see from the API calls above.
It's worse since this can change dynamically as the user walks around...
Reply all
Reply to author
Forward
0 new messages