Domanda

I found lists of system settings that I can change and of those that I can't (secure settings), but enabling/disabling Wifi-Direct (ICS) is in neither of them. How can I automatically turn Wi-Fi Direct on when I start my app?

È stato utile?

Soluzione

JB introduces the notion of concurrency which means an application does not have to turn on wifi direct as long as wifi is on.

For ICS, it is best to take the user to wireless settings to allow him to turn it on.

Altri suggerimenti

Unfortunately there is no public way to accomplish this. The Settings app in both ICS and JB use hidden methods on the manager classes to toggle the service as enabled/disabled.

  • In ICS, WifiP2PManager.enableP2P() and WifiP2PManager.disableP2P() were used, both hidden methods.
  • In JB, NsdManager.setEnabled() is used.

So it is possible, because the methods do exist on those classes. However, because of this variance, you would have to create very version specific reflection code that will almost certainly break in later versions in order to provide the functionality directly from your application.

A better solution would simply be to check if the service is enabled, and show a dialog to the user that takes them directly to the Wireless Settings page to enable WiFi Direct themselves if they so choose.

have you tried to use the WiFi manager? You should be able to toggle it like this inside an Activity:

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (!wifi.isWifiEnabled()){
  wifi.setWifiEnabled(true);
}

this should turn on WIFI, but doesn't guarantee that one has a Wifi Connection! You can also use the WifiManager to look for WifiInfo with

WifiInfo wifiInfo = wifi.getConnectionInfo();

or ping the Access-Point with

if (wifi.pingSupplicant()){
  //at least connected to a network
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top