WiFi直接返回P2P_UNSUPPORTED

时间:2016-04-03 08:38:18

标签: android p2p wifi-direct wifip2p

我有一个真正的Android设备(Android 4.1.1)。它支持使用WiFi Direct传输文件的Xender,ShareIt,Zapya等应用程序。

但是当我运行我的应用程序时,它会说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

注意:变量 state 既不等于WifiP2pManager.WIFI_P2P_STATE_ENABLED也不等于WifiP2pManager.WIFI_P2P_STATE_DISABLED

1 个答案:

答案 0 :(得分:2)

您列出的应用程序可能使用Wi-Fi Direct以外的替代传输。 P2P_UNSUPPORTED错误表示您的设备没有Wi-Fi Direct功能。您可以尝试通过执行以下操作来确认:getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)。或者,如果您的设备具有Wi-Fi Direct,则可以通过设备的Wi-Fi设置访问Wi-Fi Direct设置活动。