小編簡單介紹如何經過調用android API來獲取wifi服務(
很是感謝做者安曉輝發佈了qt android 核心編程這本書,讓我兩天搞懂了不少在qt中開發安卓的要領,如今跟你們分享下!至於還沒知道
怎麼配置qt android換將的開發者,極力推薦先去看看qt android 核心編程這本書(哈哈,一知道要出這本書, 還沒出版的時候我每天看着京東
有貨了沒!)
上代碼!
工程源碼(這些都是大二大三時候造的,代碼可能有點爛)
ExtendsQtWithJava.javajava
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;//當前所鏈接的wifi public static List<ScanResult> wifiList;// wifi列表 public static List<WifiConfiguration> wifiConList;// wifi 已成功鏈接過的配置列表 public static int wifiIndex;//從scanResult 獲得的wifi列表進行記錄位置 public static String[] str;//。。。。。 //手機鏈接wifi 後獲得的動態ip public static DhcpInfo hostDhcpInfo; /***************************tommego*********************************************************************************/ /***************************tommego*********************************************************************************/ public ExtendsQtWithJava(){ m_instance = this; } //實例化單例對象 /***************************tommego*********************************************************************************/ /***************************tommego*********************************************************************************/ //open wifi public static void openWifi(){ WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE); if(!conMan.isWifiEnabled()){ conMan.setWifiEnabled(true); } } //close wifi public static void closeWifi(){ WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE); if(conMan.isWifiEnabled()){ conMan.setWifiEnabled(false); } } //scan wifi public static void scanWifi(){ WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE); conMan.startScan(); // 獲取掃描結果 wifiList = conMan.getScanResults(); } //get wifi info public static int getWifiCount(){ return wifiList.size(); } public static String getWifiSSID(int index){ if(index>=0&&index<wifiList.size()){ return wifiList.get(index).SSID; } else{ return ""; } } public static int getWifiLevel(int index){ if(index>=0&&index<wifiList.size()){ return wifiList.get(index).level; } else{ return 0; } } public static int getWifiFrequency(int index){ if(index>=0&&index<wifiList.size()){ return wifiList.get(index).frequency; } else{ return 0; } } public static String getWifiBSSID(int index){ if(index>=0&&index<wifiList.size()){ return wifiList.get(index).BSSID; } else{ return ""; } } //加密方式判斷 public static String getWifiKeyType(int index){ if (wifiList.get(index).capabilities.contains("WEP")) { return "WEP"; } else if (wifiList.get(index).capabilities.contains("PSK")) { return "PSK"; } else if (wifiList.get(index).capabilities.contains("EAP")) { return "EAP"; } return "無"; } //get current connected wifi info public static String getCurrentWifiSSID(){ WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE); currentWifiInfo = conMan.getConnectionInfo(); return currentWifiInfo.getSSID(); } //get current ip address public static String getCurrentWifiIPAddress(){ WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE); currentWifiInfo = conMan.getConnectionInfo(); return WifiUtil.intToIp(currentWifiInfo.getIpAddress()); } //仍是路由派來的IP,啊啊啊啊(…*……*…) public static String getHostIPAddress(){ WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE); hostDhcpInfo = conMan.getDhcpInfo(); return WifiUtil.intToIp(hostDhcpInfo.serverAddress); } //get current network id public static int getCurrentNetworkId(){ WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE); currentWifiInfo = conMan.getConnectionInfo(); return currentWifiInfo.getNetworkId(); } //get current connected wifi info //get wifi connect state public static int networkState(){ WifiManager conMan = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE); return conMan.isWifiEnabled()? 1 : 0; } //鏈接到新設備 public static void connectDevice(String ssid,String passwd){ // WifiConfiguration wc = new WifiConfiguration(); // wc.SSID = "\""+ssid+"\""; // wc.preSharedKey = "\""+passwd+"\""; // 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); } //生成一個網絡配置 public WifiConfiguration CreateWifiInfo(ScanResult scanresult,String Password) { WifiConfiguration wc = new WifiConfiguration(); wc.SSID = "\""+scanresult.SSID+"\""; //<span style="color: rgb(255, 0, 0); ">這個地方必定要注意了。旁邊的「是不可以省略的。密碼的地方也同樣。</span> 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+"\""; //<span style="color: rgb(255, 0, 0); ">這個地方必定要注意了。旁邊的「是不可以省略的。密碼的地方也同樣。</span> // 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; } //根據網絡配置鏈接wifi public 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二進制轉換咱們能理解的地址的類android
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<WifiConfiguration> 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.hc++
#ifndef WIFIMANAGER_H #define WIFIMANAGER_H #include <QObject> #include <QAndroidJniObject> #include <QList> #include <QString> struct WifiInfo{ int level; QString SSID; QString BSSID; QString keytype; }; class WifiManager : public QObject { Q_OBJECT public: explicit WifiManager(QObject *parent = 0); ~WifiManager(); //wifi process Q_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(); //鏈接到wifi Q_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 process Q_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<WifiInfo> wifiList; }; #endif // WIFIMANAGER_H
wifimanager.cppgit
#include "wifimanager.h" #include <QDebug> #include <math.h> #include "jni.h" WifiManager::WifiManager(QObject *parent) : QObject(parent) { } WifiManager::~WifiManager() { } /***********************************wifi cability ********************/ bool WifiManager::isWifiEnable(){ jint state =QAndroidJniObject::callStaticMethod<jint>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "networkState"); return state==1?true:false; } void WifiManager::openWifi(){ QAndroidJniObject::callStaticMethod<void>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "openWifi"); } void WifiManager::closeWifi(){ QAndroidJniObject::callStaticMethod<void>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "closeWifi"); } void WifiManager::scanWifi(){ QAndroidJniObject::callStaticMethod<void>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "scanWifi"); } int WifiManager::getWifiListCount(){ jint count=QAndroidJniObject::callStaticMethod<jint>("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<int>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "getWifiLevel","(I)I",index); qDebug()<<"wifi level: "<<a; if(abs(a)>80){ return 1; } else if(abs(a)>50&&abs(a)<=80){ return 2; } else if(abs(a)<=50){ return 3; } } QString WifiManager::getKeyType(int index){ QAndroidJniObject str=QAndroidJniObject::callStaticObjectMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava", "getWifiKeyType", "(I)Ljava/lang/String;",index); return str.toString(); } //獲取當前鏈接的wifi信息 QString WifiManager::getConntectedWifiSSID(){ QAndroidJniObject str=QAndroidJniObject::callStaticObjectMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava", "getCurrentWifiSSID", "()Ljava/lang/String;"); return str.toString(); } QString WifiManager::getConnectedWifiAddress(){ QAndroidJniObject str=QAndroidJniObject::callStaticObjectMethod("an/qt/extendsQtWithJava/ExtendsQtWithJava", "getHostIPAddress", "()Ljava/lang/String;"); return str.toString(); } //鏈接到wifi void WifiManager::connectToWifi(int id,QString passwd){ QAndroidJniObject str=QAndroidJniObject::fromString(passwd); jint a=QAndroidJniObject::callStaticMethod<jint>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "connectToWifi", "(ILjava/lang/String;)I",id,str.object<jstring>()); qDebug()<<"connect to wifi :"<<a; } void WifiManager::connectToWifiWithoutPasswd(int id){ jint a=QAndroidJniObject::callStaticMethod<jint>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "connectToWifiWithoutPasswd", "(I)I",id); qDebug()<<"connect to wifi :"<<a; } /***********************************wifi cability ********************/ /***********************************多媒體音量控制********************/ int WifiManager::getMaxVolumnStream(){ return QAndroidJniObject::callStaticMethod<int>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "getMaxVolumnStream","()I"); } int WifiManager::getCurrentVolumnStream(){ return QAndroidJniObject::callStaticMethod<int>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "getCurrentVolumnStream","()I"); } void WifiManager::setVolumnStream(int a){ QAndroidJniObject::callStaticMethod<void>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "setVolumnStream","(I)V",a); } /***********************************多媒體音量控制********************/ //獲取屏幕像素密度 double WifiManager::getDentisy(){ return QAndroidJniObject::callStaticMethod<double>("an/qt/extendsQtWithJava/ExtendsQtWithJava", "getDentisy","()D"); } /***********************************user functionds*************************/ void WifiManager::refreshWifiList(){ wifiList.clear(); scanWifi(); int count=getWifiListCount(); for(int a=0;a<count;a++){ WifiInfo info; info.SSID=getWifiSSID(a); info.BSSID=getWifiBSSID(a); info.level=getWifiLevel(a); info.keytype=getKeyType(a); wifiList.append(info); } } int WifiManager::wifiCount(){ return wifiList.count(); } int WifiManager::wifiLevel(int i){ return wifiList.at(i).level; } QString WifiManager::wifiSSID(int i){ return wifiList.at(i).SSID; } QString WifiManager::wifiBSSID(int i){ return wifiList.at(i).BSSID; } QString WifiManager::wifiKeyType(int i){ return wifiList.at(i).keytype; } /***********************************user functionds*************************/
效果截圖:編程