Last active
November 10, 2021 08:10
-
-
Save ahmed-BH/566bd22169c097bd6e2851e10431c6f4 to your computer and use it in GitHub Desktop.
Get list of wifi networks in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ------------------------------------------------------------------------------------------------------------------------------ | |
* SIGNAL MEANING | |
* -30 dBm Maximum signal strength, you are probably standing right next to the access point. | |
* -50 dBm Anything down to this level can be considered excellent signal strength. | |
* -60 dBm Good, reliable signal strength. | |
* -67 dBm Reliable signal strength. The minimum for any service depending on a reliable connection and signal strength, such as voice over Wi-Fi and non-HD video streaming. | |
* -70 dBm Not a strong signal. Light browsing and email. | |
* -80 dBm Unreliable signal strength, will not suffice for most services. Connecting to the network. | |
* -90 dBm The chances of even connecting are very low at this level. | |
*/ ----------------------------------------------------------------------------------------------------------------------------- | |
/* | |
* Needed permission | |
* <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | |
* <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | |
* | |
*/ | |
private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context c, Intent intent) { | |
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { | |
List<ScanResult> mScanResults = mWifiManager.getScanResults(); | |
// add your logic here | |
} | |
} | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); | |
registerReceiver(mWifiScanReceiver, | |
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); | |
mWifiManager.startScan(); | |
// Turn Wifi ON/OFF | |
// <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> | |
wifiManager.setWifiEnabled(true); | |
wifiManager.setWifiEnabled(false); | |
} | |
// Asking permission at Runtime | |
@Override | |
public void onResume() | |
{ | |
super.onResume(); | |
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) | |
{ | |
if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) | |
{ | |
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 87); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment