我有一個真正的Android設備(Android 4.1.1)。它支持Xender,ShareIt,Zapya等使用WiFi Direct傳輸文件的應用程序。WiFi直接返回P2P_UNSUPPORTED
但是當我運行我的應用程序時,它說P2P_UNSUPPORTED。
我的清單權限....
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我MainActivity.java是這樣的..
intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
Receiver receiver=new Receiver();
registerReceiver(receiver,intentFilter);
WifiP2pManager manager=(WifiP2pManager)getSystemService(WIFI_P2P_SERVICE);
channel=manager.initialize(this, Looper.getMainLooper(),null);
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
// Code for when the discovery initiation is successful goes here.
// No services have actually been discovered yet, so this method
// can often be left blank. Code for peer discovery goes in the
// onReceive method, detailed below.
}
@Override
public void onFailure(int reasonCode) {
// Code for when the discovery initiation fails goes here.
// Alert the user that something went wrong.
switch (reasonCode){
case WifiP2pManager.BUSY:Log.e("code","BUSY");break;
case WifiP2pManager.ERROR:Log.e("code","ERROR");break;
case WifiP2pManager.P2P_UNSUPPORTED:Log.e("code","P2P_UNSUPPORTED");break;
}
}
});
在日誌,,我得到。
code﹕ P2P_UNSUPPORTED
而我BraodcastReceiver是這樣的..
@Override
public void onReceive(Context context, Intent intent) {
//Log.e("intent",intent.getAction());
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
Log.e("raw state", ""+state);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
Log.e("state", "ON");
} else if(state == WifiP2pManager.WIFI_P2P_STATE_DISABLED) {
Log.e("state", "DISABLED "+ state);
}
}
在那裏我得到的日誌作爲,,
raw state﹕ -1
注:變量狀態既不等於WifiP2pManage r.WIFI_P2P_STATE_ENABLED或WifiP2pManager.WIFI_P2P_STATE_DISABLED
嘗試運行Android的Wi-Fi直接演示,看看P2P的工作原理:https://github.com/android/platform_development/tree/master/samples/WiFiDirectDemo 這裏有一些其他Android的Wi-Fi Direct的資源以及: https://groups.google.com/forum/#!topic/wi-fi-direct/uWpuOzHY6y0 – Brendan