2012-02-16 56 views
2

我想在shell中編寫echo -e "AT\r" > /dev/smd0然後得到它的響應。 回覆將在\dev\smd0如何發送一個命令到android然後得到它的答案?

我搜索谷歌和發現這一點:

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

,但它不工作。

而且我不知道如何閱讀回覆。

如果我安裝終端仿真器,我可以編寫命令並獲得其響應cat \dev\smd0

回答

1

發現的this Link謝謝問題:

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需要在命令的結束和一些命令有什麼需要su我們需要使用DataOutPutStream發送命令。

編輯:

下面代碼我可以讀取它:

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

在上面提供的同一鏈接上, line:String currUid = osRes.readLine(); – Muzikant 2012-02-18 23:29:02

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權限。

+0

感謝,但我給此錯誤: 8月2日至16日:58:30.869:WARN/SU(12559):請求拒絕(0-> 0回聲) – 2012-02-16 10:07:14

+0

嘗試將'\'替換爲'cat'中的'/',我剛剛從您的問題中複製了它。 – 2012-02-16 10:11:02

+0

我測試了「su -c'echo -e \」AT \\ r \「>/dev/smd0; cat/dev/smd0'」但存在問題 – 2012-02-16 10:14:04

相關問題