Java Code Examples for android.net.wifi.WifiInfo#getNetworkId()
The following examples show how to use
android.net.wifi.WifiInfo#getNetworkId() .
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: WifiWizard2.java From WifiWizard2 with Apache License 2.0 | 6 votes |
/** * This method returns the connected WiFi network ID (if connected) * * @return -1 if no network connected, or network id if connected */ private int getConnectedNetId() { int networkId = -1; WifiInfo info = wifiManager.getConnectionInfo(); if (info == null) { Log.d(TAG, "Unable to read wifi info"); return networkId; } networkId = info.getNetworkId(); if (networkId == -1) { Log.d(TAG, "NO_CURRENT_NETWORK_FOUND"); } return networkId; }
Example 2
Source File: WifiHelper.java From android-wear-gopro-remote with Apache License 2.0 | 6 votes |
public static int getCurrentWiFiNetworkId(Context context) { final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if(wifiManager == null) { return -3; // Do nothing } if(!wifiManager.isWifiEnabled()) { return -2; // Disable } WifiInfo info = wifiManager.getConnectionInfo(); if(info == null || info.getNetworkId() < 0) { return -1; // Disconnect } return info.getNetworkId(); }
Example 3
Source File: WifiConnector.java From Android with Apache License 2.0 | 6 votes |
@Override public void onReceive(Context context, Intent intent) { if (!WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(intent.getAction())) { return; } mLock.lock(); WifiInfo info = mWifiManager.getConnectionInfo(); if ( info.getNetworkId()==mNetworkID && info.getSupplicantState() == SupplicantState.COMPLETED ) { mIsConnnected = true; mCondition.signalAll(); } mLock.unlock(); }
Example 4
Source File: SyncService.java From rcloneExplorer with MIT License | 6 votes |
private boolean checkWifiOnAndConnected() { WifiManager wifiMgr = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); if (wifiMgr == null) { return false; } if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); return wifiInfo.getNetworkId() != -1; } else { return false; // Wi-Fi adapter is OFF } }
Example 5
Source File: UploadService.java From rcloneExplorer with MIT License | 6 votes |
private boolean checkWifiOnAndConnected() { WifiManager wifiMgr = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); if (wifiMgr == null) { return false; } if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); return wifiInfo.getNetworkId() != -1; } else { return false; // Wi-Fi adapter is OFF } }
Example 6
Source File: WifiHelper.java From android-wear-gopro-remote with Apache License 2.0 | 6 votes |
public static String getCurrentWifiName(Context context) { final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if(wifiManager == null) { return null; } WifiInfo info = wifiManager.getConnectionInfo(); if(info == null || info.getNetworkId() < 0) { return null; } String ssid = info.getSSID(); if(ssid != null && ssid.startsWith("\"") && ssid.endsWith("\"")) { ssid = ssid.substring(1, ssid.length() - 1); } return ssid; }
Example 7
Source File: ProvisioningValuesLoader.java From android-NfcProvisioning with Apache License 2.0 | 6 votes |
private void loadSystemValues(HashMap<String, String> values) { Context context = getContext(); //noinspection deprecation putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, "com.example.android.deviceowner"); if (Build.VERSION.SDK_INT >= 23) { putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, "com.example.android.deviceowner/.DeviceOwnerReceiver"); } putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_LOCALE, CompatUtils.getPrimaryLocale(context.getResources().getConfiguration()).toString()); putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_TIME_ZONE, TimeZone.getDefault().getID()); if (!values.containsKey(DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID)) { WifiManager wifiManager = (WifiManager) context .getSystemService(Activity.WIFI_SERVICE); WifiInfo info = wifiManager.getConnectionInfo(); if (info.getNetworkId() != -1) { // Connected to network values.put(DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID, trimSsid(info.getSSID())); } } }
Example 8
Source File: DeviceUtils.java From PrivacyStreams with Apache License 2.0 | 5 votes |
/** * Get the BSSID of the currently-connected WiFi AP * @param context a Context instance * @return the BSSID of WiFi, or null if the device is not connected */ // @RequiresPermission(value = Manifest.permission.ACCESS_WIFI_STATE) public static String getWifiBSSID(Context context) { WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifiManager == null || !wifiManager.isWifiEnabled()) return null; WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo == null || wifiInfo.getNetworkId() == -1) return null; return wifiInfo.getBSSID(); }
Example 9
Source File: Transformer.java From WiFiAnalyzer with GNU General Public License v3.0 | 5 votes |
@NonNull WiFiConnection transformWifiInfo(WifiInfo wifiInfo) { if (wifiInfo == null || wifiInfo.getNetworkId() == -1) { return WiFiConnection.EMPTY; } return new WiFiConnection( WiFiUtils.convertSSID(wifiInfo.getSSID()), wifiInfo.getBSSID(), WiFiUtils.convertIpAddress(wifiInfo.getIpAddress()), wifiInfo.getLinkSpeed()); }
Example 10
Source File: ApConnector.java From particle-android with Apache License 2.0 | 5 votes |
private static boolean isCurrentlyConnectedToAWifiNetwork(WifiInfo currentConnectionInfo) { return (currentConnectionInfo != null && truthy(currentConnectionInfo.getSSID()) && currentConnectionInfo.getNetworkId() != -1 // yes, this happens. Thanks, Android. && !"0x".equals(currentConnectionInfo.getSSID())); }
Example 11
Source File: WifiScanWorker.java From PhoneProfilesPlus with Apache License 2.0 | 5 votes |
private static String getSSID(WifiManager wifiManager, WifiInfo wifiInfo, List<WifiSSIDData> wifiConfigurationList) { String SSID = wifiInfo.getSSID(); if (SSID == null) SSID = ""; SSID = SSID.replace("\"", ""); if (SSID.isEmpty()) { if (wifiConfigurationList != null) { for (WifiSSIDData wifiConfiguration : wifiConfigurationList) { /*if ((wifiConfiguration.bssid != null) && (wifiConfiguration.bssid.equals(wifiInfo.getBSSID()))) return wifiConfiguration.ssid.replace("\"", "");*/ if ((wifiConfiguration.ssid != null) && (wifiConfiguration.ssid.equals(wifiInfo.getSSID()))) return wifiConfiguration.ssid.replace("\"", ""); } } } if (SSID.equals("<unknown ssid>")) { List<WifiConfiguration> listOfConfigurations = wifiManager.getConfiguredNetworks(); if (listOfConfigurations != null) { for (int index = 0; index < listOfConfigurations.size(); index++) { WifiConfiguration configuration = listOfConfigurations.get(index); if (configuration.networkId == wifiInfo.getNetworkId()) { return configuration.SSID; } } } } return SSID; }
Example 12
Source File: HandsetFragment.java From spydroid-ipcamera with GNU General Public License v3.0 | 5 votes |
private void displayIpAddress() { WifiManager wifiManager = (WifiManager) mApplication.getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifiManager.getConnectionInfo(); String ipaddress = null; if (info!=null && info.getNetworkId()>-1) { int i = info.getIpAddress(); String ip = String.format(Locale.ENGLISH,"%d.%d.%d.%d", i & 0xff, i >> 8 & 0xff,i >> 16 & 0xff,i >> 24 & 0xff); mLine1.setText(mHttpServer.isHttpsEnabled()?"https://":"http://"); mLine1.append(ip); mLine1.append(":"+mHttpServer.getHttpPort()); mLine2.setText("rtsp://"); mLine2.append(ip); mLine2.append(":"+mRtspServer.getPort()); streamingState(0); } else if((ipaddress = Utilities.getLocalIpAddress(true)) != null) { mLine1.setText(mHttpServer.isHttpsEnabled()?"https://":"http://"); mLine1.append(ipaddress); mLine1.append(":"+mHttpServer.getHttpPort()); mLine2.setText("rtsp://"); mLine2.append(ipaddress); mLine2.append(":"+mRtspServer.getPort()); streamingState(0); } else { streamingState(2); } }
Example 13
Source File: NetworkUtils.java From zephyr with MIT License | 5 votes |
public static boolean isConnectedToWifi(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); return connectivityManager != null && connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork()).hasTransport(NetworkCapabilities.TRANSPORT_WIFI); } WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifiMgr != null && wifiMgr.isWifiEnabled()) { WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); return wifiInfo.getNetworkId() != -1; } else { return false; } }
Example 14
Source File: DeviceUtils.java From PrivacyStreams with Apache License 2.0 | 5 votes |
/** * Check whether WiFi is connected * @param context a Context instance * @return true if Wifi is connected */ // @RequiresPermission(value = Manifest.permission.ACCESS_WIFI_STATE) public static boolean isWifiConnected(Context context) { WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifiManager == null || !wifiManager.isWifiEnabled()) return false; WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo == null || wifiInfo.getNetworkId() == -1) return false; return wifiInfo.getSupplicantState() == SupplicantState.ASSOCIATED; }
Example 15
Source File: WifiWizard2.java From WifiWizard2 with Apache License 2.0 | 5 votes |
@Override protected String[] doInBackground(Object... params) { this.callbackContext = (CallbackContext) params[0]; int networkIdToConnect = (Integer) params[1]; final int TIMES_TO_RETRY = 15; for (int i = 0; i < TIMES_TO_RETRY; i++) { WifiInfo info = wifiManager.getConnectionInfo(); NetworkInfo.DetailedState connectionState = info .getDetailedStateOf(info.getSupplicantState()); boolean isConnected = // need to ensure we're on correct network because sometimes this code is // reached before the initial network has disconnected info.getNetworkId() == networkIdToConnect && ( connectionState == NetworkInfo.DetailedState.CONNECTED || // Android seems to sometimes get stuck in OBTAINING_IPADDR after it has received one (connectionState == NetworkInfo.DetailedState.OBTAINING_IPADDR && info.getIpAddress() != 0) ); if (isConnected) { return new String[]{ null, "NETWORK_CONNECTION_COMPLETED" }; } Log.d(TAG, "WifiWizard: Got " + connectionState.name() + " on " + (i + 1) + " out of " + TIMES_TO_RETRY); final int ONE_SECOND = 1000; try { Thread.sleep(ONE_SECOND); } catch (InterruptedException e) { Log.e(TAG, e.getMessage()); return new String[]{ "INTERRUPT_EXCEPT_WHILE_CONNECTING", null }; } } Log.d(TAG, "WifiWizard: Network failed to finish connecting within the timeout"); return new String[]{ "CONNECT_FAILED_TIMEOUT", null }; }
Example 16
Source File: WifiManagerSnippet.java From mobly-bundled-snippets with Apache License 2.0 | 4 votes |
/** * Connect to a Wi-Fi network. * * @param wifiNetworkConfig A JSON object that contains the info required to connect to a Wi-Fi * network. It follows the fields of WifiConfiguration type, e.g. {"SSID": "myWifi", * "password": "12345678"}. * @throws InterruptedException * @throws JSONException * @throws WifiManagerSnippetException */ @Rpc(description = "Connects to a Wi-Fi network.") public void wifiConnect(JSONObject wifiNetworkConfig) throws InterruptedException, JSONException, WifiManagerSnippetException { Log.d("Got network config: " + wifiNetworkConfig); WifiConfiguration wifiConfig = JsonDeserializer.jsonToWifiConfig(wifiNetworkConfig); String SSID = wifiConfig.SSID; // Return directly if network is already connected. WifiInfo connectionInfo = mWifiManager.getConnectionInfo(); if (connectionInfo.getNetworkId() != -1 && connectionInfo.getSSID().equals(wifiConfig.SSID)) { Log.d("Network " + connectionInfo.getSSID() + " is already connected."); return; } int networkId; // If this is a network with a known SSID, connect with the existing config. // We have to do this because in N+, network configs can only be modified by the UID that // created the network. So any attempt to modify a network config that does not belong to us // would result in error. WifiConfiguration existingConfig = getExistingConfiguredNetwork(wifiConfig.SSID); if (existingConfig != null) { Log.w( "Connecting to network \"" + existingConfig.SSID + "\" with its existing configuration: " + existingConfig.toString()); wifiConfig = existingConfig; networkId = wifiConfig.networkId; } else { // If this is a network with a new SSID, add the network. networkId = mWifiManager.addNetwork(wifiConfig); } mWifiManager.disconnect(); if (!mWifiManager.enableNetwork(networkId, true)) { throw new WifiManagerSnippetException( "Failed to enable Wi-Fi network of ID: " + networkId); } if (!mWifiManager.reconnect()) { throw new WifiManagerSnippetException( "Failed to reconnect to Wi-Fi network of ID: " + networkId); } if (!Utils.waitUntil( () -> mWifiManager.getConnectionInfo().getSSID().equals(SSID) && mWifiManager.getConnectionInfo().getNetworkId() != -1, 90)) { throw new WifiManagerSnippetException( String.format( "Failed to connect to '%s', timeout! Current connection: '%s'", wifiNetworkConfig, mWifiManager.getConnectionInfo().getSSID())); } Log.d( "Connected to network '" + mWifiManager.getConnectionInfo().getSSID() + "' with ID " + mWifiManager.getConnectionInfo().getNetworkId()); }
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: WifiUtils.java From WifiChat with GNU General Public License v2.0 | 4 votes |
public static int getNetworkId() { WifiInfo mWifiInfo = mWifiManager.getConnectionInfo(); if (mWifiInfo == null) return 0; return mWifiInfo.getNetworkId(); }
Example 19
Source File: WifiUtil.java From Rumble with GNU General Public License v3.0 | 4 votes |
public static boolean isConnected() { WifiInfo info = getWifiManager().getConnectionInfo(); return (info.getNetworkId() >= 0); }
Example 20
Source File: WifiUtils.java From DevUtils with Apache License 2.0 | 2 votes |
/** * 获取连接的 Network Id * @param wifiInfo {@link WifiInfo} * @return Network Id */ public static int getNetworkId(final WifiInfo wifiInfo) { if (wifiInfo == null) return -1; return wifiInfo.getNetworkId(); }