Fatih Barmanbay
Fatih Barmanbay

Reputation: 61

Android Wifi Direct onPeersAvailable doesn't shows anything

I'm trying to develop a simple app using Wifi Direct. The problem is I can't get a list of available peers using onPeersAvailable method. I tried the solutions mentioned here and here but no luck.There is nothing at logs, tried using Toast instead of log but nothing showed up on the screen either. Here is my Main and BroadCastReceiver classes.

Main Class:

public class MainActivity extends AppCompatActivity {

private final String TAG = this.getClass().toString();

WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;
BroadcastReceiver mReceiver;
IntentFilter mIntentFilter;

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

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

    Button btn_discover = (Button) findViewById(R.id.btn_discover);
    btn_discover.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
                @Override
                public void onSuccess() {
                    /*Toast.makeText(getApplicationContext(), "Discovery is a success.",
                            Toast.LENGTH_SHORT).show();*/
                    //startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                }

                @Override
                public void onFailure(int reasonCode) {
                    Toast.makeText(getApplicationContext(), "Discovery is a failure "+reasonCode,
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
    });
}

/* register the broadcast receiver with the intent values to be matched */
@Override
protected void onResume() {
    super.onResume();
    registerReceiver(mReceiver, mIntentFilter);
}
/* unregister the broadcast receiver */
@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mReceiver);
}}

BroadCastReceiver class:

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private final String LOG_TAG = this.toString();
private WifiP2pManager mManager;
private WifiP2pManager.Channel mChannel;
private MainActivity mActivity;

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) {
            Log.i(LOG_TAG, "Wifi Direct is enabled");
        } else {
            Log.i(LOG_TAG, "Wifi Direct is not enabled");
        }
    } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
        // 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 (mManager != null) {
            mManager.requestPeers(mChannel, new WifiP2pManager.PeerListListener() {
                @Override
                public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
                    Log.i(LOG_TAG, "Found some peers!!! "+wifiP2pDeviceList.getDeviceList().size());
                }
            });
        }
    } 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 am sure my device (2012 Nexus 7 running Android 4.4.4) supports Wifi Direct.

Upvotes: 1

Views: 782

Answers (1)

JAD
JAD

Reputation: 1160

I think the issue is because you are creating new "PeerListListener" inside the "WiFiDirectBroadcastReceiver".

Try to add it to your main activity instead:

public class MainActivity extends AppCompatActivity implements WifiP2pManager.PeerListListener

And then add new method to you main activity to listen to available peers:

@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
   Log.i(LOG_TAG, "Found some peers!!! " + peerList.getDeviceList().size());
}

Note: Don't forget to create new listener variable instead "WiFiDirectBroadcastReceiver" and pass the main activity as a reference to it.

Hope this helps.

Upvotes: -1

Related Questions