小编简单介绍如何通过调用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;//当前所连接的wifi
public static List wifiList;// wifi列表
public static List 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=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;
}
//根据网络配置连接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二进制转换我们能理解的地址的类
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 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 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()<<"wifi level: "<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("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"connectToWifi",
"(ILjava/lang/String;)I",id,str.object());
qDebug()<<"connect to wifi :"<("an/qt/extendsQtWithJava/ExtendsQtWithJava",
"connectToWifiWithoutPasswd",
"(I)I",id);
qDebug()<<"connect to wifi :"<("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
效果截图: