Question

I'm writing an android WIFI code to scan the nearby hot spots but I need a code to force open the wifi if it is not already opened and then force close it after the scan is done. Does anyone have an idea how to do so.

Was it helpful?

Solution

This is the way you do what you need:

WifiManager wifiManager =(WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);

You will need some extra permissions.

uses-permission android:name="android.permission.CHANGE_WIFI_STATE"

Take on count that this might not be a good android practice, usually you would like the user to explicitly enable/disable it, so instead of programatically forcing wifi to do so, you should pop the settings activity so the user does it him self.

Regards!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top