2012-08-17 32 views
6

Tôi đã tìm kiếm trong Google. Tôi đã thử rất nhiều. Trong Android 2.2 và sdk 8 làm cách nào tôi có thể sử dụng SSID trong Danh sách trong Android? Bằng cách sử dụng SSID sẽ nhận được các thuộc tính thiết bị hỗ trợ wifi cụ thể theo lập trình. Với sự trợ giúp đó, nên chuyển dữ liệu giữa hai thiết bị hỗ trợ Wifi trong Android. Bất cứ ai có thể giúp tôi trong plz này?Truyền dữ liệu giữa hai Thiết bị Wifi

Trả lời

17

Để gửi dữ liệu một cách có ý nghĩa giữa hai thiết bị Android, bạn sẽ sử dụng kết nối TCP. Để làm điều đó bạn cần địa chỉ IP và cổng mà thiết bị kia đang nghe.

Ví dụ được lấy từ here.

Đối với (bên nghe) phía máy chủ bạn cần một ổ cắm máy chủ:

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

Đối với các mặt hàng bạn cần một ổ cắm kết nối với ổ cắm máy chủ. Hãy thay thế "localhost" với điều khiển từ xa các thiết bị Android ip-address hoặc hostname:

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. 

Đối với mã xác minh thấy điều này 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 

liên kết này trong phát triển google wifip2pP2P Connection with Wi-Fi

Chụp mẫu trong Github WifiDirectFileTransfer

Các vấn đề liên quan