2017-05-25 71 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

继续前进,它不会伤害=) –