Android 7.1.1 又出幺蛾子了 —— 再谈 Android 上的 Wifi 连接

在之前的博客文章中,我写了点在 Android 6 系统中连接到指定名称的 Wifi 的体验.然而,在 Android 7 中,有一些东西又变化了.另外就是在那篇文章中我说要提供代码,结果拖到这篇文章,也没有提供.那么,请忽略那篇文章,来看看这篇吧.

不变

首先聊下 Android 6 到 Android 7之间没有变化的一些东西.想要让设备连接到指定名称的热点,那么一定要经历以下的几个过程:

  1. 打开 Wifi
  2. 包装出 WifiConfiguration 对象
  3. WifiConfiguration 对象获得 WifiManager 对象中 WifiConfiguration 的 index
  4. 用 enableNetwork 连接网络
  5. 使用完毕,断开网络然后删除创建出的配置文件
  6. 关闭 Wifi

  1. 在 Android 6 中,如果系统中已经存在了其他的 app 或者是由系统生成的热点信息,那么它将会返回 -1,而在 Android 7上并不是这个设定,是直接返回 index 的值
  2. 在 Android 7 中,无法使用 removeNetwork 这个公开的方法删除一个已有的配置,虽然在删除后,在系统中显示已经被删除,但是当重新关闭再打开 Wifi 开关时,会发现被删除的配置由系统进行创建.这里,需要使用系统的隐藏方法 forget 进行删除

以上就是所有信息了.以下是代码:

package com.xxxxxxxx.wifitest7;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity"; private final static String WIFI_SSID = "TP-LINK_ACA6";
private Button mBtnOpenWifi;
private Button mBtnCloseWifi;
private Button mBtnAddWifi;
private Button mBtnDelWifi;
private Button mBtnConnect;
private Button mBtnDisConnect;
private TextView mTextViewShowResult; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initUi();
} private void initUi() {
mBtnOpenWifi = (Button) findViewById(R.id.btnOpenWifi);
mBtnOpenWifi.setOnClickListener(this);
mBtnCloseWifi = (Button) findViewById(R.id.btnCloseWifi);
mBtnCloseWifi.setOnClickListener(this);
mBtnAddWifi = (Button) findViewById(R.id.btnAddWifi);
mBtnAddWifi.setOnClickListener(this);
mBtnDelWifi = (Button) findViewById(R.id.btnDelWifi) ;
mBtnDelWifi.setOnClickListener(this);
mBtnConnect = (Button) findViewById(R.id.btnConnect);
mBtnConnect.setOnClickListener(this);
mBtnDisConnect = (Button) findViewById(R.id.btnDisConnect);
mBtnDisConnect.setOnClickListener(this);
mTextViewShowResult = (TextView) findViewById(R.id.textViewShowResult);
} @Override
public void onClick(View view) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mTextViewShowResult.setText("");
// "TP-LINK_ACA6"
switch (view.getId()) {
case R.id.btnOpenWifi:
if (openWifi()) {
mTextViewShowResult.setText("正在打开 Wifi,请稍等");
} else {
mTextViewShowResult.setText("打开 Wifi 失败,Wifi 已经开启?");
}
break;
case R.id.btnCloseWifi:
if (closeWifi()) {
mTextViewShowResult.setText("正在关闭 Wifi,请稍等");
} else {
mTextViewShowResult.setText("关闭 Wifi 失败,Wifi 已经关闭?");
}
break;
case R.id.btnAddWifi:
int networkId = addWifiItem(WIFI_SSID);
mTextViewShowResult.setText("得到的 Wifi 的 ID 是:" + networkId);
break;
case R.id.btnDelWifi:
forgetConfiguration(this,createWifiConfiguration(WIFI_SSID));
break;
case R.id.btnConnect:
connectToSpecSsid(addWifiItem(WIFI_SSID));
break;
case R.id.btnDisConnect:
disableSpecSsid(addWifiItem(WIFI_SSID));
break;
}
} public static WifiConfiguration createWifiConfiguration(String SSID) {
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.allowedKeyManagement.clear();
wifiConfiguration.allowedAuthAlgorithms.clear();
wifiConfiguration.hiddenSSID = true; // 无密码
wifiConfiguration.SSID = "\"" + SSID + "\"";
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); return wifiConfiguration;
} private boolean openWifi() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
boolean result = false;
switch (wifiManager.getWifiState()) {
case WifiManager.WIFI_STATE_DISABLED:
case WifiManager.WIFI_STATE_DISABLING:
wifiManager.setWifiEnabled(true);
result = true;
break;
case WifiManager.WIFI_STATE_ENABLED:
case WifiManager.WIFI_STATE_ENABLING:
result = false;
break;
}
return result;
} private boolean closeWifi() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
boolean result = false;
switch (wifiManager.getWifiState()) {
case WifiManager.WIFI_STATE_ENABLED:
case WifiManager.WIFI_STATE_ENABLING:
wifiManager.setWifiEnabled(false);
result = true;
break;
case WifiManager.WIFI_STATE_DISABLED:
case WifiManager.WIFI_STATE_DISABLING:
result = false;
break;
}
return result;
} private int addWifiItem (String ssid) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> wifiConfigurations = wifiManager.getConfiguredNetworks();
int networkId = -10;
if (null != wifiConfigurations) {
for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
if (wifiConfiguration.SSID.equals("\"" + ssid + "\"")) {
networkId = wifiConfiguration.networkId;
break;
}
}
} if (networkId == -10) {
networkId = wifiManager.addNetwork(createWifiConfiguration(ssid));
} return networkId;
} private boolean delWifiItem (String ssid) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> wifiConfigurations = wifiManager.getConfiguredNetworks();
boolean result = false;
int networkId = -10;
if (null != wifiConfigurations) {
for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
if (wifiConfiguration.SSID.equals("\"" + ssid + "\"")) {
networkId = wifiConfiguration.networkId;
Log.i(TAG, "delWifiItem: 找到对应的 ssid,编号为:" + networkId);
break;
}
}
} if (networkId != -10) {
Log.i(TAG, "delWifiItem: 执行删除指定网络的过程");
wifiManager.removeNetwork(networkId);
result = true;
} wifiConfigurations = wifiManager.getConfiguredNetworks();
for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
if (wifiConfiguration.SSID.equals("\"" + ssid + "\"")) {
Log.i(TAG, "delWifiItem: 又找到对应的 ssid,编号为:" + networkId);
result = false;
break;
}
} return result;
} private boolean connectToSpecSsid(int netId) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
return wifiManager.enableNetwork(netId,true);
} private boolean disableSpecSsid(int netId) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
return wifiManager.disableNetwork(netId);
} public static void forgetConfiguration(Context context, WifiConfiguration config) {
if (config == null) {
return;
} WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configuredNetworks = wifiMan.getConfiguredNetworks();
if (configuredNetworks == null) {
Log.e(TAG, "failed to get configured networks");
return;
} for (WifiConfiguration wc : configuredNetworks) {
if (wc != null && wc.SSID != null && TextUtils.equals(wc.SSID, config.SSID)) {
wifiMan.forget(wc.networkId, null);
Log.d(TAG, "forgot network config: " + wc.toString());
break;
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xxxxxx.wifitest7.MainActivity"> <Button
android:id="@+id/btnOpenWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:text="@string/open_wifi"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/> <Button
android:id="@+id/btnCloseWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginTop="32dp"
android:text="@string/close_wifi"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/> <TextView
android:id="@+id/textViewShowResult"
android:layout_width="321dp"
android:layout_height="220dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="@+id/btnOpenWifi"
app:layout_constraintRight_toRightOf="@+id/btnCloseWifi"
app:layout_constraintTop_toBottomOf="@+id/btnConnect"
app:layout_constraintHorizontal_bias="0.484"/> <Button
android:id="@+id/btnConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:text="@string/connect"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnAddWifi"/> <Button
android:id="@+id/btnDisConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginTop="8dp"
android:text="@string/disconnect"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnDelWifi"/> <Button
android:id="@+id/btnAddWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/addwifi"
app:layout_constraintTop_toBottomOf="@+id/btnOpenWifi"
android:layout_marginStart="32dp"
app:layout_constraintLeft_toLeftOf="parent"/> <Button
android:id="@+id/btnDelWifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/delwifi"
app:layout_constraintTop_toBottomOf="@+id/btnCloseWifi"
android:layout_marginEnd="32dp"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
上一篇:Android应用自动更新功能的代码实现(转)


下一篇:UIStackView相关