If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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

Native Development

Reply
Contributor
hatmann1944
Posts: 16
Registered: ‎11-13-2010
My Device: Not Specified
Accepted Solution

How to get WiFi IP address in BlackBerry 10

[ Edited ]

I want to get the ipv4 address of WiFi in blackberry and write these codes which are referenced to others as bellow, it worked in my Dev Alpha C but I have no other device to test.

However It seems not work in the Z10 or Q10.

Is the interface of network also called "bcm0" in the Z10 or Q10?

Is there anybody konw this problem?

Thank you.

 

    foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()){

    		qDebug() <<  "humanReadableName: " << interface.humanReadableName();
    		//if(QString::compare(interface.humanReadableName(), "en0") == 0){
    		if(QString::compare(interface.humanReadableName(), "bcm0") == 0){
    			foreach (const QNetworkAddressEntry &entry, interface.addressEntries()) {
    				if (entry.prefixLength() <= 32){
    					wifiIP = entry.ip().toString();
    					qDebug() << "IPv4: " << entry.ip().toString();
    					return true;
    				}
    				else
    					qDebug() << "IPv6: " << entry.ip().toString();
    			}
    		}
    	}
    return false;

 

Please use plain text.
Developer
peardox
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: How to get WiFi IP address in BlackBerry 10

Oddly enough I wrote this yesterday for a WebWorks extension

 

Here's the code skeleton

 

 

    netstatus_interface_details_t *details;

	if(BPS_SUCCESS == netstatus_get_interface_details(NULL, &details)) {
		netstatus_ip_status_t nstatus = netstatus_interface_get_ip_status(details);
		netstatus_interface_type_t ntype = netstatus_interface_get_type(details);
		bool nisconnected = netstatus_interface_is_connected(details);
		bool nisup = netstatus_interface_is_up(details);
		const char* nname = netstatus_interface_get_name(details);

		int nipcnt = netstatus_interface_get_num_ip_addresses(details);

		for(int i=0; i<nipcnt; i++) {
			const char* nipaddr = netstatus_interface_get_ip_address(details, i);
			const char* nipmask = netstatus_interface_get_ip_address_netmask(details, i);
			const char* nipgway = netstatus_interface_get_ip_gateway(details, i);
		}

		netstatus_free_interface_details(&details);
	}

 

As I was writing this for re-tooling as an extension it simply collects all the data then throw it all away (I was just watching it in the debugger)

 

You specifically need to store the IPs that nipaddr gets in the loop if nothing else

 

A little above that are important bits of info

 

nisup - if the network device is active

nisconnected - is it connected to a network

ntype - if you get a WIFI result (look up netstatus_interface_type_t in dox for all possible returns) you're on WiFi

nstatus - More connection info (look up netstatus_ip_status_t for details)

 

There are three possible outcomes

 

The first if fails (no network at all)

The network ntype is not WIFI (also bad)

The IP gets collected and you can use it

 

It should be noted that you will get at least two IPs so yu need to work out which is ipv4 and which is ipv6

 

Personally I'm just gonna regex them

 

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

The above is easy to do in JavaScript and identifies an ip4

 

Oh - the ipv6 that comes back is the full linux notation xxxx:xxxx:xxxx:xxxx%dev

 

You may want to do a sanity check on the ssid but I ain't got that far yet (I don't need it)

 

 

 

 

 




Click the like button if you find my posts useful!
Please use plain text.
Contributor
hatmann1944
Posts: 16
Registered: ‎11-13-2010
My Device: Not Specified

Re: How to get WiFi IP address in BlackBerry 10

[ Edited ]

Thank you so much.

I added your code and it works in my Dev Alpha C.

Hope it will work on the real device.

if(NETSTATUS_INTERFACE_TYPE_WIFI == netstatus_interface_get_type(details))
			{
				for(int i=0; i<nipcnt; i++) {

					const char* nipaddr = netstatus_interface_get_ip_address(details, i);
					const char* nipmask = netstatus_interface_get_ip_address_netmask(details, i);
					const char* nipgway = netstatus_interface_get_ip_gateway(details, i);

					qDebug() << nipaddr << nipmask << nipmask;
					wifiIP = nipaddr;
					netstatus_free_interface_details(&details);
					return true;
				}
			}

 

Please use plain text.
Developer
peardox
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: How to get WiFi IP address in BlackBerry 10

Me too

 

I'm having issues with getting the extension for WebWorks working (fine using Cascades)

 

The code I supplied has been tested and works nicely (apart from in my extension)

 

Glad to be able to help you out




Click the like button if you find my posts useful!
Please use plain text.
Developer
peardox
Posts: 1,229
Registered: ‎03-20-2011
My Device: Playbook, Z10 LE, Dev Alpha B, 2x Dev Alpha C
My Carrier: 3, Orange, Vodafone

Re: How to get WiFi IP address in BlackBerry 10

Oh

 

You imply you don't have a 'REAL DEVICE'

 

PM me and I'll test on one of mine if you want

 

I'll need to know what you expect to happen so I can tell you what actually happened (which ideally are the same)




Click the like button if you find my posts useful!
Please use plain text.
Developer
slashkyle
Posts: 813
Registered: ‎10-16-2012
My Device: Red Z10, Dev Alpha C
My Carrier: Telus

Re: How to get WiFi IP address in BlackBerry 10

[ Edited ]

the Dev Alpha C is actually a production device in a different body, it's SQN 100-3 (in most cases) without the upgraded battery (which can be upgraded)

Please use plain text.