The problem: I\'m making an offline-multiplayer android game, where people can create or join a room and play together via Wi-fi. Consider the situation when a user creates
Basically, your design as I see it has very simple solution for the design with Wifi Direct.
Basically when creating "a room" that other can join, simply create a group, then do add local service to advertise the room.
note that you might need to start peer discovery, in order to be visible with your local service, at least my experience has been that, you do need to be active somehow, or you are not discoverable.
Then with clients, simply do service discovery and once you find the "room" simply connect to the it.
Then as the connections are initiated by the clients, the acceptance dialogs should be shown in Group owners side.
First of all you need to remember that if you do decide to use WiFiP2P
in your project you will need to prepare yourself for some difficult time. Unfortunately (as you already might have noticed :)) WiFiP2P
Android's official documentation is not that great. Different devices behave in different ways (e.g. they call some WifiP2P
events in different order) and docs don't cover it at all. Achieving a stable connection requires introducing unconventional and sometimes drastic measures. Obtaining a WifiP2P
connection is inseperably linked with displaying a system window (asking if you really want to connect to a device).
There are few things you need to consider before you start implementing a WifiP2P
solution:
If you answered "Yes" to question 1. or 2., you seriously need to consider if you really want to use WifiP2P
. WifiP2P
(Android's implementation of Wifi-Direct
) does not really fit your needs.
Using WifiP2P
would probably mean that a room owner creates a WifiP2P-group
and any person who wants to join the room must connect to this WifiP2p-group
.
The problem is Android does not allow for a single device to join multiple groups at the same time (which also means it does not allow to create multiple groups at the same time). Detecting changes in available WiFiP2P
devices around is also problematic (when you are already connected to a device).
If you really want to use Wifi-Direct
you would probably need to use one WiFiP2P-group
and connect all devices to it. This group would act just like a regular network, and all game-room-related stuff would have to be managed by one node acting as a typical server (or maybe not a typical server but some other network solution).
If you don't really need WifiP2P
use just a regular Wifi
. Connect all devices to one network (maybe even start a hotspot on one of your devices), and whole connection process can be relatively simple, performed in background without user being directly involved in it. Human-readable room names are not a problem, since you can send anything from your server.
If you answered "Yes" to question 3. and you really need to use WifiP2P
, you will need to find some way to broadcast your "human-readable" names to other devices.
You probably think it's super easy: you connect to other device, tell the name of the room you've created, and you're done. Not that simple. As I said before:
obtaining a
WifiP2P
connection is inseperably linked with displaying a system window (asking if you really want to connect to a device)
So the whole process would be more-less:
Device A: initiate connecting to Device B (this step can be performed by device B as well)
Device B: ugly system window appears asking you if you want to connect to Android-ao12ij219sa
(some PROGRAMMATICALLY UNCHANGEABLE system device name)
Device B: accepting
Device B: waiting for connection result
Device B: connected, waiting for information about Device A's room name
Device A: connected, sending room's name
Device B: Device A's room name obtained, disconnecting (future connection request will also result in showing system window)
While steps 1-7 are being performed no other device can connect to Device A (so during that time no other device can obtain room's name).
What could be done:
Use WifiP2P Service Discovery. It allows for broadcasting some information without the need to obtain a connection. Unfortunatelly it's even worse docummented than WifiP2P
itself, and it might be even less reliable (subjectively). If you want to make a reliable solution based on that you will need to spend hours of testing and finding different cases in which it might not work. I know from experience that it's possible, but will require from you a number of workarounds to be implemented so it can always obtain a stable WiFiP2P
connection.
Send to your devices, a mapping between those (unchangeable) WifiP2P
names and created room names, in some other way. For example through your server api or something...