#StackBounty: #java #android #wifi #p2p Android Wifi Direct detects peers but Peer List is emty

Bounty: 50

Everything works fine but when WifiP2pManager calls onPeersAvailable method (it calls it correctly) the Peers Device List is empty, which makes no sense because if the method is called, it’s because a peer was discovered. :S

Is something wrong in my code? Thanks.

Main Activity, Broadcast Receiver, and Discovery:

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;

protected void onCreate(Bundle savedInstanceState) {
    ctx = getApplicationContext();
    flManager = new FileManager(ctx);

    //Wifi Direct
    mIntentFilter = new IntentFilter();

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

    mPeerListListener = new WifiP2pManager.PeerListListener(){
        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);

protected void onResume(){
    registerReceiver(mReceiver, mIntentFilter);

protected void onDestroy(){

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){
    this.mManager = manager;
    this.mChannel = channel;
    this.mActivity = activity;
    this.mPeerListListener = peerListListener;

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
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() {
            public void onSuccess() {
                Log.i(TAG, "WiFi P2P Discovery successful");

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

enter image description here

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.