Android Wi-Fi Direct: WifiP2pInfo object passed onConnectionInfoAvailable callback has null property groupOwnerAddress
I am trying to get two Android devices to communicate with each other using WiFi Direct. I am listing the available devices in ListView
, and when the user clicks on the device, I initiate the connection by calling the connect method from the WifiP2pManager class. In the method, onSucces
I call the method requestConnectionInfo
to get the IP address of the group owner so that I can connect to ServerSocket
.
The problem is that the first time I try to hook up the object WifiP2pInfo
passed to onConnectionInfoAvailable
the callback has a null property groupOwnerAddress
. It doesn't make sense to me because I am reuestConnectionInfo
calling in the callback of onSuccess
the connect method, meaning the connection is already established.
If I try to connect again by clicking on the device name again) after a few seconds, the object WifiP2pInfo
now contains the GO address and I can initiate a TCP connection.
I tried to make a Thread Thread for a few seconds before calling 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) {
}
});
}
source to share
ConnectionInfo available on WIFI_P2P_CONNECTION_CHANGED_ACTION transmission. This broadcast is triggered when WifiP2pDevice is connected or disconnected. It's your problem. If a device is disabled, this broadcast is triggered, but this device is no longer in the group. You must check the change action to decide if the device is connected or disconnected .:
public void onConnectionInfoAvailable(WifiP2pInfo info) {
NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
//connected
} else {
//disconnected
}
}
source to share