Can I turn on WiFi-Direct from code? on Android API-14 (ICS)
Asked Answered
S

3

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
   }
Skell answered 20/12, 2011 at 6:28 Comment(1)
Don't know if it works on 4.0, but works on previous : tutorialforandroid.com/2009/10/…Vernievernier
J
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.

Jerk answered 31/10, 2012 at 10:20 Comment(6)
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)Bly
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.Bly
@Bly 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)Jerk
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.Bly
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.Bly
Could someone show me how to use reflection to turn on wifi direct?Kakapo
R
2

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

Rufford answered 20/12, 2011 at 6:30 Comment(1)
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....Skell
R
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:

Repugnance answered 4/1, 2018 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.