4

I am trying to get two android devices to communicate with each others using WiFi Direct. I list the available devices in a ListView and when the user taps on a device I initiate a connection by calling the connect method from the WifiP2pManager class. In the onSucces method, I call the requestConnectionInfo method in order to get the IP address of the Group Owner so I can connect to a ServerSocket.

The problem is that the first time I try to connect the WifiP2pInfo object passed on to onConnectionInfoAvailable callback has null groupOwnerAddress property. This doesn't make sense to me because I call the reuestConnectionInfo inside the onSuccess callback of the connect method, meaning the connection is already established.

If I try to connect again by tapping on the device's name again) a few seconds later, the WifiP2pInfo object now contains the address of the GO and I am able to initiate a TCP connection.

I have tried making the Thread sleep for a few seconds before calling the requestConnectionInfo but I still have the same problem.

@Override
public void onDialogPositiveClick(DialogFragment dialog, final String enteredPin) {

    WifiP2pConfig config = new WifiP2pConfig();
    config.deviceAddress = mDevice.deviceAddress;
    config.groupOwnerIntent = 0;
    mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {

            mManager.requestConnectionInfo(mChannel, new WifiP2pManager.ConnectionInfoListener() {
                @Override
                public void onConnectionInfoAvailable(WifiP2pInfo info) {
                    InetAddress ownerAddress=info.groupOwnerAddress;

                    if (ownerAddress!=null) {
                        Log.d("MainActivity ",ownerAddress.toString());
                        ConnectAsyncTask asyncTask=new ConnectAsyncTask(MainActivity.this,ownerAddress,8888,enteredPin);
                        asyncTask.execute();
                    } else {
                        Toast.makeText(MainActivity.this, "Connection failed! Try again!", Toast.LENGTH_LONG).show();
                    }
                }
            });

        @Override
        public void onFailure(int reason) {

        }
    });
}

1 Answer 1

8

ConnectionInfo available when WIFI_P2P_CONNECTION_CHANGED_ACTION broadcast catched. This broadcast fires when WifiP2pDevice connected or disconnected. This is your problem. If a device disconnected, this broadcast fires, but that device no longer in group. You have to check the change action to decide the device connected or disconnected. :

public void onConnectionInfoAvailable(WifiP2pInfo info) {
    NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
    if (networkInfo.isConnected()) { 
        //connected
    } else {
        //disconnected
    }
}
1
  • Yes, you are absolutely right, this solved my problem. And it's right there in the Android WiFi P2P Documentation, I should have done more reading. Thank you very much. PS I would have upped your answer but I still don't have the reputation to do so, I will when am able to..
    – idzikovski
    Nov 19, 2014 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.