Search code examples
androidandroid-4.0-ice-cream-sandwichwifi-direct

How to add a device to a WifiP2pGroup in Android


I'm seriously confused...

The android documentation is VERY good, but is very limited in scope.

Say I have a device that calls createGroup(Channel,GroupInfoListener). When available, onGroupInfoAvailable(WifiP2pGroup) gets called. At this point, the only device in the group is the owner. (Because the device that calls create group is the group owner)

From here, the group is set up, and all that needs to happen is to fill the group up with the client devices.

I'm not sure how it works from here, but I was thinking that either the client can join the group, with joinGroup or the client can request to join, in which case the GroupOwner uses joinGroup to join. The problem is, there is no joinGroup Method. So how to hell does a peer add itself to the group, or how does a GroupOwner add a client to the group?


Solution

  • You'd use one device to createGroup and then use the other devices to discoverPeers and requestPeers.

    When onPeersAvaiable, You'd then iterate through the WifiP2pDevice's and call the isGroupOwner method on each device.

    For each group owner address obtained, the client would then connect to the group owner.

    Once a connection has been established, you would call requestConnectionInfo.

    When onConnectionInfoAvailable you would check the WifiP2pInfo object to ensure the groupFormed boolean was true.

    Then, using the groupOwnerAddress InetAddress, You can use Java Sockets to send signals between the two.

    You could then send a string to the group owner saying 'BLAH BLAH BLAH UNIQUE VALUE'. If the group owner returns 'I UNDERSTAND' then you know the group owner belongs to your application. I the group owner returns anything else, or does not reply, then you know the group owner is invalid.

    If you are using API 16+, you can use Service discovery to check whether a device is running your app, and THEN connect.