2017-05-25 66 views
1

我需要發送命令的列表OBD端口與一些延遲,因爲ELM327無法管理所有的命令一起...的Android發送ArrayList的命令OBD

我想這個代碼,但不工作

public void repeatCommand(){ 


     for (final String command : commandArray){ 

      Log.d(TAG, "Giro for"); 

      final Handler handlerTimed = new Handler(); 
      handlerTimed.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        //Do something after 100ms 
        sendMessage(command); 
       } 
      }, 1000); 


     } 

     /*String message = "010C\r"; 
     sendMessage(message);*/ 
    } 

它只發送1秒後的第一個命令,但其他命令不行。 如何發送延遲的所有命令,讓寫入管理髮送給OBD的所有命令?

好吧,我使用建議的方法,發送第一個命令,並等待響應....當獲得響應,發送下一條消息。

private synchronized void manage(BluetoothSocket socket, BluetoothDevice 
      device) { 
     Log.d(TAG, "connected, Socket Type:"); 

     // Cancel the thread that completed the connection 
     if (mConnectThread != null) { 
      mConnectThread.cancel(); 
      mConnectThread = null; 
     } 

     // Cancel any thread currently running a connection 
     if (mConnectedThread != null) { 
      mConnectedThread.cancel(); 
      mConnectedThread = null; 
     } 
     // Cancel any thread currently managing connections 
     if (mManageThread != null) { 
      mManageThread.cancel(); 
      mManageThread = null; 
     } 

     // Start the thread to manage the connection and perform transmissions 
     mManageThread = new ManageDataThread(socket); 
     mManageThread.start(); 

     // Send the name of the connected device back to the UI Activity 
     Message msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME); 
     Bundle bundle = new Bundle(); 
     bundle.putString(Constants.DEVICE_NAME, device.getName()); 
     msg.setData(bundle); 
     mHandler.sendMessage(msg); 
     // Update UI title 
     updateUserInterfaceTitle(); 
    } 

這裏說的管理連接線程..

public class ManageDataThread extends Thread { 

     private final BluetoothSocket mmSocket; 
     private final InputStream mmInStream; 
     private final OutputStream mmOutStream; 
     private boolean wait_response = false; 

     public ManageDataThread(BluetoothSocket socket) { 
      Log.d(TAG, "create ManageDataThread: "); 

      mmSocket = socket; 
      InputStream tmpIn = null; 
      OutputStream tmpOut = null; 

      // Get the BluetoothSocket input and output streams 
      try { 
       tmpIn = socket.getInputStream(); 
       tmpOut = socket.getOutputStream(); 
      } catch (IOException e) { 
       Log.e(TAG, "temp sockets not created", e); 
      } 

      mmInStream = tmpIn; 
      mmOutStream = tmpOut; 
      mState = STATE_CONNECTED; 

     } 

     public void run() { 

      while(true) { 
       for (final String command : commandArray) { 

        byte[] send = command.getBytes(); 
        write(send); 
        //mState = STATE_WAIT_RESPONSE; 

        byte[] buffer = new byte[1024]; 
        int bytes; 

        // Keep listening to the InputStream while connected 
        while (wait_response) { 
         try { 
          // Read from the InputStream 
          bytes = mmInStream.read(buffer); 

          //TODO devo gestire l'arrivo di bytes 
          ObdCommand obc = new ObdCommand(); 
          obc.readResult(mmInStream); 

          formattedMessage = obc.getResult(); 
          //buffer = (byte) obc.getBuffer(); 

          // Send the obtained bytes to the UI Activity 
          mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, formattedMessage) 
            .sendToTarget(); 

          wait_response = false; 

         } catch (IOException e) { 
          Log.e(TAG, "disconnected", e); 
          connectionLost(); 
          break; 
         } 
        } 


       } 
      } 



     } 

這一點不完美,但現在它的工作.... 我會打開它停止新職位和更新的數組列表命令,因爲如果我改變命令的列表,循環保持舊數組列表,所以我需要通知線程ArrayList中具有改變

編輯

不要使用while(true)內部線程,最好使用設置爲True的變量e需要停止線程時將該變量設置爲false,或者在停止線程時發生問題....

+0

查看我的回答和plz讓我知道@Dario –

回答

1

使用OBD2(一種串行協議)的正確方法是使用特定於命令的回調來實現類似命令隊列的功能,該回調將響應傳遞給您向其請求的命令。命令隊列應該在後臺線程中工作,操作隊列並監聽串行端口。甚至不要啓動延遲或類似的方法。

+0

但我需要每個命令的延遲...我讀了OBD需要接近50millisec的時間來管理「answere」......我連續發送不發回所有響應的命令....或者我錯了? 如果我使用簡單的發送陣列中的所有comannds我得到錯誤響應.. for是太快 – Dario

+0

事情是......你永遠不會知道車輛需要多長時間迴應。這取決於ECU(加上BTLE或WiFi通信開銷) - 因此,每次延遲都會太大或太小。更好地閱讀所有內容,直到收到響應終止符'\ r \ r>',然後發送隊列中的下一個命令。 – DrMickeyLauer

+0

好吧,所以你建議一個帶命令的隊列...發送第一個命令並等待響應,當我得到並讀取命令的響應時,我發送第二個命令並執行相同的過程,直到隊列全部獲得。 我如何syncronize寫和讀?你有什麼建議? 我需要從汽車到手機的實時數據,以獲取所有(4)命令,並顯示數據,直到應用程序處於打開狀態或所選命令發生更改(對此使用preferenceActivity) 感謝 – Dario

1

有一件事需要考慮是當你創建一個處理器並且不提供一個循環器時它將使用它的循環器當前線程
所以我最好的猜測,因爲我們沒有你的代碼,你實際上是在一個不是主要的線程中運行它。確保你的線程不會在途中被殺死,它應該可以工作。

而且,一些編碼提示:
首先,我必須說,這將是更有效地設置處理外循環
這樣你不會浪費內存在每次迭代中創建一個處理程序。
因此,它應該是這個樣子的是:

public void repeatCommand() 
{ 
    final Handler handlerTimed = new Handler(); 

    for (final String command : commandArray){ 

     Log.d(TAG, "Giro for"); 

     handlerTimed.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       //Do something after 100ms 
       sendMessage(command); 
      } 
     }, 1000); 
    } 
} 

其次,我強烈建議您將設置你的類的consts,就像1000值。

+0

我可以粘貼所有Main_activity代碼,如果可以幫助 – Dario

+0

繼續前進,它不會傷害=) –