Qt do Android development, sometimes need network information, this example is to get a phone search to the signal strong WIFI signal list. The knowledge of JNI is needed in the process. In addition, the example is relatively simple, if you need this, you can modify it yourself.
The program is divided into two parts, one is the JAVA program writing, one is the Qt main program of the writing, as to the Androidmanifest.xml file modification Part I do not add.
JAVA Program section:
Package Com.wifi;
Import Android.net.wifi.ScanResult;
Import Android.net.wifi.WifiManager;
Import java.util.List;
Import Android.os.Message;
Import Android.os.Handler;
Import Android.os.Bundle;
public class Wifiinfo extends org.qtproject.qt5.android.bindings.QtActivity
{
private static Wifiinfo m_instance;
Private Wifimanager Wifimanager;
List<scanresult> Listb;
Public Wifiinfo () {
M_instance = this;
}
Public string[] Callexternalcamera ()
{
System.out.println ("I an In");
Wifimanager = (Wifimanager) getsystemservice (Wifi_service);
Listb = Wifimanager.getscanresults ();
string[] Listk = new string[listb.size ()];
String res = "%";
if (listb!=null) {
for (int i=0;i<listb.size (); i++) {
Scanresult Scanresult = Listb.get (i);
Listk[i] = Scanresult.ssid;
System.out.println (SCANRESULT.SSID);
System.out.println ("\ n");
Res + = Scanresult.ssid + "%";
}
return LISTK;
}
return LISTK;
}
}
C + + program section:
#include "Mainwindow.h"
#include "Ui_mainwindow.h"
#include <QAndroidJniObject>
#include <QAndroidJniEnvironment>
#include <QtAndroid>
#include <QString>
#include <QList>
#include <qDebug>
Mainwindow::mainwindow (Qwidget *parent):
Qmainwindow (parent),
UI (New Ui::mainwindow)
{
UI->SETUPUI (this);
Getscanresult ();
}
Mainwindow::~mainwindow ()
{
Delete UI;
}
void Mainwindow::getscanresult ()
{
#ifdef q_os_android
Qandroidjniobject activity = qtandroid::androidactivity ();
Qandroidjniobject str = activity.callobjectmethod ("Callexternalcamera", "() [ljava/lang/string;");
Jobjectarray Objectarray = str.object<jobjectarray> ();
Qandroidjnienvironment qjnienv;
const int n = qjnienv->getarraylength (Objectarray);
Qdebug () << "Got Jobjectarray of length:" << N; Correct
for (int i = 0; i < n; ++i) {
Qdebug () << "Iteration:" << i;
Qandroidjniobject element = Qjnienv->getobjectarrayelement (Objectarray, i);
Qdebug () << element.tostring ();
}
#endif
}
The function of the program is relatively single, the main parts of the program are several:
1. Qt calls JAVA non-static functions.
2. The Qt call JAVA function returns a function signature of the object array type.
Gong sparse short lead , a word all fu, four rhyme. Please sprinkle Pan Jiang, each tilt Lu Haiyunl:
Qt for Android get Wifi list