如何发送字符串从安卓到电脑通过WiFi字符串、电脑、安卓到、WiFi

2023-09-05 07:02:43 作者:相見不如懷念

您好我工作的一个Android应用程序,需要发送一个字符串通过wifi到电脑导致模拟键盘按键presses.Any想法我如何能实现这个任务?

Hello i am working on an android app which requires to send a string over wifi to PC resulting in simulating keyboard keypresses.Any ideas how i can achieve this task ?

推荐答案

您必须在PC上编写一个服务器程序,并使用一个ServerSocket接受的连接,写一个线程用于你的Andr​​oid手机,它使用普通插座(具有相同的端口作为PC端),然后使用DataInputStream和DataOutputStream类管理。您还需要打开你的Andr​​oidManifest.xml中的权限。

You would have to write a server program on the PC and use a ServerSocket to accept a connection from and write a thread for your Android phone that uses a regular socket (with the same port as the PC end) and then manage them using DataInputStream and DataOutputStream. You also need to open permissions on your AndroidManifest.xml.

有关权限使用这样的:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

对于code这里有一个小例子:

For the code here's a little example:

服务器:

String msg_received;

ServerSocket socket = new ServerSocket(1755);
Socket clientSocket = socket.accept();       //This is blocking. It will wait.
DataInputStream DIS = new DataInputStream(clientSocket.getInputStream());
msg_received = DIS.readUTF();
clientSocket.close();
socket.close();

客户端:

Socket socket = new Socket("192.168.0.1",1755);
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
DOS.writeUTF("HELLO_WORLD");
socket.close();