Question

I am developing a P2P app and was following the tutorial HERE and while I was testing it on 2 phones I was wandering when do I have to initiate Peer Discovery?

Only once when the app is started, periodically or every time in onResume?

I'm asking because some times when I try to show the list of peers it can't find any. I would also like to know the cause of this AND what actualy is peer discovery and what is it doing.

Was it helpful?

Solution

I start the discovery through listeners registered in the app every 'x' seconds and yes, you would want to initiate discovery onResume() as well. Also, I presume you are already leveraging broadcast intents to determine the state change.

I can't however, clarify on the internals of P2P discovery but the discovery could fail because the network is busy or due to an internal error or simply because the device doesn't support P2P as mentioned here.

OTHER TIPS

mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Toast.makeText(MainActivity.this, "onSuccess", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(int reasonCode) {

            switch(reasonCode){

            case WifiP2pManager.ERROR:

                Toast.makeText(MainActivity.this, "Getting error while peers discover", Toast.LENGTH_SHORT).show();
                break;

            case WifiP2pManager.P2P_UNSUPPORTED:
                Toast.makeText(MainActivity.this, "Device is not supported", Toast.LENGTH_SHORT).show();
                break;


            case WifiP2pManager.BUSY:
                Toast.makeText(MainActivity.this, "Device is busy", Toast.LENGTH_SHORT).show();
                break;

            }
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top