Dear Lebanese programmers,
I am working on my graduation project
It's about Network Coding: A way to optimize broadcast . I mean to implement it on mobile phones, and for that I need to connect the phones without the need for a router. So I decided to use Wifi-Direct or Android WiFiP2P API
I hope this isn't against this site's policy but , I already posted the question on StackOverflow. But it never even had one comment . I even added a bounty to no avail.
Link to question
I will post the question here, but if you have an account on SO with privilege to upvote posts, please gimme some upvotes so it would attract some people to it, I really need this fixed !
I am trying to connect multiple devices to a group owner that I select manually
I want the peers to connect to the group owner manually once they find him
I have 3 phones (no emulators), on each there is a "Create group" button with this click handler
public void createWifiGroup(View view) {
mManager.createGroup(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
mManager.requestGroupInfo(mChannel,new MyGroupInfoListener(MainActivity.this));
}
@Override
public void onFailure(int reason) {
}
});
}
As you can see I do `requestGroupInfo` and pass it a listener that is printing the following line in logs:
groupFormed: true isGroupOwner: true groupOwnerAddress: /192.168.49.1
So I suppose it succeeded
I also have a `MyPeerListener` class that the `BroadCastReceiver` calls when a `WIFI_P2P_PEERS_CHANGED_ACTION` action intent is received.
`MyPeerListener` will iterate through the peers and connect if it finds a group owner
@Override
public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
this.wifiP2pDeviceList = wifiP2pDeviceList;
//Toast toast = Toast.makeText(receiver.mActivity, "I found some peers", Toast.LENGTH_LONG);
// toast.show();
Iterator<WifiP2pDevice> deviceListIterator = wifiP2pDeviceList.getDeviceList().iterator();
boolean foundGroup = false;
while (deviceListIterator.hasNext()) {
final WifiP2pDevice device = deviceListIterator.next();
if (!foundGroup && device.isGroupOwner() && !MainActivity.connected) {
//MainActivity.mManager.removeGroup(MainActivity.mChannel, null);
foundGroup = true;
final WifiP2pConfig config = new WifiP2pConfig();
//config.wps.setup = WpsInfo.PBC;
config.groupOwnerIntent = 0;
config.deviceAddress = device.deviceAddress;
MainActivity.mManager.connect(MainActivity.mChannel, config, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
//success logic
Toast toast = Toast.makeText(receiver.mActivity, "connect success", Toast.LENGTH_SHORT);
toast.show();
MainActivity.connected = true;
MainActivity.mManager.requestGroupInfo(MainActivity.mChannel, new MyGroupInfoListener(receiver.mActivity));
MainActivity.mManager.requestConnectionInfo(MainActivity.mChannel, new MyConnectionInfoListener(receiver));
}
@Override
public void onFailure(int reason) {
//failure logic
//MainActivity.connected = false;
Toast toast = Toast.makeText(receiver.mActivity, "connect fail, reason: " + reason, Toast.LENGTH_LONG);
toast.show();
}
});
}
ListView list = (ListView) receiver.mActivity.findViewById(R.id.device_list_view);
list.setAdapter(new DeviceListViewAdapter(this, receiver.mActivity));
}
}
}
(I am aware of the use of static variables and overall bad design , but this is a fast prototype to learn)
Now I get a `connect success` `Toast` but the `requestConnectionInfo` right after the connection shows me this line
groupFormed: false isGroupOwner: false groupOwnerAddress: null
A call to requestGroupInfo also shows:
group is null
This happens very often about 80% of the times. Sometimes it shows the default group IP `192.168.49.1` . But I'll be lucky if both the "slave" phones have correct group info.
When I try to connect a socket to the owner while the group info is null it fails with `destination host unreachable`. But when the group info is correct I can connect and send data properly.
So why is this happening ? How is it even possible that the connection succeeds but the group is null ?