Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
simon_hain
Posts: 15,558
Registered: ‎07-29-2008
My Carrier: O2 Germany
Accepted Solution

wifi ip address

i am trying to get the ip address of my device.

the solution described in http://supportforums.blackberry.com/t5/Java-Development/wifi-IPAddress/m-p/277651#M46737 does not work for me (current network is gprs, if i disable it and use wifi only, i get a RadioException, unable to get APN).

 

if i run RadioInfo.getNumberOfNetworks() i get 1, despite being connected using GPRS and Wifi. I can use the hotspot browser and have an ip address listed on the wifi options.

If i disable GPRS (still connected on wifi)   RadioInfo.getNumberOfNetworks() returns 0.

 

i guess i could open a server socket and use getLocalAddress(), but that's a bit much, isn't it?

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
Posts: 1,474
Registered: ‎04-14-2009

Re: wifi ip address

As you correctly pointed out, the best solution (as it relies on documented behavior) is to open a udp socket (or TCP server socket) over WiFi and query its IP address. Another hack is to get the APN ID for the "MagicRudyAPN.rim" using getAccessPointNumber, and then query its IP address using getIPAddress. MagicRudyAPN.rim seems to be a virtual/fake APN for accessing/addressing the IP tunnel to the WiFi network.

 

P.S. I'm tired of pointing out on this forum that radio network numbers/indices have no relation to APN IDs... All those highly praised posts with getNumberOfNetworks() + 1, -1, +0 as the solution are wrong -- they only seem to work for some users because the number of open tunnels (identified by APN IDs) is usually between 1 and 2, and the IDs of the tunnels are quite often sequential. There can be many tunnels open from a BlackBerry via various APNs. When a BlackBerry is enabled for a BlackBerry data plan, the very first tunnel that is open is usually the one via the blackberry.net APN. As a result, the tunnel for Direct TCP is usually next, if it's open at all, but it's not guaranteed, because the device can open and close other tunnels, such as WAP, for example... And even the blackberry.net tunnel doesn't have to be the first opened tunnel.

 

Please use plain text.
Developer
simon_hain
Posts: 15,558
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: wifi ip address

from time to time it happens that i am surprised, despite developing for some years.

 

klyubins solution works. One question remains: Who is Magic Rudy? :smileywink:

 

here is the code:

 

int apnId = RadioInfo.getAccessPointNumber("MagicRudyAPN.rim");
byte[] ipByte = RadioInfo.getIPAddress(apnId);
String ip = "";
for (int i = 0; i < ipByte.length; i++) {
	int temp = (ipByte[i] & 0xff);
	if (i < 3)
		ip = ip.concat("" + temp + ".");
	else {
		ip = ip.concat("" + temp);					
	}
}
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
Comitic
Posts: 68
Registered: ‎02-05-2009

Re: wifi ip address

This solution does not seem to work for me... :smileyindifferent:

 

 

	public static String getIPAddress() {

		int apnId = 0;
		try {
			apnId = RadioInfo.getAccessPointNumber("MagicRudyAPN.rim");
		} catch (RadioException e) {
			Log.e(e);
			e.printStackTrace();
		}

		byte[] ipByte = RadioInfo.getIPAddress(apnId);
		String ip = "";
		for (int i = 0; i < ipByte.length; i++) {
			int temp = (ipByte[i] & 0xff);
			if (i < 3)
				ip = ip.concat("" + temp + ".");
			else {
				ip = ip.concat("" + temp);
			}
		}

		Log.s(TAG + "Returning IP=" + ip);
		return ip;
	}

 

The difference is some boilerplate code I had to add like exception handling and logging but otherwise it should be identical... I am confused :smileysurprised:

 

Please use plain text.
Developer
Posts: 1,474
Registered: ‎04-14-2009

Re: wifi ip address

What exactly is not working? What are the values returned from each of the API methods, what exceptions are thrown?

 

P.S. Your exception handling looks weird -- it just logs the exception and then continues as though nothing happened...

Please use plain text.
New Contributor
irufus
Posts: 3
Registered: ‎09-17-2009

Re: wifi ip address

This solution works for me. 

 

THANK YOU

Please use plain text.
Developer
branchcut
Posts: 37
Registered: ‎10-17-2008

Re: wifi ip address

The code snippet from simon_hain on 11-06-2009 works for me too.

 

However, I wanted to mention that on some platforms opening a server socket and calling getLocalAddress() is NOT working, which seems like a bug:

 

 

ServerSocketConnection serverSoc = null;
String localAddressResult = null;
int randomPort = new DistributedRandom().nextInt(60000) + 1024;
String connStr = "socket://:" + randomPort + ";interface=wifi";
serverSoc = (ServerSocketConnection) Connector.open(connStr);
localAddressResult = serverSoc.getLocalAddress();

 

 

On some platforms, localAddressResult will have the correct IP, on others it will just be null.  I got the same results when using a UDPServerSocket.

 

Platforms that return valid IP:

 

BB Bold 9000 GSM v 4.6.0.304

 

Platforms that return null:

 

BB Curve 8530 CDMA v 5.0.0.337

 

Anyone seen similar behavior?

 

 

Please use plain text.
BlackBerry Development Advisor
twindsor
Posts: 739
Registered: ‎07-15-2008
My Carrier: Bell

Re: wifi ip address

branchcut,

 

Your finding is due to an issue that was discovered with Wifi on CDMA based devices. That issue is logged here: https://www.blackberry.com/jira/browse/JAVAAPI-741

 

Tim

Tim Windsor
Application Development Consultant
Please use plain text.
New Developer
uncleop
Posts: 17
Registered: ‎11-17-2009

Re: wifi ip address

Tim - the link to the bug is now dead, perhaps because of the change to the site. Do you have a new, working link? Thanks!
Please use plain text.
Developer
sudhakar_koundinya
Posts: 66
Registered: ‎01-14-2010
My Carrier: Airtel

Re: wifi ip address

Can somebody explain me or porvide additional info on MagicRudy.

Why somebody shall use MagicRudy to get device IP address?

 

Thanks &  Regards,

Sudhakar

Please use plain text.