2012-08-17 7 views
6

Ho cercato su Google. Ho provato molto. In Android 2.2 e sdk 8 come posso usare SSID in una lista in Android? Usando SSID Dovrebbe ottenere specifiche proprietà del dispositivo wifi abilitate da programmaticamente. Con quell'aiuto, dovrebbe trasferire i dati tra due dispositivi abilitati Wifi in Android. Qualcuno può aiutarmi in questo plz?Trasferimento dati tra due dispositivi Wifi

risposta

17

per inviare i dati in modo significativo tra i due dispositivi Android si usa una connessione TCP. Per farlo è necessario l'indirizzo IP e la porta su cui l'altro dispositivo è in ascolto.

Gli esempi sono tratti da here.

Per la (lato di ascolto) lato server è necessario un socket server:

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

Per il lato client è necessario un socket che si connette al socket server. Si prega di sostituire "localhost" con i dispositivi Android indirizzo IP o il nome host remoto:

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. 

Per il codice verfication vedono questo 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 

questo link nel sviluppatori di Google wifip2p e P2P Connection with Wi-Fi

Cattura un campione in Github WifiDirectFileTransfer