如何通过wifi将字符串从Android发送到PC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10388250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:31:22  来源:igfitidea点击:

how to Send string from Android to PC over wifi

androidandroid-wifionkeypress

提问by Hasham Tahir

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 ?

您好,我正在开发一个 android 应用程序,该应用程序需要通过 wifi 向 PC 发送一个字符串,从而模拟键盘按键。有什么想法可以实现此任务吗?

回答by Moises Jimenez

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.

您必须在 PC 上编写一个服务器程序并使用 ServerSocket 接受来自使用常规套接字(与 PC 端具有相同端口)的 Android 手机的连接并为其编写线程,然后使用 DataInputStream 和数据输出流。您还需要在 AndroidManifest.xml 上打开权限。

For the permissions use this:

对于权限使用这个:

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

For the code here's a little example:

对于这里的代码,有一个小例子:

Server:

服务器:

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

Client:

客户:

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

回答by MByD

  1. The communication part is rather easy. Open a TCP server on the PC, and have a TCP Client on the Android device sending it Strings / Commands. A nice tutorial may be found here, but you will need to modify it for your needs.

    Notice that when working with TCP, it should not be done from the main thread, but from a background thread. A good method for that is AsyncTask(When you'll get there).

  2. The other part is the keyboard simulation. For that you need to use the java.awt.Robotclass.

  1. 交流部分比较简单。在 PC 上打开一个 TCP 服务器,并在 Android 设备上有一个 TCP 客户端向它发送字符串/命令。可以在此处找到一个不错的教程,但您需要根据需要对其进行修改。

    请注意,在使用 TCP 时,不应从主线程完成,而应从后台线程完成。一个很好的方法是AsyncTask(当你到达那里时)。

  2. 另一部分是键盘模拟。为此,您需要使用java.awt.Robot类。

回答by AmiNadimi

based on your web server design you either use restful communication or soap and then send your data over HTTP protocol to your web service and get the desired answer from it. i have written an asp web service for soap approach that i will explain below.

根据您的网络服务器设计,您可以使用安静的通信或肥皂,然后通过 HTTP 协议将数据发送到您的网络服务并从中获得所需的答案。我已经为soap方法编写了一个asp web服务,我将在下面解释。

Here is the java example code for soap standard:

这是soap标准的java示例代码:

    private static String NameSpace = "http://tempuri.org/";
    //below url must be your service url, mine is a local one
    private static String URL = "http://192.168.2.213/hintsservice/service.asmx";
    private static String SOAP_ACTION = "http://tempuri.org/";

    public static String Invoke(String s) {
    //respond string from server
    String resTxt = "";
    //the name of your web service method
    final String webMethName = "Hint";
     // Create request
    SoapObject request = new SoapObject(NameSpace, webMethName);
    // Property which holds input parameters
    PropertyInfo PI = new PropertyInfo();
    // Set Name
    PI.setName("s");
    // Set Value
    PI.setValue(s);
    // Set dataType
    PI.setType(String.class);
    // Add the property to request object
    request.addProperty(PI);
    // Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    //Set envelope as dotNet
    envelope.dotNet = true;
    // Set output SOAP object
    envelope.setOutputSoapObject(request);
    // Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
        // Invoke web servi.ce
        androidHttpTransport.call(SOAP_ACTION + webMethName, envelope);
        // Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to resTxt variable static variable
        resTxt = response.toString();
    }catch (Exception e) {
        //Print error
        e.printStackTrace();
        //Assign error message to resTxt
        resTxt = "Error occured";
    }
     //Return resTxt to calling object
    return resTxt;
}

now you just need to call this method from appropriate activity and let your web service do the rest. Here is the example web service in C# language:

现在您只需要从适当的活动中调用此方法,然后让您的 Web 服务完成剩下的工作。以下是 C# 语言的示例 Web 服务:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]

    public class Service : System.Web.Services.WebService
    {
        public Service () {
            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
            [WebMethod]
            public string Hint(string s) {
                string response = string.Empty;
                //todo: produce response
                return response;
            }
       }
   }

回答by waqaslam

I cant offer you full code but at least can guide you in a right direction. To achieve this, you need to use Sockets. Now if you search on the Internet you'll find lots of articles and examples related to this subject specifying Android. For example thisand this.

我不能为您提供完整的代码,但至少可以指导您朝着正确的方向前进。为此,您需要使用Sockets。现在,如果您在 Internet 上搜索,您会发现许多与此主题相关的文章和示例,这些文章和示例都指定了 Android。例如这个这个

回答by Justin Pearce

You're likely going to have to write some sort of program for the PC that acts as a 'server' for the Android app to send to via a Socket or Stream.

您可能需要为 PC 编写某种程序,充当 Android 应用程序的“服务器”,通过 Socket 或 Stream 向其发送数据。