How to send a string from Android to a PC via Wi-Fi

Hi, I am working on an Android application that requires sending a string via Wi-Fi to a PC, which leads to simulating keyboard keys. Any ideas how I can achieve this?

+8
android android wifi onkeypress
source share
5 answers

You will need to write the server program on the PC and use the ServerSocket to receive the connection and record the stream for your Android phone that uses a regular socket (with the same port as the PC), and then manage it using DataInputStream and DataOutputStream. You also need to open permissions on your AndroidManifest.xml.

For permissions use this:

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

Here is a small example for the code:

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(); 
+28
source share
  • The communication part is quite simple. Open the TCP server on the PC and ask the TCP client on the Android device to send it Strings / Commands. A good tutorial can be found here , but you will need to change it for your needs.

    Please note that when working with TCP, this should not be done from the main thread, but from the background thread. A good way to do this is AsyncTask (When you get there).

  • The other part is keyboard simulation. For this you need to use the java.awt.Robot class.

+1
source share

based on the design of your web server, you either use a quiet connection or soap, and then send your data via HTTP to your web service and get the desired response from it. I wrote an asp web service for a soapy approach, which I will explain below.

Here is an example java code for the 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 the appropriate activity and let your web service do the rest. Here is an example C # web service:

  [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; } } } 
+1
source share

I cannot offer you the complete code, but at least it can help you in the right direction. For this you need to use Sockets . Now, if you are looking on the Internet, you will find many articles and examples related to this topic that define Android. For example this and this .

0
source share

You probably have to write some kind of PC program that acts as a β€œserver” for the Android application to be sent via Socket or Stream.

0
source share

All Articles