javaandroidargument-passingwifi-directwifip2p

Usage of WiFi-Direct in Game Development (Android)


I am developing board game in android. I want to make this game playable on two separate devices, for that I need to use WiFi-Direct. I want to know is there any library available which will help me to

I am interested in built-in library. OR If possible please share implemented example of client/server architecture.


Solution

  • Make the device that starts the game run as a TCP server and make it broadcast on the network and listen on a predetermined port. When another player wants to join, he just selects the server from a menu and join the game. The coordinates can be sent as packets on touch events.

    This is for the server:

    Thread serverThread = new Thread(new Runnable() {
    
        @Override
        public void run() {                                 
        
        try { 
             serverSocketTCP = new ServerSocket(); 
             serverSocketTCP.setReuseAddress(true); 
             serverSocketTCP.bind(new InetSocketAddress(YourPort));
    
       while (status) {
            clientSocketTCP = serverSocketTCP.accept();
    
            BufferedReader bufferedReader = new BufferedReader(new 
            InputStreamReader(client.getInputStream()));
    
            OutputStream outputStream = client.getOutputStream();
    
    
    
         }
      } catch (Exception e) {
         e.printStackTrace();
                                        
       }
        });
    serverThread.start();
    

    This is for the client:

    Socket  clientSocket = new Socket(ServerIP,ServerPort);
    outputStream = clientSocket.getOutputStream();
    bufferedReader=newBufferedReader(new 
    InputStreamReader(clientSocket.getInputStream()));