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
New Developer
Posts: 18
Registered: ‎09-11-2009
My Device: Not Specified

How to get IP address in BB

Hi All,

 

 

I am porting STUN client application ( that is written in core Java 1.5) to Blackberry device. My question is how can we get the IP address of the BB device because that address we have to pass to the STUN server to know about the NAT type.... like this --

We do the same using core java classess in existing application.As we dont have InetAddress class in BB APIs so how can we know the IP address of the device ? Looking for kind reply

 

Developer
Posts: 588
Registered: ‎05-29-2009
My Device: 9100

Re: How to get IP address in BB

You can have a look into the class RadioInfo, maybe RadioInfo.getIpAddress(int apnId) is what you are looking for...

 

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: How to get IP address in BB

SocketConnection, ServerSocketConnection, and UDPDatagramConnection have a getLocalAddress method that returns the IP addresses you may be interested in. Note that these addresses may be different (if different sockets are created via different IP tunnels) and may also change with time. If you want to listen for IP address changed, you need to listen for PDP-related events via RadioStatusListener.

 

Note that RadioInfo also provides you with IP addresses assigned to the various tunnels, but you first need to find out which tunnel(s) you are interested in.

New Developer
Posts: 18
Registered: ‎09-11-2009
My Device: Not Specified

Re: How to get IP address in BB

Hi,

 

thanks for replying, I got the IP address using the below code....

String ip = new String("");

 

        try{
            int cni = RadioInfo.getCurrentNetworkIndex();
            int apnId = cni + 1;   //cni is zero based
            byte[] ipaddr = RadioInfo.getIPAddress(apnId);
            for(int i = 0;i<ipaddr.length;i++){
                int temp = (ipaddr[i] & 0xff);
                if(i<3){
                    ip=ip.concat(""+temp+".");
                }
                else{
                    ip=ip.concat(""+temp);
                }
            }
            System.out.println("ipaddress "+ip);
        }catch(Exception e)

        {
               ip = null;
        }

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: How to get IP address in BB

The above solution is blatantly wrong. Please see http://supportforums.blackberry.com/t5/Java-Development/wifi-ip-address/m-p/374279#M70949 for further discussion.