简体   繁体   中英

Arduino - Android Bluetooth connecting getting 1st character wrong sometimes

i'm design an app that gets values from an Arduino via bluetooth connection and it can get the data but some times it gets the data but with the 1st digit wrong. Like it gets 840,564,0,0,0,0 instead of 540,564,0,0,0,0.

this only happens in my app. Via USB it works perfect.

here is the code for the arduino.

void setup() {

  // initialize serial communication at 9600 bits per second:
  Serial.begin(38400);
  pinMode(7, INPUT);
  pinMode(6, INPUT);
  pinMode(5, INPUT);
  pinMode(4, INPUT);
}

void loop() {

  // read the input on analog pin 1:
  int pot1 = analogRead(A1);
  // read the input on analog pin 0:
  int pot2 = analogRead(A0);

  int switch1 = digitalRead(7);
  int switch2 = digitalRead(6);
  int switch3 = digitalRead(5);
  int switch4 = digitalRead(4);

  // print out the value you read:

  Serial.print(pot1);
  Serial.print(",");
  Serial.print(pot2);
  Serial.print(",");
  Serial.print(switch1);
  Serial.print(",");
  Serial.print(switch2);
  Serial.print(",");
  Serial.print(switch3);
  Serial.print(",");
  Serial.println(switch4);

  delay(100);        // delay in between reads for stability
}

and here is some of the code for my app

 h = new Handler() {
                public void handleMessage(android.os.Message msg) {
                    switch (msg.what) {
                        case RECIEVE_MESSAGE:                                                   // if receive massage
                            byte[] readBuf = (byte[]) msg.obj;
                            String strIncom = new String(readBuf, 0, msg.arg1);                 // create string from bytes array
                            sb.append(strIncom);                                                // append string
                            int endOfLineIndex = sb.indexOf("\r\n");                            // determine the end-of-line
                            if (endOfLineIndex > 0) {                                            // if end-of-line,
                                String sbprint = sb.substring(0, endOfLineIndex);               // extract string
                                sb.delete(0, sb.length());                                      // and clear
                                           // update TextView
                                dataFromArd=sbprint;
                                String segments[]=dataFromArd.split(",");

                                //angle1= -90 + (Integer.getInteger(segments[0]) - -333) * (90 - -90) / (333 - -333);
                                if (segments[0].length()>2 && segments[0].length()<4) {
                                    int segmentInt1= Integer.parseInt(segments[0]);
                                    int segmentInt2= Integer.parseInt(segments[1]);
                                    if (segmentInt1 > 180 || segmentInt1 < 900) {
                                        //angle1 = Integer.parseInt(segments[0]) - 529;
                                        //angle1= -90 + (Integer.parseInt(segments[0]) - (515-333)) * (90 - -90) / (515+333) - (515-333);
                                        int data1= (int) (0.27 * segmentInt1 - 139);
                                        int data2=(int) (0.27 * segmentInt2 - 151);

                                        if (data1!=angle1 && Math.abs(data1-angle1)<10){
                                            angle1 = data1;
                                            speedView.speedTo(angle1,5);
                                            Log.d("int", segments[0] + "-" + Math.abs(data1-angle1) + "-" + angle1 + " " + segments[1] + "-" + Math.abs(data2-angle2) + "-" + angle2);
                                        }
                                        if (data2!=angle2 && Math.abs(data2-angle2)<25){
                                            speedView2.speedTo(angle2,5);
                                            angle2 = data2;
                                            Log.d("int2", segments[0] + "-" + Math.abs(data1-angle1) + "-" + angle1 + " " + segments[1] + "-" + Math.abs(data2-angle2) + "-" + angle2);
                                        }
                                        //Log.d("int", Math.abs(data1-angle1) + "-" + angle1 + " " + Math.abs(data2-angle2) + "-" + angle2);
                                        teste.setText("Data from Arduino: " + segments[0] + segments[1]);
                                    }
                                }


                            }
                            //Log.d(TAG, "...String:"+ sb.toString() +  "Byte:" + msg.arg1 + "...");
                            break;
                    }
                };
            };

private class ConnectedThread extends Thread {
        private final InputStream mmInStream;

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


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

            mmInStream = tmpIn;

        }

        public void run() {
            byte[] buffer = new byte[256];  // 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"
                    h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     // Send to message queue Handler
                } catch (IOException e) {
                    break;
                }
            }
        }

    }

It seems that the problem was excess usage of memory and that made the app skip frames and not getting the correct data.

I solved it by adding a dummy value for the first segment of data as a String and after filtering out the corrupt data (if the 1st segment != to String don't get data).

After that I made a lot of performance improvements that fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM