其实就是代码实现wifi连接,废话不多说,直接上代码
public class MainActivity extends WaterPermissionActivity {
private Runnable r;
private Handler handler;
@BindView(R.id.listview)
ListView listview;
@BindView(R.id.et_password)
EditText et_password;
private List<ScanResult> listWifiAll;
@Override
protected MvcBaseModel getModelImp() {
return null;
}
@Override
protected int getContentLayoutId() {
return R.layout.activity_main;
}
@Override
protected void initWidget() {
requestPermission(ACCESS_FINE_LOCATION);
}
@Override
protected void doAccessFineLocation() {
requestPermission(ACCESS_COARSE_LOCATION);
}
@Override
protected void doAccessCoarseLocation() {
WifiManager wifiManager = ((WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE));
wifiManager.startScan();
listWifiAll = new ArrayList<>();
MainAdapter mainAdapter = new MainAdapter(this, listWifiAll);
listview.setAdapter(mainAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//选定具体的wifi后,拿到对应的wifi信息
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
List<WifiConfiguration> alreadyWifiList = wifiManager.getConfiguredNetworks();
boolean flag = true;
for (int i = 0; i < alreadyWifiList.size(); i++) {
if (listWifiAll.get(position).SSID.equals(alreadyWifiList.get(i).SSID.replaceAll("\"",""))){
//找到了相同配置
flag = false;
@SuppressLint("WifiManagerLeak")
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = new WifiConfiguration();
config.SSID = "\""+listWifiAll.get(position).SSID+"\"";
config.preSharedKey = "\""+alreadyWifiList.get(i).preSharedKey+"\"";//加密wifi
config.hiddenSSID = true;
config.status = WifiConfiguration.Status.ENABLED;
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);//WPA_PSK NONE(非加密)
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
int netId = manager.addNetwork(config);
boolean b = manager.enableNetwork(netId, true);
break;
}
}
if (flag){
//没有找到相同配置
String password = et_password.getText().toString().trim();
if (NullUtil.isStringEmpty(password)){
ToastUtil.toastWord(MainActivity.this,"请输入密码");
return;
}
WifiManager manager = (WifiManager) MainActivity.this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = new WifiConfiguration();
config.SSID = "\""+listWifiAll.get(position).SSID+"\"";
config.preSharedKey = "\""+password+"\"";;//加密wifi
config.hiddenSSID = true;
config.status = WifiConfiguration.Status.ENABLED;
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);//WPA_PSK NONE(非加密)
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
int netId = manager.addNetwork(config);
boolean b = manager.enableNetwork(netId, true);
}
}
});
handler = new Handler();
r = new Runnable() {
@Override
public void run() {
List<ScanResult> listWifi = wifiManager.getScanResults();
for (int i = 0; i < listWifi.size(); i++) {
boolean flag = true;
for (int j = 0; j < listWifiAll.size(); j++) {
if (listWifi.get(i).SSID.equals(listWifiAll.get(j).SSID)){
flag = false;
break;
}
}
if (flag) {
listWifiAll.add(listWifi.get(i));
}
}
mainAdapter.notifyDataSetChanged();
handler.postDelayed(r,2000);
}
};
handler.postDelayed(r,100);
}
}
权限如下
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
这里要说明一下,虽然原理是实现了,但实际操作起来好像并不是那么好用。后续如果有改进我会第一时间写上来。
|