Reputation: 53
I'm working on an app, that should detect wifi networks around the device. I want to detect if the "Scanning always available" is switch on or not, but I couldn't find how. I know that it's possible because Google Now, actually does:
Upvotes: 3
Views: 2028
Reputation: 43342
You can use the isScanAlwaysAvailable() method in WifiManager.
I just tested this on Android 4.4, and it works.
To query the state, and show the prompt if it's disabled, use this code (I put it in onCreate()
):
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (Build.VERSION.SDK_INT >= 18 ) {
if (wifiManager.isScanAlwaysAvailable()) {
Toast.makeText(this, "Scan always available is on", Toast.LENGTH_SHORT).show();
}
else{
startActivityForResult(new Intent(WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE), 100);
}
}
else{
//Not supported
}
Then, use this code to capture the decision that the user made in the prompt in the case that it was disabled:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
if (requestCode == 100) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "User enabled Scan always available", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "User did not enable Scan always available", Toast.LENGTH_SHORT).show();
}
}
}
Note that you will also need this permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Upvotes: 8