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

Ach*_*ver 6 android android-wifi

我在谷歌搜索过.在Android 2.2和sdk 8中如何在Android中的List中使用SSID?

通过使用SSID,应通过编程方式获取特定的WiFi启用设备属性.有了这个帮助,应该在Android中的两个支持Wifi的设备之间传输数据.

Dir*_*kel 19

要在两个Android设备之间以有意义的方式发送数据,您将使用TCP连接.为此,您需要IP地址和其他设备正在侦听的端口.

例子来自这里.

对于服务器端(监听端),您需要一个服务器套接字:

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();
}
Run Code Online (Sandbox Code Playgroud)

对于客户端,您需要一个连接到服务器套接字的套接字.请将"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();
}
Run Code Online (Sandbox Code Playgroud)