[Android-development] connect to wifi via Qt

Stromme Christian Christian.Stromme at digia.com
Fri Sep 5 10:01:22 CEST 2014


On 04 Sep 2014, at 16:04 , Katerina Karagiannaki <kat.karayan at gmail.com> wrote:

> Hello everyone,
> 
> I am developing an application on Qt for Android. The application serves the role of aTCP client that connects to a server written in C++ and retrieves data from it. I also have deployed it on my Android phone.
> 
> I am also trying to connect to a wifi network programmatically via Qt. I have read the documentation of Bearer Management and QNetworkConfiguration and I am able to find all the available configurations in the wlan0 interface. I am also able to start a new session for a configuration and see all available interfaces etc.
> Therefore I think I have understood how the documentation works, but I am not able to connect to a wifi.
> I have read in the Qt forums that there is no built-in way in Qt to do that, and that I need to use some native api.

If you talk about configuring and establishing the connection to a wireless access point, then no, you can’t do that with the QNetwork classes.
On some platforms, if the plugin supports it[1], you can start and stop interfaces.

> 
> Another thing that i’ve tried is to execute a QProcess with Linux system calls, such as ifconfig and iwconfig to connect to the wifi in the same way as in Linux terminal (Qt desktop version) but I haven’t seen any results and if I see any, I don’t know if that will work in Android.

afaik, there are no command-line tools or native (C/C++) API's that expose the functionality needed to connect to a wifi network on Android.  In general there are very few native C/C++ APIs (or command line tools) available on Android compared to a normal desktop version of Linux (see the ‘docs’ folder in your ndk).

> 
> Is there a way to connect to wifi via Qt? Is there anything in the documentation that i have not seen or understood yet? If not, is it possible to do it in C++ with some API for Linux? (I have found out that there are APIs on Windows that do this but my server needs to be running on a Linux device). 

It depends on the level of control you need… There are bearer plugins for both ConnMan[2] and NetworkManager[3] available in Qt for Linux (requires DBus) which will enable you to start and stop interfaces. If you need more fine grained control then that, you might need to talk with iwconfig, ConnMan or NetworkManager directly. Note that neither of these are available on Android, so if you intend to share the network code between your client and server application, then you'll need to write an abstraction layer yourself.

> Finally, I am also thinking if it is possible to establish a wifi connection in Java. I googled and found code snippets in Android Java that make that connection possible. Is there a way to do that in Qt - run Java code with android packages (from Android SDK) somehow? I found this link -> http://developer.android.com/training/articles/perf-jni.html , should I continue towards that direction or better find something else?
> Sorry for the burst of questions, but I've been facing that problem for quite a while.

The android.net.wifi[4] package is probably what you want (I’ve not used it myself so I only know what the documentation says…). Since the APIs are only available through Java, you'll need to access them
through JNI[5]. To simplify the communication between your C++ code and Java, I would recommend that you write a small Java interface where you only expose the the APIs you need.


[1] http://doc-snapshot.qt-project.org/qt5-dev/qnetworkconfigurationmanager.html#Capability-enum
[2] http://en.wikipedia.org/wiki/ConnMan
[3] http://en.wikipedia.org/wiki/NetworkManager
[4] http://developer.android.com/reference/android/net/wifi/package-summary.html
[5] http://qt-project.org/doc/qt-5/qtandroidextras-module.html

—
Christian

> Any help would be great!
> Thanks in advance,
> Katerina
> 
> _______________________________________________
> Android-development mailing list
> Android-development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/android-development




More information about the Android-development mailing list