how to Send string from Android to PC over wifi
Asked Answered
S

6

12

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 ?

Sheath answered 30/4, 2012 at 17:54 Comment(1)
Excuse me You can use this link: enter link description hereFieldsman
M
29

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();
Mumps answered 30/4, 2012 at 18:54 Comment(2)
ok i have done the client server socket connection and server notify me if the connection is established but how can i make the client to notify me of the connection ?Sheath
You can use socket.isConnected()Mumps
C
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.

Ced answered 30/4, 2012 at 17:59 Comment(3)
Thanks but i also need the string to be received on the PC will my PC act as a server? how would the android app and PC app communicate?Sheath
That's exactly what the TCP sockets are for. for both devices to communicate.Ced
ok i have done the client server socket connection and server notify me if the connection is established but how can i make the client to notify me of the connection ?Sheath
I
1

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;
            }
       }
   }
Inhesion answered 28/2, 2016 at 6:50 Comment(0)
J
0

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.

Jessamine answered 30/4, 2012 at 17:57 Comment(1)
ok i have done the client server socket connection and server notify me if the connection is established but how can i make the client to notify me of the connection ?Sheath
G
0

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.

Grimmett answered 30/4, 2012 at 17:58 Comment(1)
ok i have done the client server socket connection and server notify me if the connection is established but how can i make the client to notify me of the connection ?Sheath
D
0

A good practical and easy way to do it, with a lot of community, is using RabbitMQ. I use it for my project and I send strings https://www.rabbitmq.com/getstarted.html

Once you learn the basics, you will be able to scale the project to more things. In principle, tutorial 1 is enough to send and receive strings.

Follow the server installation instructions and open the requested ports or use docker.

If you want more things of RabbitMQ in android-server, ask me.

Dygert answered 22/11, 2021 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.