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

天大地大妈咪最大 提交于 2019-12-17 18:36:30

问题


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
   }

回答1:


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.




回答2:


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




回答3:


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:

  • https://android.googlesource.com/platform/frameworks/base/+/f0afe4144d09aa9b980cffd444911ab118fa9cbe%5E%21/wifi/java/android/net/wifi/p2p/WifiP2pService.java
  • https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/a8d5e40/service/java/com/android/server/wifi/p2p/WifiP2pServiceImpl.java#639
  • https://android.googlesource.com/platform/frameworks/base.git/+/f0afe4144d09aa9b980cffd444911ab118fa9cbe/core/java/android/net/NetworkInfo.java#433
  • https://android.googlesource.com/platform/frameworks/base.git/+/220871a/core/java/android/net/NetworkInfo.java#415


来源:https://stackoverflow.com/questions/8571566/can-i-turn-on-wifi-direct-from-code-on-android-api-14-ics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!