2016-04-03 81 views
0

我有一個真正的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

+0

嘗試運行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

回答

1

您列出的應用程序可能使用除Wi-Fi Direct以外的其他傳輸。 P2P_UNSUPPORTED錯誤意味着您的設備沒有Wi-Fi Direct功能。您可以嘗試通過執行以下操作來確認:getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)。或者,如果您的設備具有Wi-Fi Direct,則可以從設備的Wi-Fi設置訪問Wi-Fi Direct設置活動。