/** Method is beeing called on any click of an preference... */ public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { if (preference.getKey().equals(KEY_WIFI_BASED_SCREEN)) { mWifiList = mWifiManager.getConfiguredNetworks(); if (mWifiList == null) { return false; } for (WifiConfiguration wifi : mWifiList) { // Friendly SSID-Name String ssid = wifi.SSID.replaceAll("\"", ""); // Add PreferenceScreen for each network PreferenceScreen pref = getPreferenceManager().createPreferenceScreen(this); pref.setPersistent(false); pref.setKey("wifiNetwork" + ssid); pref.setTitle(ssid); Intent intent = new Intent(this, ConnectionSettings.class); intent.putExtra("SSID", ssid); pref.setIntent(intent); if (WifiConfiguration.Status.CURRENT == wifi.status) pref.setSummary(R.string.connected); else pref.setSummary(R.string.notInRange); mWifibasedCategory.addPreference(pref); } } return false; }
// 实时获取 wifiSSID public boolean findSSID(String ssid) { mWifiManager.startScan(); List<ScanResult> wifiList = mWifiManager.getScanResults(); // ? ? for (int i = 0; i < wifiList.size(); i++) { ScanResult wifi = wifiList.get(i); if (wifi.SSID.equals(ssid)) { return true; } } return false; }
// 添加指定WIFI的配置信息,原列表不存在此SSID public int AddWifiConfig(List<ScanResult> wifiList, String ssid, String pwd) { int wifiId = -1; for (int i = 0; i < wifiList.size(); i++) { ScanResult wifi = wifiList.get(i); if (wifi.SSID.equals(ssid)) { Log.i(TAG, "equals"); WifiConfiguration wifiCong = new WifiConfiguration(); wifiCong.SSID = "\"" + wifi.SSID + "\""; // \"转义字符,代表" wifiCong.preSharedKey = "\"" + pwd + "\""; // WPA-PSK密码 wifiCong.hiddenSSID = false; wifiCong.status = WifiConfiguration.Status.ENABLED; wifiId = localWifiManager.addNetwork(wifiCong); // 将配置好的特定WIFI密码信息添加,添加完成后默认是不激活状态,成功返回ID,否则为-1 if (wifiId != -1) { return wifiId; } } } return wifiId; }
// 保持一个wifi信息 public int AddWifiConfig(String ssid, String pwd) { int wifiId = -1; mWifiManager.startScan(); List<ScanResult> wifiList = mWifiManager.getScanResults(); // ???????? // mWifiManager.re for (int i = 0; i < wifiList.size(); i++) { ScanResult wifi = wifiList.get(i); if (wifi.SSID.equals(ssid)) { Log.i("AddWifiConfig", "equals"); WifiConfiguration wifiCong = new WifiConfiguration(); wifiCong.SSID = "\"" + wifi.SSID + "\""; // \"????????" wifiCong.preSharedKey = "\"" + pwd + "\""; // WPA-PSK???? wifiCong.hiddenSSID = false; wifiCong.status = WifiConfiguration.Status.ENABLED; wifiId = mWifiManager.addNetwork( wifiCong); // ?????ú?????WIFI??????????,????????????????????????????ID???????-1 if (wifiId != -1) { return wifiId; } } } return wifiId; }