11-06-2009 05:49 AM
i am trying to get the ip address of my device.
the solution described in http://supportforums.blackberry.com/t5/Java-Develo
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?
Solved! Go to Solution.
11-06-2009 06:06 AM
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.
11-06-2009 07:02 AM
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? ![]()
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);
}
}
02-10-2010 05:23 PM
This solution does not seem to work for me... ![]()
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 ![]()
02-11-2010 05:30 AM
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...
03-02-2010 04:00 PM
This solution works for me.
THANK YOU
04-12-2010 03:13 PM
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?
04-12-2010 04:52 PM
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
07-21-2010 01:33 PM
08-09-2010 03:18 AM
Can somebody explain me or porvide additional info on MagicRudy.
Why somebody shall use MagicRudy to get device IP address?
Thanks & Regards,
Sudhakar