13

I'm using the new Wi-Fi Direct API from google on Android 4.0 and in Sample code they send the User to Settings, to activate WiFi -Direct Mode.

Is there a way to Start it by code???

all they offer is to listen to WIFI_P2P_STATE_CHANGED_ACTION intent, and then use this code

String action = intent.getAction();

if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {

   // UI update to indicate wifi p2p status.
   int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);

   if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
       // Wifi Direct mode is enabled

   } else {
       // Wifi Direct mode is disabled
   }
zaxy78
  • 1,406
  • 3
  • 19
  • 32
  • 1
    Don't know if it works on 4.0, but works on previous : http://www.tutorialforandroid.com/2009/10/turn-off-turn-on-wifi-in-android-using.html – Carlos Mar 26 '12 at 11:02

3 Answers3

13

Yes there is a way using reflection. Works on my GSII (and fails gracefully on non Wifi Direct HTC Sensation) but as this is reflection it may not work on all phones.

p2pManager = (WifiP2pManager) getSystemService(WIFI_P2P_SERVICE);
channel = p2pManager.initialize(getApplicationContext(),
        getMainLooper(), null);

try {
    Class<?> wifiManager = Class
            .forName("android.net.wifi.p2p.WifiP2pManager");

    Method method = wifiManager
            .getMethod(
                "enableP2p",
                new Class[] { android.net.wifi.p2p.WifiP2pManager.Channel.class });

    method.invoke(p2pManager, channel);

} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Please note:

On Jelly Bean and above, when you try to use the WifiP2pManager API, WiFi-Direct is automatically enabled (as long as WiFi is on), so there is no need to use this hack.

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
Ciaran Fisher
  • 1,006
  • 9
  • 16
  • I'd like to add that WiFi direct on JB and above (at least on AOSP) is not active all the time - it only appears to be. If you look at listeners for WiFi direct, it turns itself off after some time. It turns itself back on if you open the wifi direct menu, however. You might have to have the host do a peer search or initialize itself in order to be able to be found. Likely a battery saving trick. I have also found that it's blocking, since as it accepts a connection, the entire system will lock up and fail to connect sometimes. (The system invitation) – Mgamerz Jan 21 '13 at 05:14
  • And add one more thing: This code only works if Wifi is on to start with it seems. My phone (LG Mach 4.0.4) throws method not found if it is off, but it works beautifully if it is already on. – Mgamerz Jan 21 '13 at 22:52
  • @Mgamerz Wierd, on the GSII it works when Wifi is On or Off (if its off it automatically turns it on but it takes a few seconds to actually work although the method returns straight away) – Ciaran Fisher Jan 22 '13 at 09:28
  • It seems OEM's choose how to do their own implementation to some degree. And it's broken on pretty much all of them... including vanilla. – Mgamerz Jan 23 '13 at 01:11
  • Correction: It now works. I thought it was broken because it was rebooting some devices but it appears ICS has an invitation bug where it will crash upon invitation. – Mgamerz Jan 27 '13 at 23:48
  • 2
    Could someone show me how to use reflection to turn on wifi direct? – windchime Jul 19 '13 at 18:24
2

No, all you could do is notify the user to turn on WiFi.

CoffeeRain
  • 4,460
  • 4
  • 31
  • 50
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
  • Is there a way to check if it's on without waiting for the intent? -for example if user turned in on before running my app.... – zaxy78 Dec 20 '11 at 10:07
0

On some devices, even though Wi-Fi direct is supported, it's not enabled due to some system bugs. Here's a more reliable way to check whether it's enabled (unfortunately it requires root) using Kotlin.

val matcher = "^mNetworkInfo .* (isA|a)vailable: (true|false)"
    .toPattern(Pattern.MULTILINE)
    .matcher(su("dumpsys ${Context.WIFI_P2P_SERVICE}"))
if (!matcher.find()) return "Root unavailable"
if (matcher.group(2) != "true") return "Wi-Fi Direct unavailable"
return "Wi-Fi Direct available"

This should work for Android 4.3 - 8.1. Check source code below:

Mygod
  • 2,077
  • 1
  • 19
  • 43