2012-04-10 140 views
2

----------------------编辑------------------------ -
有一点进展,但不是解决方案。如果我在要发送的命令之间插入以下代码,至少允许命令有时间在远程端进行处理(但这仍然不是正确的方法,正确的方法是等待在发送另一个命令之前响应“>”)...蓝牙Android SPP,发送串口命令到设备?

android.os.SystemClock.sleep(150);

在系统时钟正在休眠的过程中,监听线程被阻塞,所以调制解调器的输入不会被附加到文本视图,直到代码序列被发送后,这并不理想。再一次,睡眠不是正确的方式,我需要找到更好的方法,所以我不发送新的命令,直到“>”结果从另一端的设备回来。当我完成这段代码时,我需要一种方法来处理输入,所以如果我想一想,睡眠真的没有进展。插入睡眠示例:

sendData("enable"); 
    android.os.SystemClock.sleep(150); 
    sendData("password");   
    android.os.SystemClock.sleep(150); 
    sendData("conf t");   
    android.os.SystemClock.sleep(150);  
    sendData("interface eth0");   
    android.os.SystemClock.sleep(150);  
    //etc...etc...etc... 

---------------------- original post below ----------- ---------------

我与马特·贝尔的博客这个文笔优美的代码,设在这里的工作:设在这里 http://bellcode.wordpress.com/2012/01/02/android-and-arduino-bluetooth-communication/

来源: http://project-greengiant.googlecode.com/svn/trunk/Blog/Android%20Arduino%20Bluetooth

不切断c我试图以一种优雅地将命令串行地发送到所连接的调制解调器的方式,每次等待直到在发送下一个命令之前接收到完整的响应为止。 (我知道这不是Android的做事方式,我可以用其他语言来处理这个问题)。

这是我到目前为止所做的工作(你会看到我没有做太多的事情,事实上,这段代码的工作非常出色,直到我需要等待第一个命令完成后才发送更多命令)。我尽可能多地忽略了与这个问题无关的代码。提前致谢。

package Android.Arduino.Bluetooth;   

    import java.io.IOException;   
    import java.io.InputStream;   
    import java.io.OutputStream;   
    import java.util.Set;   
    import java.util.UUID;   
    import android.app.Activity;   
    import android.bluetooth.BluetoothAdapter;   
    import android.bluetooth.BluetoothDevice;   
    import android.bluetooth.BluetoothSocket;   
    import android.content.Intent;   
    import android.os.Bundle;   
    import android.os.Handler;   
    import android.widget.TextView;   


    public class BluetoothTest extends Activity   
    {   
     TextView myLabel;   
     BluetoothAdapter mBluetoothAdapter;   
     BluetoothSocket mmSocket;   
     BluetoothDevice mmDevice;   
     OutputStream mmOutputStream;   
     InputStream mmInputStream;   
     int counter;   
     Thread workerThread;   
     byte[] readBuffer;   
     int readBufferPosition;   
     volatile boolean stopWorker;   

     @Override   
     public void onCreate(Bundle savedInstanceState)   
     {   
      super.onCreate(savedInstanceState);   
      setContentView(R.layout.main);   

      myLabel = (TextView)findViewById(R.id.label);   

         try   
         {   
          findBT();   
          openBT();   
          sendData("enable");   
          //insert some code to wait for response before sending (or handle that in the above line, or otherwise) 
          sendData("password");   
          //insert some code to wait for response before sending (or handle that in the above line, or otherwise)   
          sendData("conf t");   
          //insert some code to wait for response before sending (or handle that in the above line, or otherwise)   
          sendData("interface eth0");   
          //insert some code to wait for response before sending (or handle that in the above line, or otherwise)   
          //etc...etc...etc...   
         }   
         catch (IOException ex) { }        

     }   

     }   

     void openBT() throws IOException   
     {   
      UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID   
      mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);     
      mmSocket.connect();   
      mmOutputStream = mmSocket.getOutputStream();   
      mmInputStream = mmSocket.getInputStream();   
      beginListenForData();   
      myLabel.append("Bluetooth Opened" + "\n");   
     }   

     void beginListenForData()   
     {   
      final Handler handler = new Handler();   
      final byte delimiter = 62; //This is the ASCII code for a > character indicating all data received   

      stopWorker = false;   
      readBufferPosition = 0;   
      readBuffer = new byte[1024];    

      workerThread = new Thread(new Runnable()   
      {   
       public void run()   
       {       

       while(!Thread.currentThread().isInterrupted() && !stopWorker)   
        {   
         try   
         {   
          int bytesAvailable = mmInputStream.available();         
          if(bytesAvailable > 0)   
          {   
           byte[] packetBytes = new byte[bytesAvailable];   
           mmInputStream.read(packetBytes);   
           for(int i=0;i<bytesAvailable;i++)   
           {   
            byte b = packetBytes[i];   
            if(b == delimiter)   
            {   
             byte[] encodedBytes = new byte[readBufferPosition];   
             System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);   
             final String data = new String(encodedBytes, "US-ASCII");   
             readBufferPosition = 0;   

             handler.post(new Runnable()   
             {   
              public void run()   
              {              
               myLabel.append(data);           
              }   
             });   
            }   
            else   
            {   
             readBuffer[readBufferPosition++] = b;   
            }   
           }   
          }   
         }   
         catch (IOException ex)   
         {   
          stopWorker = true;   
         }   
        }   

       }   
      });   

      workerThread.start();   
     }   

     void sendData(String msg0) throws IOException   
     {   
      msg0 += "\r";   
      mmOutputStream.write(msg0.getBytes());   
      myLabel.append("Data Sent" + "\n");   
     }   

    }   
+0

好吧,我在两个命令之间加入了一个暂停,所以至少这些命令有时间在远端执行。这仍然不是正确的方法,因为我不应该直接从远程设备发送命令或解释/处理输入,直到我收到“完成处理”字符“>”。 – user1324818 2012-04-12 16:36:07

+1

好的。我闯入了这条路,并使其工作。这可能不对,但我会在有机会时发布代码。它的工作原理,但我需要添加更多的错误处理! – user1324818 2012-04-13 01:46:56

+1

如果你写了一些进展或最终解决方案,我会非常感激。在此先感谢..干杯 – Ewoks 2012-07-06 12:00:38

回答

1

我意识到这是一个相当古老的问题。然而,由于我正在开发一个类似的应用程序,我用不同的方法解决了这个问题,这可能会有所帮助

而不是发送之间的延迟,你可以通过标志实现发送/响应逻辑。所以在开始时连接是initial。您发送enable并将您的标志设置为enableRequested。然后你让听众等待回应。一旦你继续发送password,将标志设置为passwordSent并再次释放该线程。

所以我建议不要在onCreate中做,而是触发一个线程从onCreate连接。这应该很好。

+0

伟大的....寻找和你的回答给了我一个提示:) – Snake 2013-09-06 02:11:14