2012-02-16 3 views
2

シェルにecho -e "AT\r" > /dev/smd0と書いて、その応答を得たいです。 応答は\dev\smd0になります。アンドロイドにコマンドを送信し、その答えを得るには?

私はグーグルを検索し、この発見:

Runtime r = Runtime.getRuntime(); 
      process = r.exec("su"); 
      process = r.exec("echo -e \"AT\\r\" > /dev/smd0"); 

を、それは動作しません。

私はどのように応答を読むのか分かりません。

私がTerminal Emulatorをインストールした場合、コマンドを書き込んで、その応答をcat \dev\smd0で得ることができます。

答えて

1

this Linkのおかげで問題を発見しました。

EDIT:

コードの下で、私はそれを読むことができます:

public class read implements Runnable { 

    private Thread mBlinker; 

    private ArrayList<String> output = new ArrayList<String>(); 

    public String getResponce() { 
     if (output.size() != 0) { 
      String ans = output.get(0); 
      output.remove(0); 
      return ans; 
     } 
     return null; 
    } 

    public void start() { 
     mBlinker = new Thread(this); 
     mBlinker.start(); 
    } 

    public void stop() { 
     mBlinker = null; 
    } 

    private DataInputStream dis; 
    private DataOutputStream dos; 

    @Override 
    public void run() { 
     System.out.println("START READ"); 
     try { 
      Runtime r = Runtime.getRuntime(); 
      Process process = r.exec("su"); 
      dos = new DataOutputStream(process.getOutputStream()); 
      dos.writeBytes("cat /dev/smd0\n"); 
      dos.flush(); 

      dis = new DataInputStream(process.getInputStream()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     while (mBlinker != null) { 
      try { 
       int av = dis.available(); 
       if (av != 0) { 
        byte[] b = new byte[av]; 
        dis.read(b); 
        output.add(new String(b)); 
        System.out.println(new String(b) + "Recieved form modem"); 
       } 
       else 
       { 
        Thread.sleep(100); 
       } 
      } catch (IOException e) { 
       if (e.getMessage() != null) 
        System.out.println(e.getMessage()); 
       e.printStackTrace(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     try { 
      dos.writeBytes("exit\n"); 
      dos.flush(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("STOP READ"); 
    } 
} 
+0

上記で提供した同じリンクで、行:文字列currUid = osRes.readLine(); – Muzikant

2

like thisをお試しください:

try { 
    Runtime r = Runtime.getRuntime(); 
    Process process = r.exec(" su -c 'echo -e \"AT\\r\" > /dev/smd0; cat /dev/smd0' "); 

    BufferedReader in = new BufferedReader( 
         new InputStreamReader(process.getInputStream())); 
    String line = null; 
    while ((line = in.readLine()) != null) { 
     System.out.println(line); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

はまたあなたの携帯電話へのrootアクセスを必要としています。我々は、コマンドを送信するためにDataOutPutStreamを使用する必要がsuを必要とするもの、コマンドの最後に、いくつかのコマンドのために必要な

Runtime r = Runtime.getRuntime(); 


      Process process = Runtime.getRuntime().exec("su"); 
      DataOutputStream os = new DataOutputStream(
        process.getOutputStream()); 
      os.writeBytes("echo -e \"AT\\r\" > /dev/smd0\n"); 
      os.flush(); 
      os.writeBytes("exit\n"); 
      os.flush(); 

\n

+0

を感謝しかし、私はこのエラーを与える: 2月16日08:58:30.869:WARN/SU(12559):リクエスト拒否されました(0-> 0 'エコー) –

+0

' cat 'の'/'に' \ 'を入れてみてください。 –

+0

私は "su -c" echo -e \ "AT \\ r \">/dev/smd0; cat/dev/smd0 '"をテストしますが、まだ問題があります –

関連する問題