简体   繁体   English

两个Wifi设备之间的数据传输

[英]Data transfer between two Wifi devices

I have searched in Google. 我在谷歌搜索过。 In Android 2.2 and sdk 8 how can I use SSID in a List in Android ? 在Android 2.2和sdk 8中如何在Android中的List中使用SSID?

By using SSID should get specific wifi enabled device properties by programmatically. 通过使用SSID,应通过编程方式获取特定的WiFi启用设备属性。 With that help, should transfer the data between two Wifi enabled devices in Android. 有了这个帮助,应该在Android中的两个支持Wifi的设备之间传输数据。

To send data in a meaningful manner between two Android devices you would use a TCP connection. 要在两个Android设备之间以有意义的方式发送数据,您将使用TCP连接。 To do that you need the ip address and the port on which the other device is listening. 为此,您需要IP地址和其他设备正在侦听的端口。

Examples are taken from here . 例子来自这里

For the server side (listening side) you need a server socket: 对于服务器端(监听端),您需要一个服务器套接字:

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

For the client side you need a socket that connects to the server socket. 对于客户端,您需要一个连接到服务器套接字的套接字。 Please replace "localhost" with the remote Android devices ip-address or hostname: 请将“localhost”替换为远程Android设备的ip-address或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();
}

For data Transfer between 2 devices over the wifi can be done by using "TCP" protocol. 对于数据,可以使用“TCP”协议通过wifi在2个设备之间进行传输。 Connection between Client and Server requires 3 things 客户端和服务器之间的连接需要3件事

  1. Using NSD Manager, Client device should get server/host IP Address. 使用NSD Manager,客户端设备应获取服务器/主机IP地址。
  2. Send data to server using Socket. 使用Socket将数据发送到服务器。
  3. Client should send its IP Address to server/host for bi-directional communication. 客户端应将其IP地址发送到服务器/主机以进行双向通信。

For faster transmission of data over wifi can be done by using "WifiDirect" which is a "p2p" connection. 为了通过wifi更快地传输数据,可以使用“WifiDirect”,这是一个“p2p”连接。 so that this will transfer the data from one to other device without an Intermediate(Socket). 这样就可以在没有中间(套接字)的情况下将数据从一个设备传输到另一个设备。 For example, see this link in google developers wifip2p and P2P Connection with Wi-Fi . 例如,请参阅google开发人员wifip2p中的此链接以及使用Wi-Fi的P2P连接

Catch a sample in Github WifiDirectFileTransfer 在Github WifiDirectFileTransfer中捕获一个示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM