Arduino / Processing / Android Bluetooth (Ketai) communication problem

Long time lurker, first time poster.

I have a silly race car project. I’ve designed circuitry and Arduino code to probe various things on the car, and then perform some basic actions, like shift the transmission, turn on the fan, etc. There’s also a Raspberry Pi in the system that logs the data from the Arduino, as well as some other information, like GPS data. When the car passes a track-side router, the Pi pushes data to a track-side router onto an FTP, where a Python script I wrote plots cool information. Swell. If you want to see some pictures of this lunacy, here’s a link to our Instagram: The Gunbarrel Cobras

Another feature I’ve added is the ability to have data from the car pushed over Bluetooth (HC-05 adapter on the car) to a Processing app dashboard. I first wrote this Processing program on Mac, and it works. Great! But now I want to port it to Android (using Ketai for my BT interface) for extra cool factor and portability of display.

When I send a string from the Arduino Mega (through the BT adapter) to Android, the string is nearly always incomplete or chopped up. So instead of receiving, for example, “Test String” I will receive on the Android “T” followed by “est String” a while later. I’m afraid I’m misunderstanding how things are being sent and received.

Here are minimal working examples. When the Processing code starts, connect to the BT adapter. Every few frames, send a “" to let the Arduino know it wants a string. Arduino then gets that "” and sends back just a sample string as a test.

Processing code:

// Initialize Bluetooth for Android
import android.content.Intent;
import android.os.Bundle;

import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;

KetaiBluetooth bt;

boolean btConnected = false;
String inChar;
boolean newDisplay = false;

//********************************************************************
// The following code is required to enable bluetooth at startup.
//********************************************************************
void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bt = new KetaiBluetooth(this);
}

void onActivityResult(int requestCode, int resultCode, Intent data) {
  bt.onActivityResult(requestCode, resultCode, data);
}
//********************************************************************

void setup() {

  fullScreen();
  orientation(LANDSCAPE);

  // Start Bluetooth
  bt.start();
}

void draw() {
  background(0, 0, 0);
  if (!bt.isDiscovering() && btConnected == false) {
    bt.discoverDevices();
  }
  if (bt.getDiscoveredDeviceNames().size() > 0 && btConnected == false) {
    bt.connectToDeviceByName(bt.getDiscoveredDeviceNames().get(0));
    btConnected = true;
  }

  if (btConnected && frameCount%60==0) {
    byte[] toSendData = {'*'};
    bt.broadcast(toSendData);
  }
}

void onBluetoothDataEvent(String who, byte[] data) {
  println(who);
  inChar = new String(data);
  println(inChar);
}

Arduino code:

void setup() {

  Serial.begin(115200);           // Serial port for communication to computer
  Serial2.begin(115200);          // Serial port for communicating with auxillary serial port (Bluetooth)

}

void loop() {
  while (Serial2.available() > 0) {
    int readinaux = Serial2.read();
    Serial.println(readinaux);
    Serial2.print("Test string sent.\n");
  }
}

This returns on Processing (small sample of all responses):

00:14:03:06:75:E4
Test str
00:14:03:06:75:E4
ing sent.
00:14:03:06:75:E4
Test s
00:14:03:06:75:E4
tring sent.
00:14:03:06:75:E4
Test str
00:14:03:06:75:E4
ing sent.
00:14:03:06:75:E4
Test st
00:14:03:06:75:E4
ring sent.
00:14:03:06:75:E4
Test str
00:14:03:06:75:E4
ing sent.

I’ve tried slowing down the rate of asking for the string, among other things. Any help is greatly appreciated.

For any digital archaeologists stumble upon this, here was the solution I came up with:

Pull each byte of the byte array in and compare it to a new line. If it’s not, add it to an ongoing string. If it is, do whatever I want with it. I think I was just abusing the string and byte array stuff.