Java Code Examples for android.net.wifi.WifiManager#disconnect()
The following examples show how to use
android.net.wifi.WifiManager#disconnect() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WifiBaseActivity.java From android-wifi-activity with Creative Commons Zero v1.0 Universal | 6 votes |
/** * Start to connect to a specific wifi network */ private void connectToSpecificNetwork() { final WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); WifiInfo wifiInfo = wifi.getConnectionInfo(); if (networkInfo.isConnected() && wifiInfo.getSSID().replace("\"", "").equals(getWifiSSID())) { return; } else { wifi.disconnect(); } progressDialog = ProgressDialog.show(this, getString(R.string.connecting), String.format(getString(R.string.connecting_to_wifi), getWifiSSID())); taskHandler = worker.schedule(new TimeoutTask(), getSecondsTimeout(), TimeUnit.SECONDS); scanReceiver = new ScanReceiver(); registerReceiver(scanReceiver , new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); wifi.startScan(); }
Example 2
Source File: WifiBase.java From android-wifi-activity with Creative Commons Zero v1.0 Universal | 6 votes |
/** * Start to connect to a specific wifi network */ private void connectToSpecificNetwork() { final WifiManager wifi = (WifiManager) getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE); ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); WifiInfo wifiInfo = wifi.getConnectionInfo(); if (networkInfo.isConnected() && wifiInfo.getSSID().replace("\"", "").equals(getWifiSSID())) { return; } else { wifi.disconnect(); } progressDialog = ProgressDialog.show(getContext(), getContext().getString(R.string.connecting), String.format(getContext().getString(R.string.connecting_to_wifi), getWifiSSID())); taskHandler = worker.schedule(new TimeoutTask(), getSecondsTimeout(), TimeUnit.SECONDS); scanReceiver = new ScanReceiver(); getContext().registerReceiver(scanReceiver , new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); wifi.startScan(); }
Example 3
Source File: WifiAutoConnect.java From ShareBox with Apache License 2.0 | 6 votes |
public void startScan() { _wifiManager= (WifiManager) _context.getSystemService(Context.WIFI_SERVICE); _wifiReceiver=new WifiReceiver(_wifiManager,this); _wifiManager.disconnect(); if(!_wifiManager.isWifiEnabled()) _wifiManager.setWifiEnabled(true); IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); // IntentFilter filter=new IntentFilter("wifi_scan_available"); // filter.addAction("wifi_scan_available"); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); _context.registerReceiver(_wifiReceiver,filter); }
Example 4
Source File: WiFiUtils.java From FimiX8-RE with MIT License | 5 votes |
public static void disConnectCurWifi(Context context) { try { WifiManager wifiManager = (WifiManager) context.getSystemService("wifi"); wifiManager.disableNetwork(wifiManager.getConnectionInfo().getNetworkId()); wifiManager.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } }
Example 5
Source File: WiFiProxyUtil.java From WiFiProxySwitcher with Apache License 2.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private boolean unSetWiFiProxySettings5() { try { WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiConfiguration config = getCurrentWifiConfiguration(manager); Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings"); Class[] setProxyParams = new Class[2]; setProxyParams[0] = proxySettings; setProxyParams[1] = ProxyInfo.class; Method setProxy = WifiConfiguration.class.getDeclaredMethod("setProxy", setProxyParams); setProxy.setAccessible(true); Object[] methodParams = new Object[2]; methodParams[0] = Enum.valueOf(proxySettings, "NONE"); methodParams[1] = null; setProxy.invoke(config, methodParams); manager.updateNetwork(config); manager.disconnect(); manager.reconnect(); ToastUtil.showToast(context, "取消Proxy设置成功"); return true; } catch (Exception e) { ToastUtil.showToast(context, "取消Proxy设置失败"); return false; } }
Example 6
Source File: WiFiProxyUtil.java From WiFiProxySwitcher with Apache License 2.0 | 5 votes |
/** * 低于 Android 5.0 系统使用 */ private boolean unsetWiFiProxySettings4() { WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiConfiguration config = getCurrentWifiConfiguration(manager); if (null == config) return false; try { // 获得 linkProperties Object linkProperties = getField(config, "linkProperties"); if (null == linkProperties) return false; // 获得 setHttpProxy 方法 Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties"); Class[] setHttpProxyParams = new Class[1]; setHttpProxyParams[0] = proxyPropertiesClass; Class<?> lpClass = Class.forName("android.net.LinkProperties"); Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams); setHttpProxy.setAccessible(true); // 参数传递空, 表示取消 proxy 设置 Object[] params = new Object[1]; params[0] = null; setHttpProxy.invoke(linkProperties, params); setProxySettings("NONE", config); // 保存设置 manager.updateNetwork(config); manager.disconnect(); manager.reconnect(); ToastUtil.showToast(context, "取消Proxy设置成功"); return true; } catch (Exception e) { ToastUtil.showToast(context, "取消Proxy设置失败"); return false; } }
Example 7
Source File: PNetwork.java From PHONK with GNU General Public License v3.0 | 5 votes |
@PhonkMethod(description = "Connect to mContext given Wifi network with mContext given 'wpa', 'wep', 'open' type and mContext password", example = "") @PhonkMethodParam(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().getApplicationContext().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; } } }
Example 8
Source File: OBConnectionManager.java From GLEXP-Team-onebillion with Apache License 2.0 | 5 votes |
public void disconnectWifi () { MainActivity.log("OBConnectionManager.disconnectWifi"); // forgetAllNetworks(); // WifiManager wifiManager = (WifiManager) MainActivity.mainActivity.getApplicationContext().getSystemService(MainActivity.WIFI_SERVICE); MainActivity.log("OBConnectionManager.disconnectWifi. Disconnected network"); wifiManager.disconnect(); MainActivity.log("OBConnectionManager.disconnectWifi. Disabling wifi"); wifiManager.setWifiEnabled(false); }
Example 9
Source File: ConnectorUtils.java From WifiUtils with Apache License 2.0 | 5 votes |
@RequiresPermission(ACCESS_WIFI_STATE) static boolean disconnectFromWifi(@NonNull final ConnectivityManager connectivityManager, @NonNull final WifiManager wifiManager) { if (isAndroidQOrLater()) { return disconnectAndroidQ(connectivityManager); } return wifiManager.disconnect(); }
Example 10
Source File: WifiUtil.java From ShareBox with Apache License 2.0 | 4 votes |
public static void getNearWifiList(final Context context, final WifiReceiver.IReceiveNewNetWorks listener) { WifiManager manager= (WifiManager) context.getSystemService(Context.WIFI_SERVICE); final WifiReceiver receiver=new WifiReceiver(manager); WifiReceiver.IReceiveNewNetWorks wListener=new WifiReceiver.IReceiveNewNetWorks() { WifiReceiver _receiver=receiver; @Override public void onReceive(ArrayList<ScanResult> list) { listener.onReceive(list); if(_receiver!=null) { try { context.unregisterReceiver(_receiver); _receiver=null; }catch (Exception e) { e.printStackTrace(); } } } }; receiver.setReceiveListener(wListener); manager.disconnect(); if(!manager.isWifiEnabled()) manager.setWifiEnabled(true); IntentFilter filter=new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); context.registerReceiver(receiver,filter); }
Example 11
Source File: WiFiProxyUtil.java From WiFiProxySwitcher with Apache License 2.0 | 4 votes |
/** * 低于 Android 5.0 系统 */ private boolean setWiFiProxySettings4(String ip, int port) { // 获得 WifiConfiguration WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiConfiguration config = getCurrentWifiConfiguration(manager); if (null == config) return false; try { // 从 WifiConfiguration 中获得 linkProperties Object linkProperties = getField(config, "linkProperties"); if (null == linkProperties) return false; // 获得 setHttpProxy 方法 Class<?> proxyPropertiesClass = Class.forName("android.net.ProxyProperties"); Class[] setHttpProxyParams = new Class[1]; setHttpProxyParams[0] = proxyPropertiesClass; Class<?> lpClass = Class.forName("android.net.LinkProperties"); Method setHttpProxy = lpClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams); setHttpProxy.setAccessible(true); // 获得 ProxyProperties 构造方法 Class[] proxyPropertiesCtorParamTypes = new Class[3]; proxyPropertiesCtorParamTypes[0] = String.class; proxyPropertiesCtorParamTypes[1] = int.class; proxyPropertiesCtorParamTypes[2] = String.class; Constructor proxyPropertiesCtor = proxyPropertiesClass.getConstructor(proxyPropertiesCtorParamTypes); // 给构造方法创建参数 Object[] proxyPropertiesCtorParams = new Object[3]; proxyPropertiesCtorParams[0] = ip; proxyPropertiesCtorParams[1] = port; proxyPropertiesCtorParams[2] = null; // 创建 ProxyProperties 的对象 Object proxySettings = proxyPropertiesCtor.newInstance(proxyPropertiesCtorParams); // 反射调用 linkProperties 的 setHttpProxy 方法, 参数为 ProxyProperties Object[] params = new Object[1]; params[0] = proxySettings; setHttpProxy.invoke(linkProperties, params); setProxySettings("STATIC", config); // 保存设置 manager.updateNetwork(config); manager.disconnect(); manager.reconnect(); ToastUtil.showToast(context, "保存Proxy设置成功"); return true; } catch (Exception e) { ToastUtil.showToast(context, "保存Proxy设置失败"); return false; } }
Example 12
Source File: OBConnectionManager.java From GLEXP-Team-onebillion with Apache License 2.0 | 4 votes |
public void connectToNetwork_connectToWifi (final String ssid, final String password, final OBUtils.RunLambdaWithSuccess block) { if (ssid == null) return; // final WifiManager wfMgr = (WifiManager) MainActivity.mainActivity.getApplicationContext().getSystemService(Context.WIFI_SERVICE); // String connectionSSID = wfMgr.getConnectionInfo().getSSID(); if (connectionSSID.startsWith("\"") && connectionSSID.endsWith("\"")) { connectionSSID = connectionSSID.substring(1, connectionSSID.length() - 1); } // SupplicantState connectionState = wfMgr.getConnectionInfo().getSupplicantState(); // MainActivity.log("OBConnectionManager.connectToNetwork_connectToWifi. [" + connectionSSID + "] - [" + connectionState.toString() + "]"); // if (connectionSSID.equals(ssid) && connectionState == SupplicantState.COMPLETED) { MainActivity.log("OBConnectionManager.connectToNetwork_connectToWifi. already connected to the network"); connectToNetWork_complete(true, block); } else if (!ssid.equals(connectionSSID) && connectionState == SupplicantState.COMPLETED) { MainActivity.log("OBConnectionManager.connectToNetwork_connectToWifi. Connected to OTHER Wifi. Disconnecting current WiFi"); // int currentNetworkID = wfMgr.getConnectionInfo().getNetworkId(); // boolean networkDisabled = wfMgr.disableNetwork(currentNetworkID); boolean configurationSaved = wfMgr.saveConfiguration(); boolean disconnected = wfMgr.disconnect(); // if (!networkDisabled || !configurationSaved || !disconnected) { MainActivity.log("OBConnectionManager.connectToNetwork_connectToWifi. FAILED to disconnect from current WiFi. Aborting operation"); } else { connectToNetwork_disableAirplaneMode(ssid, password, block); } } else { MainActivity.log("OBConnectionManager.connectToNetwork_connectToWifi. Wifi not connected. Go to check airplane, enable wifi and scan"); connectToNetwork_disableAirplaneMode(ssid, password, block); //connectToNetwork_scanForWifi(ssid, password, block); } }
Example 13
Source File: WiFiProxyUtil.java From WiFiProxySwitcher with Apache License 2.0 | 4 votes |
/** * 高于 Android 5.0 系统使用 */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) private boolean setWiFiProxySettings5(String ip, int port) { try { WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiConfiguration config = getCurrentWifiConfiguration(manager); Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings"); Class[] setProxyParams = new Class[2]; setProxyParams[0] = proxySettings; setProxyParams[1] = ProxyInfo.class; Method setProxy = WifiConfiguration.class.getDeclaredMethod("setProxy", setProxyParams); setProxy.setAccessible(true); ProxyInfo proxyInfo = ProxyInfo.buildDirectProxy(ip, port); Object[] methodParams = new Object[2]; methodParams[0] = Enum.valueOf(proxySettings, "STATIC"); methodParams[1] = proxyInfo; setProxy.invoke(config, methodParams); manager.updateNetwork(config); String result = getCurrentWifiConfiguration(manager).toString(); String key = "Proxy settings: "; int start = result.indexOf(key) + key.length(); if (result.substring(start, start + 4).equals("NONE")) { throw new RuntimeException("Can't update the Network, you should have the right WifiConfiguration"); } manager.disconnect(); manager.reconnect(); ToastUtil.showToast(context, "保存Proxy设置成功"); return true; } catch (Exception e) { ToastUtil.showToast(context, "保存Proxy设置失败"); return false; } }
Example 14
Source File: WifiManagerModule.java From react-native-wifi-manager with MIT License | 4 votes |
public void connectTo(ScanResult result, String password, String ssid) { //Make new configuration WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + ssid + "\""; String Capabilities = result.capabilities; if (Capabilities.contains("WPA2")) { conf.preSharedKey = "\"" + password + "\""; } else if (Capabilities.contains("WPA")) { conf.preSharedKey = "\"" + password + "\""; } else if (Capabilities.contains("WEP")) { conf.wepKeys[0] = "\"" + password + "\""; conf.wepTxKeyIndex = 0; conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); } else { conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); } //Remove the existing configuration for this netwrok WifiManager mWifiManager = (WifiManager) getReactApplicationContext().getSystemService(Context.WIFI_SERVICE); List<WifiConfiguration> mWifiConfigList = mWifiManager.getConfiguredNetworks(); String comparableSSID = ('"' + ssid + '"'); //Add quotes because wifiConfig.SSID has them for(WifiConfiguration wifiConfig : mWifiConfigList){ if(wifiConfig.SSID.equals(comparableSSID)){ int networkId = wifiConfig.networkId; mWifiManager.removeNetwork(networkId); mWifiManager.saveConfiguration(); } } //Add configuration to Android wifi manager settings... WifiManager wifiManager = (WifiManager) getReactApplicationContext().getSystemService(Context.WIFI_SERVICE); mWifiManager.addNetwork(conf); //Enable it so that android can connect List < WifiConfiguration > list = mWifiManager.getConfiguredNetworks(); for (WifiConfiguration i: list) { if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) { wifiManager.disconnect(); wifiManager.enableNetwork(i.networkId, true); wifiManager.reconnect(); break; } } }
Example 15
Source File: WifiBase.java From android-wifi-activity with Creative Commons Zero v1.0 Universal | 4 votes |
@Override public void onReceive(Context context, Intent intent) { WifiManager wifi = (WifiManager) context.getSystemService(context.WIFI_SERVICE); List<ScanResult> scanResultList = wifi.getScanResults(); boolean found = false; String security = null; for (ScanResult scanResult : scanResultList) { if (scanResult.SSID.equals(getWifiSSID())) { security = getScanResultSecurity(scanResult); found = true; break; // found don't need continue } } if (!found) { // if no wifi network with the specified ssid is not found exit if (progressDialog != null) { progressDialog.dismiss(); } new AlertDialog.Builder(context) .setCancelable(false) .setMessage(String.format(context.getString(R.string.wifi_not_found), getWifiSSID())) .setPositiveButton(context.getString(R.string.exit_app), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { getActivity().unregisterReceiver(ScanReceiver.this); getActivity().finish(); } }) .show(); } else { // configure based on security final WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + getWifiSSID() + "\""; switch (security) { case WEP: conf.wepKeys[0] = "\"" + getWifiPass() + "\""; conf.wepTxKeyIndex = 0; conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); break; case PSK: conf.preSharedKey = "\"" + getWifiPass() + "\""; break; case OPEN: conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); break; } try {context.unregisterReceiver(connectionReceiver);} catch (Exception e) {} // do nothing connectionReceiver = new ConnectionReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); context.registerReceiver(connectionReceiver, intentFilter); int netId = wifi.addNetwork(conf); wifi.disconnect(); wifi.enableNetwork(netId, true); wifi.reconnect(); context.unregisterReceiver(this); } }
Example 16
Source File: PhoneWifiHelper.java From arcusandroid with Apache License 2.0 | 4 votes |
public static boolean disconnectFromWifi (Context context) { final WifiManager wifiManager = getWiFiManager(context); return wifiManager.disconnect(); }
Example 17
Source File: WifiHelper.java From android-wear-gopro-remote with Apache License 2.0 | 4 votes |
public static boolean connectToWifi(Context context, int networkId) { if(networkId <= -3) return false; // Do nothing final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (wifiManager == null) { Logger.error(TAG, "No WiFi manager"); return false; } WifiInfo info = wifiManager.getConnectionInfo(); if(info != null && info.getNetworkId() == networkId) { return true; } if(networkId == -2) { // Disable return wifiManager.setWifiEnabled(false); } if(!wifiManager.isWifiEnabled()) { return false; } if (!wifiManager.disconnect()) { Logger.error(TAG, "WiFi disconnect failed"); return false; } if(networkId == -1) { // Disconnect return true; } if (!wifiManager.enableNetwork(networkId, true)) { Logger.error(TAG, "Could not enable WiFi."); return false; } if (!wifiManager.reconnect()) { Logger.error(TAG, "WiFi reconnect failed"); return false; } return true; }
Example 18
Source File: WifiHelper.java From android-wear-gopro-remote with Apache License 2.0 | 4 votes |
public static boolean connectToWifi(Context context, final String ssid, String password) { int networkId = -1; int c; final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (wifiManager == null) { Logger.error(TAG, "No WiFi manager"); return false; } List<WifiConfiguration> list; if (wifiManager.isWifiEnabled()) { list = wifiManager.getConfiguredNetworks(); } else { if (!wifiManager.setWifiEnabled(true)) { Logger.error(TAG, "Enable WiFi failed"); return false; } c = 0; do { Utils.sleep(500); list = wifiManager.getConfiguredNetworks(); } while (list == null && ++c < 10); } if (list == null) { Logger.error(TAG, "Could not get WiFi network list"); return false; } for (WifiConfiguration i : list) { if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) { networkId = i.networkId; break; } } WifiInfo info; if (networkId < 0) { WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + ssid + "\""; conf.preSharedKey = "\"" + password + "\""; networkId = wifiManager.addNetwork(conf); if (networkId < 0) { Logger.error(TAG, "New WiFi config failed"); return false; } } else { info = wifiManager.getConnectionInfo(); if (info != null) { if (info.getNetworkId() == networkId) { if(Logger.DEBUG) Logger.debug(TAG, "Already connected to " + ssid); return true; } } } if (!wifiManager.disconnect()) { Logger.error(TAG, "WiFi disconnect failed"); return false; } if (!wifiManager.enableNetwork(networkId, true)) { Logger.error(TAG, "Could not enable WiFi."); return false; } if (!wifiManager.reconnect()) { Logger.error(TAG, "WiFi reconnect failed"); return false; } c = 0; do { info = wifiManager.getConnectionInfo(); if (info != null && info.getNetworkId() == networkId && info.getSupplicantState() == SupplicantState. COMPLETED && info.getIpAddress() != 0) { if(Logger.DEBUG) Logger.debug(TAG, "Successfully connected to %s %d", ssid, info.getIpAddress()); return true; } Utils.sleep(500); } while (++c < 30); Logger.error(TAG, "Failed to connect to " + ssid); return false; }