QT Android wifi自动重连开发
小米平板连接无公网的路由器时,断开后不会自动连接,原因是 安卓系统面对无网络连接的wifi不会启动自动重连机制。
查看发现不能自动重连的WIFI会有 NETWORK_SELECTION_PERMANENTLY_DISABLED此项配置,正常的为 NETWORK_SELECTION_ENABLED,所以在自己的软件上开发了自动重连功能,实现选取 已连接过的WiFi进行自动重连。
配置安卓wifi操作权限
其中以下几项与wifi权限有关:
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
PRO文件中添加:
android:QT += androidextras
头文件:
#if defined (Q_OS_ANDROID)
#include <QAndroidJniObject>
#include <QtAndroid>
#endif
通过监听QNetworkConfigurationManager信号判断WiFi是否失去连接
接收到WiFi断开信号时开启重连计时器
QNetworkConfigurationManager mgr;
connect(&mgr,SIGNAL(onlineStateChanged(bool)),this,SLOT(isNetWorkOnline(bool)));
Java代码
//连接配置过的WiFi
public static String connectWifi(String SSID)
{
WifiManager mWifiManager = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE);
int netId = getExitsWifiConfig(SSID).networkId;
if(netId>0)
{
//这个方法的第一个参数是需要连接wifi网络的networkId,第二个参数是指连接当前wifi网络是否需要断开其他网络
//无论是否连接上,都会返回true。
boolean enableNetwork = mWifiManager.enableNetwork(netId, true);
return "succeed";
}
else
{
return "failed";
}
}
//通过SSID获取netID
public static WifiConfiguration getExitsWifiConfig(String SSID)
{
WifiManager mWifiManager = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> wifiConfigurationList = mWifiManager.getConfiguredNetworks();
for (WifiConfiguration wifiConfiguration : wifiConfigurationList)
{
if (wifiConfiguration.SSID.equals("\"" + SSID + "\""))
{
int netId = wifiConfiguration.networkId;
return wifiConfiguration;
}
}
return null;
}
//获取已经连接过的wifi列表
public static String getExitsWifiConfiglist()
{
WifiManager mWifiManager = (WifiManager) m_instance.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> wifiConfigurationList = mWifiManager.getConfiguredNetworks();
String wifiConfiglist ="";
for (WifiConfiguration wifiConfiguration : wifiConfigurationList)
{
//去除名字的首尾
if(wifiConfiglist=="")
{
wifiConfiglist+= wifiConfiguration.SSID.substring(1, wifiConfiguration.SSID.length() - 1);
}
else
{
wifiConfiglist+=","+wifiConfiguration.SSID.substring(1, wifiConfiguration.SSID.length() - 1);
}
}
return wifiConfiglist;
}
QT代码
使用jni的方式调用java代码,连接WiFi
/**
* @brief wifi断开重连
*/
void MainWindow::OnReConnectionWifi()
{
if(++wifireconnect_count<20)
{
showPlainText("正在重新连接无线网络......");
QAndroidJniObject m_activity = QtAndroid::androidActivity();
m_activity.callStaticMethod<void>("com/Battery","openWifi");
//wifi SSID存储在配置文件中
QAndroidJniObject str=QAndroidJniObject::fromString(wifiSSID);
QAndroidJniObject s = m_activity.callStaticObjectMethod("com/Battery",
"connectWifi",
"(Ljava/lang/String;)Ljava/lang/String;",
str.object<jstring>());
if(s.toString()=="succeed")
{
wifireconnect_count=0;
showPlainText("无线网络重连成功");
}
}
else
{
m_connectWifiTimer->stop();
showPlainText("无线网络自动重连失败,请手动连接WLAN:"+wifiSSID);
}
}
获取并显示已经连接过的wifi列表
QAndroidJniObject str2=QAndroidJniObject::callStaticObjectMethod("com/Battery",
"getExitsWifiConfiglist",
"()Ljava/lang/String;");
QString date=str2.toString();
QStringList list = date.split(",");//QString字符串分割函数
ui->wifiList_Cbox->addItems(list);