2012-04-30 47 views
8

你好我正在一個Android應用程序,需要發送一個字符串通過WiFi到PC導致模擬鍵盤按鍵。任何想法我可以如何實現這一任務?如何發送字符串從Android到PC通過WiFi

+0

你可以使用這個鏈接 [此處輸入鏈接的描述(http://stackoverflow.com/questions/10388250/how-to-send-string-從android-to-pc-over-wifi?noredirect = 1&lq = 1) –

+0

對不起,您可以使用此鏈接:[請輸入鏈接描述](http://stackoverflow.com/questions/31611593/send-text-從功能的Android到PC的通過的Wi-Fi連接?noredirect = 1&lq = 1) –

回答

28

你會寫在PC上的服務器程序,並使用一個ServerSocket接受的連接,並寫你的Android手機一個線程,使用常規插座(具有相同的端口作爲PC端)然後使用DataInputStream和DataOutputStream來管理它們。您還需要在AndroidManifest.xml中打開權限。

對於權限使用:

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

的代碼這裏有一個小例子:

服務器:

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

好的我已經做了客戶端服務器套接字連接和服務器通知我,如果連接建立,但我如何使客戶端通知我的連接? –

+0

你可以使用socket.isConnected() –

0

我不能爲您提供完整的代碼,但至少可以引導您在一個正確的方向。要達到此目的,您需要使用Sockets。現在,如果你在互聯網上搜索,你會發現許多文章和例子涉及到這個主題指定Android。例如thisthis

+0

好的我已經完成了客​​戶端服務器套接字連接,並且服務器通知我連接建立了,但是如何讓客戶端通知我連接? –

0

您可能需要爲個人電腦編寫某種程序,作爲Android應用程序通過Socket或Stream發送的「服務器」。

+0

好的我已經完成了客​​戶端服務器套接字連接,並且服務器通知我連接已建立,但是如何讓客戶端通知我該連接? –

1
  1. 通信部分相當簡單。在PC上打開一個TCP服務器,並在Android設備上有一個TCP客戶端發送它的字符串/命令。 可以找到一個很好的教程here,但您需要根據需要對其進行修改。

    請注意,使用TCP時,不應該從主線程完成,而應從後臺線程完成。一個好的方法是AsyncTask(當你到達那裏)。

  2. 另一部分是鍵盤模擬。爲此,您需要使用java.awt.Robot類。

+0

謝謝,但我還需要在PC上接收字符串將我的PC作爲服務器? Android應用程序和PC應用程序如何通信? –

+0

這正是TCP套接字的用途。爲兩個設備進行通信。 – MByD

+0

好的我已經做了客戶端服務器套接字連接和服務器通知我,如果連接建立,但我怎麼能讓客戶端通知我的連接? –

1

根據您的網站erver設計你要麼使用平靜的通信或肥皂,然後通過HTTP協議發送你的數據到你的web服務,並從它得到所需的答案。我已經寫了一個用於soap方法的asp web服務,我將在下面進行解釋。

這裏是肥皂標準的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; 
} 

現在你只需要調用從適當的活動這種方法,讓你的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; 
      } 
     } 
    } 
相關問題