我在Android4.0上使用来自谷歌的新Wi-Fi Direct API,在示例代码中,他们将用户发送到设置,以激活WiFi -Direct模式。
有没有办法通过代码启动它?
它们所提供的全部功能就是监听WIFI_P2P_STATE_CHANGED_ACTION意图,然后使用以下代码
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
}发布于 2012-10-31 18:20:05
是的,有一种使用反射的方法。在我的GSII上工作(在非Wifi Direct HTC Sensation上失败),但由于这是反映,它可能不会在所有的手机上工作。
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();
}请注意:
在Jelly Bean及更高版本中,当您尝试使用WiFi时,WiFi-Direct会自动启用(只要WiFi开启),因此不需要使用此WifiP2pManager。
https://stackoverflow.com/questions/8571566
复制相似问题