idzikovski
idzikovski

Reputation: 41

Android Wi-Fi Direct: WifiP2pInfo object passed on to onConnectionInfoAvailable callback has null groupOwnerAddress property

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

        }
    });
}

Upvotes: 4

Views: 2648

Answers (1)

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

Upvotes: 8

Related Questions