Search code examples
androidclient-serverchatwifi-directwifip2p

Send and receive string in both client and server sides using WiFi Direct


My application is a simple WiFi Direct chat. I can connect two devices, but I don't know how I should send and receive a String (not a File) in both sides (not just from client to server like the developer example)

In both sides: I just want an EditText and a Button that when clicked sends EditText.getText() to the other side
If anyone has full code, please send me a link. Thanks a lot


Solution

  • Android's WiFiDirectServiceDiscovery sample project does that:

    package com.example.android.wifidirect.discovery;
    
    import android.os.Handler;
    import android.util.Log;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    
    /**
     * Handles reading and writing of messages with socket buffers. Uses a Handler
     * to post messages to UI thread for UI updates.
     */
    public class ChatManager implements Runnable {
    
        private Socket socket = null;
        private Handler handler;
    
        public ChatManager(Socket socket, Handler handler) {
            this.socket = socket;
            this.handler = handler;
        }
    
        private InputStream iStream;
        private OutputStream oStream;
        private static final String TAG = "ChatHandler";
    
        @Override
        public void run() {
            try {
    
                iStream = socket.getInputStream();
                oStream = socket.getOutputStream();
                byte[] buffer = new byte[1024];
                int bytes;
                handler.obtainMessage(WiFiServiceDiscoveryActivity.MY_HANDLE, this)
                        .sendToTarget();
    
                while (true) {
                    try {
                        // Read from the InputStream
                        bytes = iStream.read(buffer);
                        if (bytes == -1) {
                            break;
                        }
    
                        // Send the obtained bytes to the UI Activity
                        Log.d(TAG, "Rec:" + String.valueOf(buffer));
                        handler.obtainMessage(WiFiServiceDiscoveryActivity.MESSAGE_READ,
                                bytes, -1, buffer).sendToTarget();
                    } catch (IOException e) {
                        Log.e(TAG, "disconnected", e);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public void write(byte[] buffer) {
            try {
                oStream.write(buffer);
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }
    
    }