两个Wifi设备之间的数据传输
问题描述:
我已经在Google中搜索。我尝试了很多。在Android 2.2和sdk 8中,我如何在Android的List中使用SSID?通过使用SSID应该通过编程获得特定的WiFi启用设备属性。有了这个帮助,应该可以在Android中的两个启用Wifi的设备之间传输数据。任何人都可以帮我解决这个问题吗?两个Wifi设备之间的数据传输
答
要在两个Android设备之间以有意义的方式发送数据,您可以使用TCP连接。为此,您需要使用其他设备正在侦听的IP地址和端口。
示例摘自here。
对于服务器端(监听方),你需要一个服务器套接字:
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();
}
对于需要连接到服务器套接字的套接字客户端。请更换 “本地主机” 与远程Android设备的IP地址或主机名:
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();
}
答
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.
对于代码verfication看到这个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
在谷歌开发这个环节wifip2p和P2P Connection with Wi-Fi
在Github上抓取样本WifiDirectFileTransfer