Qt for Android get Wifi list

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.