2012-08-17 24 views
6

Szukałem w Google. Próbowałem dużo. W Androidzie 2.2 i sdk 8 w jaki sposób mogę używać SSID na liście w systemie Android? Korzystając z identyfikatora SSID Powinno się uzyskać konkretne właściwości urządzenia z włączoną obsługą wifi, programowo. Dzięki tej pomocy, należy przesłać dane między dwoma urządzeniami obsługującymi Wi-Fi w systemie Android. Czy ktoś może mi pomóc w tym plz?Transfer danych między dwoma urządzeniami WiFi

Odpowiedz

17

Aby wysłać dane w znaczący sposób między dwoma urządzeniami z Androidem, należy użyć połączenia TCP. Aby to zrobić, potrzebujesz adresu ip i portu, na którym słuchasz drugiego urządzenia.

Przykłady pochodzą z here.

Dla (strony słuchania) po stronie serwera trzeba gniazdo serwera:

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

po stronie klienta trzeba gniazdo, który łączy się z gniazdem serwera. Proszę zastąpić "localhost" z zdalnych urządzeń Android adres IP lub nazwę hosta:

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. 

Dla kodu verfication zobaczyć ten 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 

ten link w deweloperów google wifip2p i P2P Connection with Wi-Fi

Złap próbkę w Github WifiDirectFileTransfer

Powiązane problemy