Transfer data between two Wi-Fi devices

I searched on google. I've tried a lot. In Android 2.2 and sdk 8, how can I use the SSID in the list on Android? Use the SSID to use certain features of a Wi-Fi enabled device programmatically. With this, you should transfer data between two Wi-Fi-enabled devices in Android. Can someone help me in this please?

+6
source share
2 answers

To send data in a meaningful way between two Android devices, you must use a TCP connection. To do this, you need the IP address and port on which another device is listening.

Examples are taken from here .

For the server side (listener side), a server socket is required:

try { Boolean end = false; ServerSocket ss = new ServerSocket(12345); while(!end){ //Server is waiting for client here, if needed Socket s = ss.accept(); BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush String st = input.readLine(); Log.d("Tcp Example", "From client: "+st); output.println("Good bye and thanks for all the fish :)"); s.close(); if ( STOPPING conditions){ end = true; } } ss.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

For the client side, you need a socket that connects to the server socket. Please replace "localhost" with remote ip-address or hostname Android devices:

 try { Socket s = new Socket("localhost",12345); //outgoing stream redirect to socket OutputStream out = s.getOutputStream(); PrintWriter output = new PrintWriter(out); output.println("Hello Android!"); BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); //read line(s) String st = input.readLine(); //. . . //Close connection s.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
+17
source
  For data Transfer between 2 devices over the wifi can be done by using "TCP" protocol. Connection between Client and Server requires 3 things 1) Using NSD Manager, Client device should get server/host IP Address. 2) Send data to server using Socket. 3) Client should send its IP Address to server/host for bi-directional communication. 

For code layout see link

 For faster transmission of data over wifi can be done by using "WifiDirect" which is a "p2p" connection. so that this will transfer the data from one to other device without an Intermediate(Socket). For Example catch 

this link is in Google developers wifip2p and P2P connection with Wi-Fi

Catch a sample in Github WifiDirectFileTransfer

+2
source

Source: https://habr.com/ru/post/923103/


All Articles