SonOfEl
SonOfEl

Reputation: 177

Android WifiP2pManager discovery - Unable to narrow down issue

The flow of my code is - discover, connect, check connection info. Below is the code with logic written in the same order -

         manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                tvStatus.setText("Status - Discovering peers...");
            }

            @Override
            public void onFailure(int reasonCode) {
                //**ISSUE 1**
               
                   //Timer delay of 5-10 seconds. Then discovering again.
                   manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
                      @Override
                      public void onSuccess() { // }

                      @Override
                      public void onFailure(int reason) { // }
                        });
                    }
        });
        
        //NEXT POPULATING PEERS LIST
        peerListListener = new WifiP2pManager.PeerListListener() {
            @Override
            public void onPeersAvailable(WifiP2pDeviceList devList) {
                Toast.makeText(PeersIntent.this, "Inside onPeersAvailable", Toast.LENGTH_SHORT).show();
                //***DOESNT EVEN MAKE IT IN HERE***
                //Populating list view
            }
        };

       //CONNECTING TO PEER BASED ON LIST VIEW ITEM CLICK. Hiding this code block because discovery itself fails.

       //NEXT CONNECTION INFO LISTENER
       connectionInfoListener = new WifiP2pManager.ConnectionInfoListener() {
            @Override
            public void onConnectionInfoAvailable(WifiP2pInfo info) {
                final InetAddress groupOwnerAddress = info.groupOwnerAddress;
                if(info.groupFormed && info.isGroupOwner)
                {//}
                else if(info.groupFormed)
                {//}
                if(WiFiDirectBroadcastReceiver.disconnected)
                {
                   //**ISSUE 2**
                   //INDICATED IN CODE BLOCK BELOW
                }

            }
        };

My WiFiDirectBroadcastReceiver class is defined as specified in the official docs -

            if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            // Respond to new connection or disconnections
            if(manager != null)
            {
                NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
                if(networkInfo.isConnected())
                {
                    manager.requestConnectionInfo(channel, PeersIntent.connectionInfoListener);
                }
                else
                {
                    Toast.makeText(context, "Device Disconnected", Toast.LENGTH_LONG).show();
                    disconnected = true; //a public status variable
                    //**ISSUE 2**
                }
            }

My manifest file (I'm checking and requesting permissions during runtime as well)-

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

->Issue 1 (indicated in the code) - discovery fails with code 2. So I wait for 10 seconds to make sure it isn't busy and try again. Still same issue.

->Issue 2 (indicated in the code) - The broadcast class disconnects everytime I rerun the app after turning off and on the wifi.

->Issue 3 - Discovery doesn't seem to work even though discoverPeers() returns onSuccess(). The PeerListListener() never goes inside the overriden onPeersAvailable().

I am trying out the code on Pixel 2 (Pie) and Pixel 3a (R) emulators with location permission on. I have no idea what I'm doing wrong or where I'm going wrong. Either the logcat isn't of much help to me or I'm reading it wrong.

Where am I going wrong/ What can I do differently?

Upvotes: 0

Views: 411

Answers (1)

Cath
Cath

Reputation: 462

From the documentation for WifiP2pManager:

When an application needs to fetch the current list of peers, it can request the list of peers with requestPeers(WifiP2pManager.Channel, WifiP2pManager.PeerListListener). When the peer list is available PeerListListener#onPeersAvailable is called with the device list.

You should listen to WIFI_P2P_PEERS_CHANGED_ACTION within your broadcast receiver to request peers, requestConnectionInfo() is used to request information after a group has already been established. Make sure to check out the sample project.

Upvotes: 1

Related Questions