how to Send string from Android to PC over wifi

浪子不回头ぞ 提交于 2019-11-26 17:58:17

问题


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 ?


回答1:


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.

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



回答2:


  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.Robot class.




回答3:


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.

Here is the java example code for soap standard:

    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:

    [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;
            }
       }
   }



回答4:


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 this and this.




回答5:


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.



来源:https://stackoverflow.com/questions/10388250/how-to-send-string-from-android-to-pc-over-wifi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!