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
Developer
tanphat1412
Posts: 125
Registered: ‎11-06-2012
My Device: BlackBerry
Accepted Solution

Get IP address of wifi

I looking for api to get the IP address when connected to a Wifi. Any body know how to do that?

Thanks.

Please use plain text.
Developer
guru2000at
Posts: 101
Registered: ‎02-25-2011
My Device: Playbook, q5, z10

Re: Get IP address of wifi

[ Edited ]

you can do something like:

 

foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()){
	qDebug() << interface.humanReadableName();
	if(QString::compare(interface.humanReadableName(), "tiw_sta0") == 0){
		foreach (const QNetworkAddressEntry &entry, interface.addressEntries()) {
			qDebug() << "IP: " << entry.ip().toString();
		}
	}
}

 

________________
mappau Dev Team
blackberry@mappau.com
twitter: mappau
Please use plain text.
Developer
tanphat1412
Posts: 125
Registered: ‎11-06-2012
My Device: BlackBerry

Re: Get IP address of wifi

Hi,

 

All I can get is this:

 

default   9000  humanReadableName:  "lo0" 
default   9000  humanReadableName:  "bcm0" 
default   9000  humanReadableName:  "bcm1" 
default   9000  humanReadableName:  "pflog0" 
default   9000  humanReadableName:  "lo2" 
default   9000  humanReadableName:  "msm0" 
default   9000  humanReadableName:  "msm1" 
default   9000  humanReadableName:  "msm2" 
default   9000  humanReadableName:  "msm3" 
default   9000  humanReadableName:  "msm4" 
default   9000  humanReadableName:  "bptp0" 
default   9000  humanReadableName:  "rndis0" 

 Please help me with this. I don't know how to indentifier what name is wifi interface.

Please use plain text.
Developer
guru2000at
Posts: 101
Registered: ‎02-25-2011
My Device: Playbook, q5, z10

Re: Get IP address of wifi

[ Edited ]

which device do you use ?

do you need the application for a specific device ?

you can ssh to your device and type ifconfig to find your wifi interface. You can identify it by your current wifi ip adress for example

(to ssh to your device, you can use the momentics IDE: right click in the target navigator -> SSH)

 

________________
mappau Dev Team
blackberry@mappau.com
twitter: mappau
Please use plain text.
Developer
tanphat1412
Posts: 125
Registered: ‎11-06-2012
My Device: BlackBerry

Re: Get IP address of wifi

I'm using it on my Dev Alpha C.

And the up result is the log from SSH :smileyhappy:

Please use plain text.
Developer
guru2000at
Posts: 101
Registered: ‎02-25-2011
My Device: Playbook, q5, z10

Re: Get IP address of wifi

i have access to a dev c tomorrow and will have a look at it

________________
mappau Dev Team
blackberry@mappau.com
twitter: mappau
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

Re: Get IP address of wifi

I'm fairly sure it's in Native in deviceInfo

 

You may have to dig around a bit but I'm sure it's in there somewhere

 

You most likely need bbdevice linked

 

If you can't find it - lemme know - I'll look (useful for me as well)




Click the like button if you find my posts useful!
Please use plain text.
Developer
zezke
Posts: 1,004
Registered: ‎12-12-2010
My Device: Z10 (red Limited Edition)

Re: Get IP address of wifi

Try this:

 

QList<QHostAddress> hosts = QNetworkInterface::allAddresses();
for (int i = 0; i < hosts.count(); i++)
{
    qDebug() << hosts[i];
}

 

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Please use plain text.
Developer
guru2000at
Posts: 101
Registered: ‎02-25-2011
My Device: Playbook, q5, z10

Re: Get IP address of wifi

so, i tested it on a dev c and the name should be: bcm0

________________
mappau Dev Team
blackberry@mappau.com
twitter: mappau
Please use plain text.
Developer
tanphat1412
Posts: 125
Registered: ‎11-06-2012
My Device: BlackBerry

Re: Get IP address of wifi

Thanks.This is the way I get the IPv4 and IPv6 address based on prefixLength().

 

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

 

Please use plain text.