Unable to fetch devices from WifiP2pDeviceList

I am just trying to implement Wifi P2P and I'm stuck on getting the peer list. When I click on discover device Button then I just want to fetch all the devices append the list of devices to a string which I can output using a text view.

MainActivity.java

package com.example.lrmah.wifip2p;


public class MainActivity extends AppCompatActivity implements 
WifiP2pManager.PeerListListener {

private List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;
BroadcastReceiver mReceiver;
Button B;
TextView t;


IntentFilter mIntentFilter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    t=(TextView) findViewById(R.id.displayTextView);
    B=(Button) findViewById(R.id.discoverPeers);

    mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
    mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);

    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);

    B.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
                @Override
                public void onSuccess() {
                    Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(int reasonCode) {
                }
            });

        }
    });




}

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(mReceiver, mIntentFilter);
}


/* unregister the broadcast receiver */
@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mReceiver);
}

@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
    List<WifiP2pDevice> refreshedPeers = (List<WifiP2pDevice>) peerList.getDeviceList();
    if (!refreshedPeers.equals(peers)) {
        peers.clear();
        peers.addAll(refreshedPeers);
    }

    WifiP2pDevice device= refreshedPeers.get(0);
    t.setText(device.deviceName);
    if (peers.size() == 0) {
        return;
    }
}}

WifiDirectBroadcastReceiver

package com.example.lrmah.wifip2p;


public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {

private WifiP2pManager mManager;
private WifiP2pManager.Channel mChannel;
private MainActivity mActivity;
WifiP2pManager.PeerListListener myPeerListListener;

public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,
                                   MainActivity activity) {
    super();
    this.mManager = manager;
    this.mChannel = channel;
    this.mActivity = activity;
}

@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) {
            Toast.makeText(mActivity,"wifi is on",Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(mActivity,"Please turn on wifi",Toast.LENGTH_LONG).show();
        }
    } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
        if (mManager != null) {
            mManager.requestPeers(mChannel, mActivity);
        }
    }
    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
    }
}}

I may be wrong but I think the onPeerAvailable is not being called.

1 answer

  • answered 2018-01-08 03:16 Demonick

    Some points:

    1. else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { // here you should call

      NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
      
      if (networkInfo.isConnected() && isSender) {
          // connected with the other device, request connection info to find group owner IP
          manager.requestConnectionInfo(channel, yourWifiP2pManager.ConnectionInfoListene);
      }
      
    2. else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { // call discoverPeers here

      manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
          @Override
          public void onSuccess() {
              if (manager != null) {
                  manager.requestPeers(channel, yourWifiP2pManager.PeerListListener);
              }
          }
          @Override
          public void onFailure(int reasonCode) {}
      });
      
    3. then the device list will be broadcast in

      else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
          WifiP2pDeviceList list = intent.getParcelableExtra(WifiP2pManager.EXTRA_P2P_DEVICE_LIST);
          for (WifiP2pDevice d : list.getDeviceList()) { //...