2013-04-29 1 views
1

J'essaie de me connecter à l'émulateur Android en utilisant un TcpClient. L'émulateur est Android 4.2.2 en cours d'exécution sur localhost: 5554 que je démarre à partir de AVD Manager. Je peux me connecter et envoyer la commande 'power status discharge' mais après l'envoi de la seconde commande, le programme se bloque en attente de réponse. Les commandes fonctionnent lorsque je me connecte en utilisant la connexion brute Putty.Connexion à l'émulateur Android à l'aide de .NET C#

Voici le code complet:

using System; 
using System.Net.Sockets; 
using System.Text; 

namespace AndroidBatteryChangeEmulator 
{ 
    class Program 
    { 
     private static readonly TcpClient connection = new TcpClient(); 

     static void Main(string[] args) 
     { 
      try 
      { 
       connection.Connect("localhost", 5554); 
       NetworkStream stream = connection.GetStream(); 

       ReadDataToConsole(stream); 
       SendCommand(stream, "power status discharging"); 

       string command = string.Format("power capacity {0}", 50); 
       SendCommand(stream, command); 

       stream.Close(); 
       connection.Close(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("The following error has occured: {0}", ex.Message); 
      } 
     } 

     private static void ReadDataToConsole(NetworkStream stream) 
     { 
      var responseBytes = new byte[connection.ReceiveBufferSize]; 
      stream.Read(responseBytes, 0, connection.ReceiveBufferSize); 

      string responseText = Encoding.ASCII.GetString(responseBytes).Trim(new[] { ' ', '\0', '\n', '\r' }); 
      if (!string.IsNullOrEmpty(responseText)) 
       Console.WriteLine("Response: '{0}'.", responseText); 
     } 

     private static void SendCommand(NetworkStream stream, string command) 
     { 
      Console.WriteLine("Sending command '{0}'.", command); 
      byte[] commandBytes = Encoding.ASCII.GetBytes(command + "\r"); 
      Buffer.BlockCopy(command.ToCharArray(), 0, commandBytes, 0, commandBytes.Length); 

      stream.Write(commandBytes, 0, commandBytes.Length); 

      ReadDataToConsole(stream); 
     } 

    } 
} 

Voici la sortie du programme:

Response: 'Android Console: type 'help' for a list of commands'. 
Sending command 'power status discharging'. 
Response: 'OK'. 
Sending command 'power capacity 50'. 

Je ne sais pas ce qui cause le problème.

Merci pour l'aide à l'avance!

Répondre

0

Si quelqu'un se demande, je résolu le problème en enveloppant le NetworkStream avec StreamReader dans la fonction ReadDataToConsole() et StreamWriter dans la fonction SendCommand(). Assurez-vous que AutoFlush est true dans StreamWriter!

Tout fonctionne maintenant!

0

Cela vous dérangerait de poster votre code de travail? Voici le code que j'utilise (pour les futurs visiteurs):

using (TcpClient client = new TcpClient(host, port)) 
{ 
    using (NetworkStream stream = client.GetStream()) 
    { 
     using (StreamReader reader = new StreamReader(stream)) 
     { 
      using (StreamWriter writer = new StreamWriter(stream)) 
      { 
       writer.AutoFlush = true; 
       foreach (string command in commandList) 
       { 
        writer.WriteLine(command); 
        string response = reader.ReadLine(); 
        Thread.Sleep(5000); 
       } 
      } 
     } 
    } 
} 

Martin