我在谷歌上搜索过。在 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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)