Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8795599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:25:23+00:00 2026-06-13T23:25:23+00:00

I’m developping a simple application based on WiFi Direct for Android that has to

  • 0

I’m developping a simple application based on WiFi Direct for Android that has to connect two devices. To do so I need to call to the función onPeersAvailable(myPeerListListener), but I don’t know how.

My app has this two elements:

1-Main Activity:

package android.nacho.WifiDirect;



import android.net.wifi.p2p.WifiP2pManager.Channel;

import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class WifiDirect extends Activity {


    WifiP2pManager mManager;
    Channel mChannel;
    BroadcastReceiver mReceiver;

    IntentFilter mIntentFilter;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_direct);


        //To register the BroadastReceiver
        mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel =   mManager.initialize(this, getMainLooper(), null); //It was necessary to make a cast (Channel)
        mReceiver = new WiFiBroadcastReceiver(mManager, mChannel, this); //, this);

        //Layout
        final Button btnScan = (Button)findViewById(R.id.btnScan); 
        final TextView TextDebug=(TextView)findViewById(R.id.TextDebug);


        //To define the filter in the BroadcastReceiver
        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);




        final OnClickListener ScanListener=new OnClickListener() //Sacado de TEstPsycologico
        {
            public void onClick(View v){



                TextDebug.setText("Se intentan buscar Peers");

                mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {


                    public void onSuccess() {

                        TextDebug.setText("Ha habido éxito buscando Peers");//DEBUG: Para ver si es posible encontrar Peers

                    }


                    public void onFailure(int reasonCode) {

                        TextDebug.setText("Algo ha salido mal buscando Peers"); //DEBUG: Para ver si pasó algo raro busando Peers
                    }
                });


                onPeersAvailable(myPeerListListener);

            }


        };

        btnScan.setOnClickListener(ScanListener);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_wifi_direct, menu);
        return true;
    }

    //


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

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

}

2 Class BroadcastReceiver:

package android.nacho.WifiDirect;

import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pManager;
import android.widget.Toast;



/**
 * A BroadcastReceiver that notifies of important Wi-Fi p2p events.
 */

public class WiFiBroadcastReceiver extends BroadcastReceiver {

    private WifiP2pManager manager;
    private Channel channel;
    private WifiDirect activity;
    private PeerListListener myPeerListListener;

    //For toast, add also context
    //private Context context;

    public WiFiBroadcastReceiver(WifiP2pManager manager, Channel channel, WifiDirect activity){//, Context context) {

        super();
        this.manager = manager;
        this.channel = channel;
        this.activity = activity;
       // this.context= context;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();     


        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {

            // Check to see if Wi-Fi is enabled and notify appropriate activity
             int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
             if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {

                //Toast.makeText(context, "Wi-Fi Direct is enable", Toast.LENGTH_LONG).show();

             } else {

                //Toast.makeText(context, "Wi-Fi Direct is not enable", Toast.LENGTH_LONG).show();
             }      

        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            // Call WifiP2pManager.requestPeers() to get a list of current peers

             // request available peers from the wifi p2p manager. This is an
            // asynchronous call and the calling activity is notified with a
            // callback on PeerListListener.onPeersAvailable()
            if (manager != null) {
                 manager.requestPeers(channel, myPeerListListener);
                 manager.onPeersAvailable(myPeerListListener);

            }



        } 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
        }
    }
}

Until now my code should be able to detect peers around the device running the app, which ID should be stored in the variable myPeerListListener. All this take place in the second intend of the BroadcastReceiver method called OnReceiv(), by calling:

 manager.requestPeers(channel, myPeerListListener);

Now I want to manipulate that list. So following the API information it should be calling requestPeers, you can see the API on here:

http://developer.android.com/guide/topics/connectivity/wifip2p.html

*Section Discovering Peers

So what I have tried was to write below a call to:

manager.onPeersAvailable(myPeerListListener); 

but I get this error:

The method onPeersAvailable(WifiP2pManager.PeerListListener) is undefined for the type WifiP2pManager

Could anyone tell me how could I send the PeerListListener to the main activity properly?

Thank you very much for your time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:25:24+00:00Added an answer on June 13, 2026 at 11:25 pm

    You should implement the onPeersAvailable method inside the PeerListListener.

    Please see http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.