How to use wifi to send data to the same app installed on other android devices
I have an app like “A” on my android device, the
same app like “B” installed on another android device, and now I want to send data from app “A” to app “B” using the WIFI service. So please advise me on how to implement this feature.
I tried several times to get help from Google, but in vain.
DIRECTLY VIA WIFI OR NFC.
Solution
You can use a simple P2P architecture.
You will need to use this, this and a pair of streams that deal with the type of data you need to send, eg this .
Sender:
Socket s = new Socket(IP,PORT);
s.connect();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.write("hello".toByteArray());
Then on the receiving side:
ServerSocket ss = new ServerSocket(PORT);
Socket s = ss.accept(); This call will block execution, use separate thread
DataInputStream dis = new DataInputStream(s.getInputStream);
byte[] data = dis.read();
With it, you can send and receive bytes simply using the stream that is appropriate for your data type.
Of course, once the connection is established, both clients can send/write by simply creating the appropriate input/output streams.
Hope this helps you.