2

I'm running into a curious problem. I have programmed an app that will establish a Bluetooth SPP link with an arduino. The bluetooth device on the Arduino is configured at 9600 bauds. I can receive data from the arduino, but it seem I receive some glitch with the value 0 or high peaks. This is pretty annoying because I do need precise value for the graphing part and I know the arduino send good data because I log what it send in a file.

I'm looking to fix or understand why this is happening rather that creating an average or something similar that will make a "patch".

Thanks for your help.

Here is a picture that can explain my problem, the arduino data range is around 101 to 103 :

Screenshot

And here is the code where I create a connection and receive data :

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;
    }  

and finally the handler :

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){


                    }
                }   
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Mathieu660
  • 61
  • 1
  • 9

1 Answers1

0

I think your error most likely comes in when the string is parsed. Try carefully debugging the lines

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

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


                }
            }
SmallJoeMan
  • 353
  • 4
  • 9
  • Thanks for your reply! But even the rmsgBluetooth string have this problem... So basically the problem is elsewhere. Maybe it's in the string builder area when I'm trying to parse all the bytes to make a string but I can't find the bug. – Mathieu660 Mar 21 '13 at 20:02