qt android 开发之wifi开发篇

小编简单介绍如何通过调用android API来获取wifi服务(
非常感谢作者安晓辉发布了qt android 核心编程这本书,让我两天搞懂了很多在qt中开发安卓的要领,现在跟大家分享下!至于还没知道
怎么配置qt android换将的开发者,极力推荐先去看看qt android 核心编程这本书(哈哈,一知道要出这本书, 还没出版的时候我天天看着京东
有货了没!)
上代码!
ExtendsQtWithJava.java

package an.qt.extendsQtWithJava;

import android.app.PendingIntent;
import android.widget.Toast;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.location.LocationManager;
import android.location.Criteria;
import android.provider.Settings;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationProvider;
import java.lang.ClassLoader;
import dalvik.system.DexClassLoader;
import java.lang.reflect.Field;
import android.os.Bundle;
import android.os.Environment;
import java.io.File;

//tommego

import java.util.List;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import java.io.IOException;
import java.lang.Exception;
import java.lang.Throwable;
import android.net.DhcpInfo;
import android.content.Context;

//屏幕像素密度
import android.util.DisplayMetrics;
//tommego

public class ExtendsQtWithJava extends org.qtproject.qt5.android.bindings.QtActivity
{
private static ExtendsQtWithJava m_instance;

/             *tommego                                        *//             *tommego                                        */public static WifiInfo currentWifiInfo;//当前所连接的wifipublic static List wifiList;// wifi列表public static List wifiConList;// wifi 已成功连接过的配置列表public static int wifiIndex;//从scanResult 得到的wifi列表进行记录位置public static  String[] str;//。。。。。//手机连接wifi 后得到的动态ippublic static DhcpInfo hostDhcpInfo;/             *tommego                                        *//             *tommego                                        */public ExtendsQtWithJava(){    m_instance = this;}                                   //实例化单例对象/             *tommego                                        *//             *tommego                                        *///open wifipublic static void openWifi(){    WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE);    if(!conMan.isWifiEnabled()){        conMan.setWifiEnabled(true);    }}//close wifipublic static void closeWifi(){    WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE);    if(conMan.isWifiEnabled()){        conMan.setWifiEnabled(false);    }}//scan wifipublic static void scanWifi(){    WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE);    conMan.startScan();      // 获取扫描结果    wifiList = conMan.getScanResults();}//get wifi infopublic static int getWifiCount(){    return wifiList.size();}public static String getWifiSSID(int index){    if(index>=0&&index=0&&index=0&&index=0&&index这个地方一定要注意了。旁边的“是不能够省略的。密码的地方也一样。   wc.preSharedKey = "\""+Password+"\"";      //该热点的密码   wc.hiddenSSID = true;   wc.status = WifiConfiguration.Status.ENABLED;   wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);   wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);   wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);   wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);   wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);   wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);   wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);   return wc;}//无密码public WifiConfiguration CreateWifiInfoWithoutPasswd(ScanResult scanresult){    WifiConfiguration wc = new WifiConfiguration();    wc.SSID = "\""+scanresult.SSID+"\"";      //这个地方一定要注意了。旁边的“是不能够省略的。密码的地方也一样。

// wc.preSharedKey = "\""+Password+"\""; //该热点的密码
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
return wc;
}

//根据网络配置连接wifipublic static int connectToWifi(int scanresultId,String Password){    WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE);    int networkId = conMan.addNetwork(m_instance.CreateWifiInfo(wifiList.get(scanresultId),Password));    if(networkId != -1){        conMan.enableNetwork(networkId, false);        conMan.saveConfiguration();        return 1;//success    }    return 0;//falure}//无密码public static int connectToWifiWithoutPasswd(int scanresultId){    WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE);    int networkId = conMan.addNetwork(m_instance.CreateWifiInfoWithoutPasswd(wifiList.get(scanresultId)));    if(networkId != -1){        conMan.enableNetwork(networkId, false);        conMan.saveConfiguration();        return 1;//success    }    return 0;//falure}//获取屏幕像素密度public static double getDentisy(){    DisplayMetrics metrics=new DisplayMetrics();    m_instance.getWindowManager().getDefaultDisplay().getMetrics(metrics);    return metrics.density;}    /             *tommego                                        *//             *tommego                                        */

}
WifiUtil.java //一个将IP二进制转换我们能理解的地址的类

package an.qt.extendsQtWithJava;
import java.util.List;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
public class WifiUtil
{

public static WifiConfiguration createWifiInfo(String SSID,
String Password, int Type, WifiManager wifiManager)
{
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
WifiConfiguration tempConfig = isExsits(SSID, wifiManager);
if (tempConfig != null) {
wifiManager.removeNetwork(tempConfig.networkId);
}
if (Type == 1) // WIFICIPHER_NOPASS
{
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if (Type == 2) // WIFICIPHER_WEP
{
config.hiddenSSID = true;
config.wepKeys[0] = "\"" + Password + "\"";
config.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
if (Type == 3) // WIFICIPHER_WPA
{
config.preSharedKey = "\"" + Password + "\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
// config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}

//判断wifi是否存在

private static WifiConfiguration isExsits(String SSID,
WifiManager wifiManager)
{
List existingConfigs = wifiManager
.getConfiguredNetworks();
for (WifiConfiguration existingConfig : existingConfigs) {
if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
return existingConfig;
}
}
return null;
}

//转换IP地址

public static String intToIp(int i)
{
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)

  • "." + ((i >> 24) & 0xFF);
    }
    }
    这个是java端实现操作wifi的类,接下来是c++通过jni调用java接口的实现
    wifimanager.h

ifndef WIFIMANAGER_H

define WIFIMANAGER_H

include

include

include

include

struct WifiInfo{
int level;
QString SSID;
QString BSSID;
QString keytype;
};

class WifiManager : public QObject
{
Q_OBJECT
public:
explicit WifiManager(QObject *parent = 0);
~WifiManager();

//wifi processQ_INVOKABLE bool isWifiEnable();Q_INVOKABLE void openWifi();Q_INVOKABLE void closeWifi();Q_INVOKABLE void scanWifi();Q_INVOKABLE int getWifiListCount();Q_INVOKABLE QString getWifiSSID(int index);Q_INVOKABLE QString getWifiBSSID(int index);Q_INVOKABLE int getWifiLevel(int index);Q_INVOKABLE QString getKeyType(int index);//获取当前连接的wifi信息Q_INVOKABLE QString getConntectedWifiSSID();Q_INVOKABLE QString getConnectedWifiAddress();//连接到wifiQ_INVOKABLE void connectToWifi(int id,QString passwd);Q_INVOKABLE void connectToWifiWithoutPasswd(int id);//多媒体音量控制Q_INVOKABLE int getMaxVolumnStream();Q_INVOKABLE int getCurrentVolumnStream();Q_INVOKABLE void setVolumnStream(int a);//获取屏幕像素密度Q_INVOKABLE double getDentisy();//user processQ_INVOKABLE void refreshWifiList();Q_INVOKABLE int  wifiCount();Q_INVOKABLE int  wifiLevel(int i);Q_INVOKABLE QString wifiSSID(int i);Q_INVOKABLE QString wifiBSSID(int i);Q_INVOKABLE QString wifiKeyType(int i);

signals:

public slots:
private:

QList wifiList;

};

endif // WIFIMANAGER_H

wifimanager.cpp

include "wifimanager.h"

include

include

include "jni.h"

WifiManager::WifiManager(QObject *parent) : QObject(parent)
{

}

WifiManager::~WifiManager()
{

}

/ *wifi cability /
bool WifiManager::isWifiEnable(){
jint state =QAndroidJniObject::callStaticMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"networkState");
return state==1?true:false;
}

void WifiManager::openWifi(){
QAndroidJniObject::callStaticMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"openWifi");
}

void WifiManager::closeWifi(){
QAndroidJniObject::callStaticMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"closeWifi");
}

void WifiManager::scanWifi(){
QAndroidJniObject::callStaticMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"scanWifi");
}

int WifiManager::getWifiListCount(){

jint count=QAndroidJniObject::callStaticMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"getWifiCount");
return count;
}

QString WifiManager::getWifiSSID(int index){
QAndroidJniObject str=QAndroidJniObject::callStaticObjectMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"getWifiSSID",
"(I)Ljava/lang/String;",index);
return str.toString();
}

QString WifiManager::getWifiBSSID(int index){
QAndroidJniObject str=QAndroidJniObject::callStaticObjectMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"getWifiBSSID",
"(I)Ljava/lang/String;",index);
return str.toString();

}

int WifiManager::getWifiLevel(int index){
int a= QAndroidJniObject::callStaticMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"getWifiLevel","(I)I",index);
qDebug()80){
return 1;
}
else if(abs(a)>50&&abs(a)("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"connectToWifi",
"(ILjava/lang/String;)I",id,str.object());
qDebug()("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"connectToWifiWithoutPasswd",
"(I)I",id);
qDebug()("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"getMaxVolumnStream","()I");
}

int WifiManager::getCurrentVolumnStream(){
return QAndroidJniObject::callStaticMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"getCurrentVolumnStream","()I");
}

void WifiManager::setVolumnStream(int a){
QAndroidJniObject::callStaticMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"setVolumnStream","(I)V",a);
}

/ *多媒体音量控制 /

//获取屏幕像素密度
double WifiManager::getDentisy(){
return QAndroidJniObject::callStaticMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"getDentisy","()D");
}

/ user functionds /
void WifiManager::refreshWifiList(){
wifiList.clear();
scanWifi();
int count=getWifiListCount();
for(int a=0;a
效果截图:

关键字:qt5

版权声明

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部