2012-08-17 13 views
6

He buscado en Google. Lo intenté mucho. En Android 2.2 y SDK 8, ¿cómo puedo usar SSID en una lista en Android? Al usar SSID, debe obtener propiedades específicas del dispositivo WiFi habilitado programáticamente. Con esa ayuda, debe transferir los datos entre dos dispositivos habilitados para WiFi en Android. ¿Alguien puede ayudarme en esto por favor?Transferencia de datos entre dos dispositivos Wifi

Respuesta

17

Para enviar datos de manera significativa entre dos dispositivos Android, usaría una conexión TCP. Para hacer eso necesitas la dirección IP y el puerto en el que el otro dispositivo está escuchando.

Los ejemplos están tomados del here.

Para el (lado escuchar) del lado del servidor se necesita un socket de servidor:

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(); 
} 

Por el lado del cliente que necesita un enchufe que se conecta al socket del servidor. Por favor, reemplazar "localhost" con el mando a distancia dispositivos Android dirección IP o nombre de host:

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(); 
} 
2
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. 

Para el código VERIFICACIÓN ver este 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 

este enlace en Google Developers wifip2p y P2P Connection with Wi-Fi

Capture una muestra en Github WifiDirectFileTransfer

Cuestiones relacionadas