public void drop(int from, int to) { if (from == to) return; // Sort networks by user selection List<WifiNetwork> networks = mAdapter.getNetworks(); WifiNetwork o = networks.remove(from); networks.add(to, o); // Set the new priorities of the networks int cc = networks.size(); ArrayList<WifiConfiguration> configList = new ArrayList<>(); for (int i = 0; i < cc; i++) { WifiNetwork network = networks.get(i); network.config.priority = cc - i; configList.add(network.config); } mNetworksListView.invalidateViews(); Intent intent = new Intent(ModHwKeys.ACTION_UPDATE_WIFI_CONFIG); intent.putParcelableArrayListExtra(ModHwKeys.EXTRA_WIFI_CONFIG_LIST, configList); intent.putExtra("receiver", mReceiver); WifiPriorityActivity.this.sendBroadcast(intent); }
/** * Update the network: either create a new network or modify an existing network * @param config the new network configuration */ private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) { Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID); if (foundNetworkID != null) { Log.i(TAG, "Removing old configuration for network " + config.SSID); wifiManager.removeNetwork(foundNetworkID); wifiManager.saveConfiguration(); } int networkId = wifiManager.addNetwork(config); if (networkId >= 0) { // Try to disable the current network and start a new one. if (wifiManager.enableNetwork(networkId, true)) { Log.i(TAG, "Associating to network " + config.SSID); wifiManager.saveConfiguration(); } else { Log.w(TAG, "Failed to enable network " + config.SSID); } } else { Log.w(TAG, "Unable to add network " + config.SSID); } }
@Nullable static WifiConfiguration getWifiConfiguration(@NonNull final WifiManager wifiMgr, @NonNull final ScanResult scanResult) { if (scanResult.BSSID == null || scanResult.SSID == null || scanResult.SSID.isEmpty() || scanResult.BSSID.isEmpty()) return null; final String ssid = convertToQuotedString(scanResult.SSID); final String bssid = scanResult.BSSID; final String security = getSecurity(scanResult); final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks(); if (configurations == null) return null; for (final WifiConfiguration config : configurations) { if (bssid.equals(config.BSSID) || ssid.equals(config.SSID)) { final String configSecurity = getSecurity(config); if (Objects.equals(security, configSecurity)) return config; } } return null; }
public static boolean connectToNtutcc(Context context) { try { WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + NTUTCC_SSID + "\""; conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wifiManager.addNetwork(conf); List<WifiConfiguration> list = wifiManager.getConfiguredNetworks(); for (WifiConfiguration i : list) { if (i.SSID != null && i.SSID.equals("\"" + NTUTCC_SSID + "\"")) { wifiManager.disconnect(); wifiManager.enableNetwork(i.networkId, true); wifiManager.reconnect(); return true; } } } catch (Exception e) { } return false; }
@SuppressWarnings("UnusedReturnValue") private static boolean checkForExcessOpenNetworkAndSave(@NonNull final ContentResolver resolver, @NonNull final WifiManager wifiMgr) { final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks(); sortByPriority(configurations); boolean modified = false; int tempCount = 0; final int numOpenNetworksKept = Build.VERSION.SDK_INT >= 17 ? Settings.Secure.getInt(resolver, Settings.Global.WIFI_NUM_OPEN_NETWORKS_KEPT, 10) : Settings.Secure.getInt(resolver, Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT, 10); for (int i = configurations.size() - 1; i >= 0; i--) { final WifiConfiguration config = configurations.get(i); if (Objects.equals(ConfigSecurities.SECURITY_NONE, ConfigSecurities.getSecurity(config))) { tempCount++; if (tempCount >= numOpenNetworksKept) { modified = true; wifiMgr.removeNetwork(config.networkId); } } } return !modified || wifiMgr.saveConfiguration(); }
public static void removeSSIDFromConfiguredNetwork(Context context, String ssid) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (!wifiManager.isWifiEnabled()) wifiManager.setWifiEnabled(true); List<WifiConfiguration> configuredWifiList = wifiManager.getConfiguredNetworks(); for (int x = 0; x < configuredWifiList.size(); x++) { WifiConfiguration i = configuredWifiList.get(x); if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) { Log.w(TAG, "Removing network: " + i.SSID); wifiManager.removeNetwork(i.networkId); return; } } }
@Override protected void writeToEntropyPool(DataOutputStream out) throws IOException { super.writeToEntropyPool(out); out.writeInt(android.os.Process.myPid()); out.writeInt(android.os.Process.myTid()); out.writeInt(android.os.Process.myUid()); if (Build.FINGERPRINT != null) out.writeUTF(Build.FINGERPRINT); if (Build.SERIAL != null) out.writeUTF(Build.SERIAL); ContentResolver contentResolver = appContext.getContentResolver(); String id = Settings.Secure.getString(contentResolver, ANDROID_ID); if (id != null) out.writeUTF(id); Parcel parcel = Parcel.obtain(); WifiManager wm = (WifiManager) appContext.getSystemService(WIFI_SERVICE); List<WifiConfiguration> configs = wm.getConfiguredNetworks(); if (configs != null) { for (WifiConfiguration config : configs) parcel.writeParcelable(config, 0); } BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter(); if (bt != null) { for (BluetoothDevice device : bt.getBondedDevices()) parcel.writeParcelable(device, 0); } out.write(parcel.marshall()); parcel.recycle(); }
@Rpc( description = "Clears all configured networks. This will only work if all configured " + "networks were added through this MBS instance" ) public void wifiClearConfiguredNetworks() throws WifiManagerSnippetException { List<WifiConfiguration> unremovedConfigs = mWifiManager.getConfiguredNetworks(); List<WifiConfiguration> failedConfigs = new ArrayList<>(); if (unremovedConfigs == null) { throw new WifiManagerSnippetException( "Failed to get a list of configured networks. Is wifi disabled?"); } for (WifiConfiguration config : unremovedConfigs) { if (!mWifiManager.removeNetwork(config.networkId)) { failedConfigs.add(config); } } if (!failedConfigs.isEmpty()) { throw new WifiManagerSnippetException("Failed to remove networks: " + failedConfigs); } }
public int connectToAP(Context context, String networkSSID, String networkPasskey) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); for (ScanResult result : wifiManager.getScanResults()) { if (result.SSID.equals(networkSSID)) { String securityMode = getScanResultSecurity(result); WifiConfiguration wifiConfiguration = createAPConfiguration(networkSSID, networkPasskey, securityMode); int res = wifiManager.addNetwork(wifiConfiguration); Log.d(TAG, "# addNetwork returned " + res); boolean b = wifiManager.enableNetwork(res, true); Log.d(TAG, "# enableNetwork returned " + b); wifiManager.setWifiEnabled(true); boolean changeHappen = wifiManager.saveConfiguration(); if (res != -1 && changeHappen) { Log.d(TAG, "# Change happen: " + networkSSID); } else { Log.d(TAG, "# Change NOT happen"); } return res; } } return -1; }
/** * Enable Wi-Fi Soft AP (hotspot). * * @param configuration The same format as the param wifiNetworkConfig param for wifiConnect. * @throws Throwable */ @Rpc(description = "Enable Wi-Fi Soft AP (hotspot).") public void wifiEnableSoftAp(@Nullable JSONObject configuration) throws Throwable { // If no configuration is provided, the existing configuration would be used. WifiConfiguration wifiConfiguration = null; if (configuration != null) { wifiConfiguration = JsonDeserializer.jsonToWifiConfig(configuration); // Have to trim off the extra quotation marks since Soft AP logic interprets // WifiConfiguration.SSID literally, unlike the WifiManager connection logic. wifiConfiguration.SSID = JsonSerializer.trimQuotationMarks(wifiConfiguration.SSID); } if (!(boolean) Utils.invokeByReflection( mWifiManager, "setWifiApEnabled", wifiConfiguration, true)) { throw new WifiManagerSnippetException("Failed to initiate turning on Wi-Fi Soft AP."); } if (!Utils.waitUntil(() -> wifiIsApEnabled() == true, 60)) { throw new WifiManagerSnippetException( "Timed out after 60s waiting for Wi-Fi Soft AP state to turn on with configuration: " + configuration); } }
public static boolean wifiConnection(Context context, String wifiSSID, String password) { WifiManager wifi = (WifiManager) context.getSystemService("wifi"); String strQuotationSSID = "\"" + wifiSSID + "\""; WifiInfo wifiInfo = wifi.getConnectionInfo(); if (wifiInfo != null && (wifiSSID.equals(wifiInfo.getSSID()) || strQuotationSSID.equals(wifiInfo.getSSID()))) { return true; } List<ScanResult> scanResults = wifi.getScanResults(); if (scanResults == null || scanResults.size() == 0) { return false; } for (int nAllIndex = scanResults.size() - 1; nAllIndex >= 0; nAllIndex--) { String strScanSSID = ((ScanResult) scanResults.get(nAllIndex)).SSID; if (wifiSSID.equals(strScanSSID) || strQuotationSSID.equals(strScanSSID)) { WifiConfiguration config = new WifiConfiguration(); config.SSID = strQuotationSSID; config.preSharedKey = "\"" + password + "\""; config.status = 2; return wifi.enableNetwork(wifi.addNetwork(config), false); } } return false; }
public boolean setWifiApEnabled(boolean enabled) { if (Constants.DEBUG) { Log.d(TAG, "setWifiApEnabled(" + enabled + ")"); } if (enabled) { // disable WiFi in any case mWifiManager.setWifiEnabled(false); } try { // TODO comment from here /* Method getWifiApConfigurationMethod = mWifiManager.getClass().getMethod("getWifiApConfiguration"); Object config = getWifiApConfigurationMethod.invoke(mWifiManager); */ // configuration = null works for many devices Method setWifiApEnabledMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); return (Boolean) setWifiApEnabledMethod.invoke(mWifiManager, null, enabled); } catch (Exception e) { Log.e(TAG, "", e); return false; } }
public void TestWiFi() { WifiInfo wifiInfo = getWifiInfo(); wifiAdmin.openWifi(); WifiConfiguration tempConfig = wifiAdmin.IsExsits(wifiInfo.ssid); if (tempConfig != null && tempConfig.networkId != -1) { Log.d("XLight", "remove ssid at:" + tempConfig.networkId); wifiAdmin.RemoveSSID(tempConfig.networkId); } int type = 1; if (wifiInfo.capabilities.toUpperCase().contains("WPA")) { type = 3; } else if (wifiInfo.capabilities.toUpperCase().contains("WEP")) { type = 2; } wifiAdmin.addNetwork(wifiAdmin.CreateWifiInfo(wifiInfo.ssid, wifiInfo.password, type)); }
public void connectWifi(String ssid) { //停止扫描 stopScanWifi = true; updateWifiHandler.removeCallbacks(runnable); Log.d("XLight", "connect to photon wifi"); //连接到此网络 WifiAdmin wifiAdmin = new WifiAdmin(this); WifiConfiguration tempConfig = wifiAdmin.IsExsits(ssid); if (tempConfig != null && tempConfig.networkId != -1) { Log.d("XLight", "photon netId:" + tempConfig.networkId); mWifiManager.enableNetwork(tempConfig.networkId, true); } else wifiAdmin.addNetwork(wifiAdmin.CreateWifiInfo(ssid, "", 1)); //连接失败回到自己的网络 mWifiManager.reconnect(); }
public void onClick(View v) { out.setText(""); out.append("\n\nConfigured Networks:"); // Get IP Address int ipAddress = wifimanager.getConnectionInfo().getIpAddress(); out.append("\nThe ip address is " + Formatter.formatIpAddress(ipAddress)); // Get configured networks List<WifiConfiguration> configuredNetworks = wifimanager.getConfiguredNetworks(); for (WifiConfiguration conf : configuredNetworks) { out.append(String.format("\n%s", conf.SSID)); } wifimanager.startScan(); }
/** * Helper method to decide whether to bypass conference WiFi setup. Return true if * WiFi AP is already configured (WiFi adapter enabled) or WiFi configuration is complete * as per shared preference. */ public static boolean shouldBypassWiFiSetup(final Context context) { final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); // Is WiFi on? if (wifiManager.isWifiEnabled()) { // Check for existing APs. final List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks(); final String conferenceSSID = getConferenceWifiConfig().SSID; for(WifiConfiguration config : configs) { if (conferenceSSID.equalsIgnoreCase(config.SSID)) return true; } } return WIFI_CONFIG_DONE.equals(getWiFiConfigStatus(context)); }
@Nullable static WifiConfiguration getWifiConfiguration(@NonNull final WifiManager wifiMgr, @NonNull final WifiConfiguration configToFind) { final String ssid = configToFind.SSID; if (ssid.isEmpty()) { return null; } final String bssid = configToFind.BSSID; final String security = getSecurity(configToFind); final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks(); if (configurations == null) { wifiLog("NULL configs"); return null; } for (final WifiConfiguration config : configurations) { if (bssid.equals(config.BSSID) || ssid.equals(config.SSID)) { final String configSecurity = getSecurity(config); if (Objects.equals(security, configSecurity)) return config; } } wifiLog("Couldn't find " + ssid); return null; }
private static int getMaxPriority(@NonNull final WifiManager wifiManager) { final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks(); int pri = 0; for (final WifiConfiguration config : configurations) { if (config.priority > pri) { pri = config.priority; } } return pri; }
private static int shiftPriorityAndSave(@NonNull final WifiManager wifiMgr) { final List<WifiConfiguration> configurations = wifiMgr.getConfiguredNetworks(); sortByPriority(configurations); final int size = configurations.size(); for (int i = 0; i < size; i++) { final WifiConfiguration config = configurations.get(i); config.priority = i; wifiMgr.updateNetwork(config); } wifiMgr.saveConfiguration(); return size; }
private static boolean connectToConfiguredNetwork(@NonNull WifiManager wifiManager, @Nullable WifiConfiguration config, boolean reassociate) { if (config == null) return false; if (Build.VERSION.SDK_INT >= 23) return disableAllButOne(wifiManager, config) && (reassociate ? wifiManager.reassociate() : wifiManager.reconnect()); int oldPri = config.priority; // Make it the highest priority. int newPri = getMaxPriority(wifiManager) + 1; if (newPri > MAX_PRIORITY) { newPri = shiftPriorityAndSave(wifiManager); config = ConfigSecurities.getWifiConfiguration(wifiManager, config); if (config == null) return false; } // Set highest priority to this configured network config.priority = newPri; int networkId = wifiManager.updateNetwork(config); if (networkId == -1) return false; // Do not disable others if (!wifiManager.enableNetwork(networkId, false)) { config.priority = oldPri; return false; } if (!wifiManager.saveConfiguration()) { config.priority = oldPri; return false; } // We have to retrieve the WifiConfiguration after save. config = ConfigSecurities.getWifiConfiguration(wifiManager, config); return config != null && disableAllButOne(wifiManager, config) && (reassociate ? wifiManager.reassociate() : wifiManager.reconnect()); }
private static boolean disableAllButOne(@NonNull final WifiManager wifiManager, @Nullable final WifiConfiguration config) { @Nullable final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks(); if (configurations == null || config == null || configurations.isEmpty()) return false; boolean result = false; for (WifiConfiguration wifiConfig : configurations) if (wifiConfig.networkId == config.networkId) result = wifiManager.enableNetwork(wifiConfig.networkId, true); else wifiManager.disableNetwork(wifiConfig.networkId); wifiLog("disableAllButOne " + result); return result; }
@SuppressWarnings("UnusedReturnValue") private static boolean disableAllButOne(@NonNull final WifiManager wifiManager, @Nullable final ScanResult scanResult) { @Nullable final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks(); if (configurations == null || scanResult == null || configurations.isEmpty()) return false; boolean result = false; for (WifiConfiguration wifiConfig : configurations) if (Objects.equals(scanResult.BSSID, wifiConfig.BSSID) && Objects.equals(scanResult.SSID, trimQuotes(wifiConfig.SSID))) result = wifiManager.enableNetwork(wifiConfig.networkId, true); else wifiManager.disableNetwork(wifiConfig.networkId); return result; }
@ProtoMethod(description = "Connect to mContext given Wifi network with mContext given 'wpa', 'wep', 'open' type and mContext password", example = "") @ProtoMethodParam(params = {"ssidName", "type", "password"}) public void connectWifi(String networkSSID, String type, String networkPass) { WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String // should contain ssid in quotes if (type.equals("wep")) { // wep conf.wepKeys[0] = "\"" + networkPass + "\""; conf.wepTxKeyIndex = 0; conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); } else if (type.equals("wpa")) { // wpa conf.preSharedKey = "\"" + networkPass + "\""; } else if (type.equals("open")) { // open conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); } WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); wifiManager.addNetwork(conf); List<WifiConfiguration> list = wifiManager.getConfiguredNetworks(); for (WifiConfiguration i : list) { if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) { wifiManager.disconnect(); wifiManager.enableNetwork(i.networkId, true); wifiManager.reconnect(); break; } } }
/** * This method takes a given String, searches the current list of configured WiFi * networks, and returns the networkId for the network if the SSID matches. If not, * it returns -1. */ private int ssidToNetworkId(String ssid) { List<WifiConfiguration> currentNetworks = wifiManager.getConfiguredNetworks(); int networkId = -1; // For each network in the list, compare the SSID with the given one for (WifiConfiguration test : currentNetworks) { if ( test.SSID.equals(ssid) ) { networkId = test.networkId; } } return networkId; }
static boolean cleanPreviousConfiguration(@NonNull final WifiManager wifiManager, @Nullable final WifiConfiguration config) { //On Android 6.0 (API level 23) and above if my app did not create the configuration in the first place, it can not remove it either. wifiLog("Attempting to remove previous network config..."); if (config == null) return true; if (wifiManager.removeNetwork(config.networkId)) { wifiManager.saveConfiguration(); return true; } return false; }
private static WifiConfiguration changeNetworkCommon(WifiParsedResult wifiResult) { WifiConfiguration config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); // Android API insists that an ascii SSID must be quoted to be correctly handled. config.SSID = quoteNonHex(wifiResult.getSsid()); config.hiddenSSID = wifiResult.isHidden(); return config; }
private void connectPreviousNetwork(String ssidToAdd, String password) { ScanResult scanResult = NetworkUtil.getWifiScanResultWithSSID(mContext, ssidToAdd); if (scanResult == null) { Timber.i("connectPreviousNetwork>> Not found scan result = %s", ssidToAdd); checkParams(); return; } WifiNetworkUtils.getInstance(mContext).clearCallback(); WifiConfiguration configuration = NetworkUtil.getWifiConfigurationWithInfo(mContext, ssidToAdd, NetworkUtil.getScanResultSecurity(scanResult), password); WifiNetworkUtils.getInstance(mContext).connectToWifi(configuration, mContext, new WifiNetworkUtils.BitbiteNetworkUtilsCallback() { @Override public void successfullyConnectedToNetwork(String ssid) { mIsSearching = true; Timber.i("connectPreviousNetwork>> Connected to SSID in profile added to SL device: \"" + ssidToAdd + "\"." + " Searching for new SL devices in the local network for cfg verification: Activating Ping Bcast + UDPBcastServer + mDNS discovery"); mFindDevicePresenter.startPing(); mFindDevicePresenter.restartUdp(); if (!mDeviceFound) { scanForDevices(); } } @Override public void failedToConnectToNetwork(WifiNetworkUtils.WifiConnectionFailure failure) { Timber.e("connectPreviousNetwork>> Connect old network for confirm config device is failed = %s", failure.toString()); propagateError(mConfigurationToyNetOutput, new ConfigurationToyNetException(ConfigurationToyNetException.Reason.CANNOT_CONNECT_PREVIOS_WIFI, failure.toString())); } }, true); }
public int AddNetwork(WifiConfiguration configuration) { int configurationId = mWifiManager.addNetwork(configuration); boolean b = mWifiManager.enableNetwork(configurationId, true); System.out.println("configurationId---------->" + configurationId); System.out.println("b---------->" + b); return configurationId; }
private static Integer findNetworkInExistingConfig(WifiManager wifiManager, String ssid) { Iterable<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals(ssid)) { return existingConfig.networkId; } } return null; }
@Override protected Void doInBackground(Void... params) { WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + networkSSID + "\""; conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); WifiManager wifiManager = (WifiManager) getBaseContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifiManager.addNetwork(conf); if (!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true); wifiManager.startScan(); } int netId = wifiManager.addNetwork(conf); wifiManager.disconnect(); wifiManager.enableNetwork(netId, true); wifiManager.reconnect(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Joined to " + networkSSID, Toast.LENGTH_SHORT).show(); System.out.println("SUCCESS "); } }); return null; }
private void uiUpdateWifiNetwork() { viewWifiId.setText(FDroidApp.ipAddressString); viewWifiId.setVisibility(TextUtils.isEmpty(FDroidApp.ipAddressString) ? View.GONE : View.VISIBLE); WifiApControl wifiAp = WifiApControl.getInstance(getActivity()); if (wifiAp != null && wifiAp.isWifiApEnabled()) { WifiConfiguration config = wifiAp.getConfiguration(); viewWifiNetwork.setText(getContext().getString(R.string.swap_active_hotspot, config.SSID)); } else if (TextUtils.isEmpty(FDroidApp.ssid)) { // not connected to or setup with any wifi network viewWifiNetwork.setText(R.string.swap_no_wifi_network); } else { // connected to a regular wifi network viewWifiNetwork.setText(FDroidApp.ssid); } }
public WiFiAdmin(Context context) { // 取得wifimannager mWifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); // 取得wifiinfo mWifiInfo = mWifiManager.getConnectionInfo(); mWifiList=new ArrayList<ScanResult>(); mWifiConfigurations=new ArrayList<WifiConfiguration>(); }
/** * This method uses the callbackContext.success method to send a JSONArray * of the currently configured networks. * * @param callbackContext A Cordova callback context * @param data JSON Array, with [0] being SSID to connect * @return true if network disconnected, false if failed */ private boolean listNetworks(CallbackContext callbackContext) { Log.d(TAG, "WifiWizard: listNetworks entered."); List<WifiConfiguration> wifiList = wifiManager.getConfiguredNetworks(); JSONArray returnList = new JSONArray(); for (WifiConfiguration wifi : wifiList) { returnList.put(wifi.SSID); } callbackContext.success(returnList); return true; }
public void addNetwork(WifiConfiguration wcg) { int wcgID = wcg.networkId; if (wcg.networkId == -1) wcgID = mWifiManager.addNetwork(wcg); boolean b = mWifiManager.enableNetwork(wcgID, true); System.out.println("a--" + wcgID); System.out.println("b--" + b); }
private void reloadNetworks() { mNetworks = new ArrayList<WifiNetwork>(); List<WifiConfiguration> networks = mWifiManager.getConfiguredNetworks(); if (networks == null) return; // Sort network list by priority (or by network id if the priority is the same) Collections.sort(networks, new Comparator<WifiConfiguration>() { @Override public int compare(WifiConfiguration lhs, WifiConfiguration rhs) { // > priority -- > lower position if (lhs.priority < rhs.priority) return 1; if (lhs.priority > rhs.priority) return -1; // < network id -- > lower position if (lhs.networkId < rhs.networkId) return -1; if (lhs.networkId > rhs.networkId) return 1; return 0; } }); // read trusted SSIDs from prefs Set<String> trustedNetworks = mPrefs.getStringSet(PREF_KEY_WIFI_TRUSTED, new HashSet<String>()); for (WifiConfiguration c : networks) { WifiNetwork wn = new WifiNetwork(c); wn.trusted = trustedNetworks.contains(filterSSID(c.SSID)); mNetworks.add(wn); } // remove forgotten networks from trusted list boolean shouldUpdatePrefs = false; for (String ssid : trustedNetworks) { if (!containsNetwork(ssid)) { shouldUpdatePrefs = true; break; } } if (shouldUpdatePrefs) { saveTrustedNetworks(); } }
private WifiConfiguration IsExsits(String SSID) { List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals("\""+SSID+"\"")) { return existingConfig; } } return null; }
private WifiConfiguration isExsits(String SSID) { List<WifiConfiguration> existingConfigs = wifiManager .getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals("\"" + SSID + "\"")) { return existingConfig; } } return null; }
@Override public void run() { // 打开wifi openWifi(); // 开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi // 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句 while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) { try { // 为了避免程序一直while循环,让它睡个100毫秒检测…… Thread.sleep(100); } catch (InterruptedException ie) { LogUtils.e(ie.toString()); } } WifiConfiguration tempConfig = isExsits(ssid); if (tempConfig != null) { boolean b = wifiManager.enableNetwork(tempConfig.networkId, true); } else { WifiConfiguration wifiConfig = createWifiInfo(ssid, password, type); if (wifiConfig == null) { LogUtils.d("wifiConfig is null!"); return; } int netID = wifiManager.addNetwork(wifiConfig); boolean enabled = wifiManager.enableNetwork(netID, true); LogUtils.d("enableNetwork status enable=" + enabled); boolean connected = wifiManager.reconnect(); LogUtils.d("enableNetwork connected=" + connected); } }