gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:59 26 4
gpt4 key购买 nike

我已经在 Google 中搜索过了。在 Android 2.2 和 sdk 8 中,如何在 Android 的列表中使用 SSID?

通过使用 SSID,应以编程方式获取特定的启用 wifi 的设备属性。在这种帮助下,应该可以在 Android 中的两个支持 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();
}

对于客户端,您需要一个连接到服务器套接字的套接字。请将“localhost”替换为远程 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();
}

关于android - 两个 Wifi 设备之间的数据传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12002303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com