2013-03-21 90 views
2

我遇到一個奇怪的問題。我已經編寫了一個應用程序,該應用程序將與arduino建立藍牙SPP鏈接。 Arduino上的藍牙設備配置爲9600波特。我可以接收來自arduino的數據,但似乎我收到一些值爲0或高峯的小故障。這是相當煩人,因爲我確實需要爲上述圖形部分的精確值,我知道的Arduino送好數據,因爲我登錄了它在一個文件發送。藍牙SPP(串行)glitchs(安卓)

我在尋找解決或理解爲什麼這種情況正在發生,而在創建的平均或類似的,這將使一個「補丁」的東西。

感謝您的幫助。

這裏是一個可以解釋我的問題的圖片,Arduino的數據範圍是大約101至103:

Screenshot http://s9.postimage.org/n1td3bb4f/Screenshot_2013_03_20_22_30_15_1.png

而且這裏是我創建了一個連接和接收數據的代碼:

private class ConnectedThread extends Thread { 
    private final DataInputStream mmInStream; 
    private final DataOutputStream mmOutStream; 

    public ConnectedThread(BluetoothSocket socket) { 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

     // Get the input and output streams, using temp objects because 
     // member streams are final 
     try { 
      tmpIn = socket.getInputStream(); 
      tmpOut = socket.getOutputStream(); 
     } catch (IOException e) { } 

     mmInStream = new DataInputStream(tmpIn); 
     mmOutStream = new DataOutputStream(tmpOut); 
    } 

    public void run() { 
     byte[] buffer = new byte[1024]; // buffer store for the stream 
     int bytes; // bytes returned from read() 

     // Keep listening to the InputStream until an exception occurs 
     while (true) { 
      try { 
       // Read from the InputStream 
       bytes = mmInStream.read(buffer);  // Get number of bytes and message in "buffer" 
       hBluetooth.obtainMessage(RECEIVE_MESSAGE, bytes, -1, buffer).sendToTarget();  // Send to message queue Handler 
      } catch (IOException e) { 
       break; 
      } 
     } 
    } 

private void connectDevice() { 


    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); 

    try { 
     btSocket = createBluetoothSocket(device); 
    } catch (IOException e) { 
     errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + "."); 
    } 


    mBluetoothAdapter.cancelDiscovery(); 


    // Establish the connection. This will block until it connects. 
    Log.d(TAG, "...Connecting..."); 
    try { 
     btSocket.connect(); 
     Log.d(TAG, "....Connection ok..."); 
     // Create a data stream so we can talk to server. 
     Log.d(TAG, "...Create Socket..."); 

     mConnectedThread = new ConnectedThread(btSocket); 
     mConnectedThread.start(); 
     mActionBar.setSubtitle("Connecté"); 

     //If fail, we disconnect or display an error warning regarding the situation 
    } catch (IOException e) { 
     try { 
     btSocket.close(); 
     mActionBar.setSubtitle("Deconnecté"); 
     } catch (IOException e2) { 
     errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + "."); 
     } 
    } 

    return; 
    } 

最後的處理程序:

hBluetooth = new Handler() { 
    public void handleMessage(android.os.Message msg) { 
     switch (msg.what) { 
     case RECEIVE_MESSAGE:             // If we receive a message 
      byte[] readBuf = (byte[]) msg.obj; 
      String stringIncome = new String(readBuf, 0, msg.arg1);    // Create string from byte array 
      stringBuilder.append(stringIncome);            
      int endOfLineIndex = stringBuilder.indexOf("\r\n");     // Determine the end-of-line 
      if (endOfLineIndex > 0) {           // If we are at the end-of-line we parsed all the data that was sent 
       rmsgBluetooth = stringBuilder.substring(0, endOfLineIndex);  // The string is extracted in a string object rmsgBluetooth 
       stringBuilder.delete(0, stringBuilder.length());     


       if(btSocket != null && btSocket.isConnected()){     


       //Here we send the value of the string to a txtbox 
       txtArduino.setText("Arduino: " + rmsgBluetooth); 




       if(rmsgBluetooth.matches("-?\\d+(\\.\\d+)?")) {     
        try{ 

        sensorReading = Float.parseFloat(rmsgBluetooth); 
        }catch(NumberFormatException e){ 


        } 
       } 

回答

0

我認爲你的錯誤很可能出現在字符串解析時。嘗試仔細調試線

  if(rmsgBluetooth.matches("-?\\d+(\\.\\d+)?")) {     
       try{ 

       sensorReading = Float.parseFloat(rmsgBluetooth); 
       }catch(NumberFormatException e){ 


       } 
      } 
+0

感謝您的答覆!但即使rmsgBluetooth字符串也有這個問題...所以基本上這個問題在別處。也許它是在字符串生成器區域,當我試圖解析所有的字節來創建一個字符串,但我找不到錯誤。 – Mathieu660 2013-03-21 20:02:52