Android Wifi Direct会检测同伴,但是Peer List是emty

时间:2015-06-04 20:34:56

标签: java android wifi p2p

一切正常但是当onPeersAvailable调用public class Main extends Activity{ FileManager flManager; Context ctx; LinearLayout lay_found_users; LinearLayout lay_conversations; AddLayoutItem addLayoutItem = new AddLayoutItem(); private final String TAG = "Main"; //Wifi Direct WifiP2pManager mManager; WifiP2pManager.Channel mChannel; BroadcastReceiver mReceiver; IntentFilter mIntentFilter; WifiP2pManager.PeerListListener mPeerListListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.act_main); ctx = getApplicationContext(); flManager = new FileManager(ctx); //Wifi Direct mIntentFilter = new IntentFilter(); mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); mChannel = mManager.initialize(this, getMainLooper(), null); mPeerListListener = new WifiP2pManager.PeerListListener(){ @Override public void onPeersAvailable(WifiP2pDeviceList peerList){ Log.i(TAG, "Peers available"); int size = peerList.getDeviceList().size(); Log.i(TAG, String.valueOf(size)); } }; mReceiver = new WDBroadcastReceiver(mManager, mChannel, this, mPeerListListener); WDDiscovery wdDiscovery = new WDDiscovery(mManager, mChannel); wdDiscovery.discover(); } @Override protected void onResume(){ super.onResume(); registerReceiver(mReceiver, mIntentFilter); } @Override protected void onDestroy(){ super.onDestroy(); unregisterReceiver(mReceiver); } public class WDBroadcastReceiver extends BroadcastReceiver { private WifiP2pManager mManager; private WifiP2pManager.Channel mChannel; private Main mActivity; private final String TAG = this.getClass().getSimpleName(); private WifiP2pManager.PeerListListener mPeerListListener; public WDBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel, Main activity, WifiP2pManager.PeerListListener peerListListener){ super(); this.mManager = manager; this.mChannel = channel; this.mActivity = activity; this.mPeerListListener = peerListListener; } @Override public void onReceive(Context context, Intent intent){ String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)){ int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED){ Log.i(TAG, "Wifi P2P Enabled"); } else { Log.i(TAG, "Wifi P2P Disabled"); } }else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){ if (mManager != null){ mManager.requestPeers(mChannel, mPeerListListener); Log.i(TAG, "Peer Discovered"); } }else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){ // Respond to new connection or disconnections }else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){ // Respond to this device's wifi state changing } } } 方法(它正确调用它)时,对等设备列表为空,这没有意义,因为如果调用该方法,那是因为发现了对等。 :S

我的代码有问题吗?感谢。

主要活动,广播接收器和发现:

public class WDDiscovery {

    WifiP2pManager mManager;
    WifiP2pManager.Channel mChannel;
    private final String TAG = this.getClass().getSimpleName();

    public WDDiscovery(WifiP2pManager manager, WifiP2pManager.Channel channel) {
        mManager = manager;
        mChannel = channel;
    }

    public void discover() {

        mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.i(TAG, "WiFi P2P Discovery successful");
            }

            @Override
            public void onFailure(int reasonCode) {
                Log.i(TAG, "WiFi P2P Discovery error");
            }
        });
    }
}
{{1}}

enter image description here

3 个答案:

答案 0 :(得分:0)

尝试一下

public void onPeersAvailable(WifiP2pDeviceList peerList){
  List<WifiP2pDevice> refreshedPeers = peerList.getDeviceList();
  Log.i(TAG, String.valueOf(refreshedPeer.size()));

}

答案 1 :(得分:0)

在我的情况下,我添加

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

清单中的

并授予应用权限。 可以。

答案 2 :(得分:0)

?‍?在Android设备(非虚拟设备)上经过测试的解决方案

  • ?‍♂️首先授予C:\Program Files\JetBrains\PyCharm Community Edition 2019.2.3\helpers\pycharm\docrunner.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses import imp 权限
  • ?然后,在设备中打开位置
  • ?同行将很快出现