after this problem Bluetooth connection in Android > 4.1.2 that I fixed by forcing connection by repeatedly try to connect with a while iteration, now I'm having an other issue with Android/Arduino communication. My HW is Nexus 7 MY2012 with Android 4.3, Arduino UNO R3 and a BT module RN42.
From Android I'm sending an array of bytes to Arduino got from a string. At the beginning, with Baudrate 115200 and no parity, only the first byte arrives correct and the rest was apparently mismatched (mostly in a repetitive manner). After setting parity EVEN in the RN42 module, I see that at least first 3 bytes arrive correctly, and the rest is messed up. Here below the core part of comunication from Android (initialization of BT basically follows the SDK example). It is located inside a class estended from AsyncTask used to manage connection work:
public void write(String message) {
Log.d(TAG, "...Data to send: " + message + "...");
byte[] msgBuffer = message.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
mHardwareToServiceHdlr.obtainMessage(MSG_ERR_BT_WRITE).sendToTarget();
Log.d(TAG, "...Error data send: " + e.getMessage() + "...");
}
}
and Arduino sketch
#include <SoftwareSerial.h>
#include <Streaming.h>
const int bluetoothTx = 2;
const int bluetoothRx = 3;
byte incomingByte;
String incomingString;
SoftwareSerial bluetooth(bluetoothTx,bluetoothRx);
void setup() {
Serial.begin(115200);
bluetooth.begin(115200);
bluetooth.print("$$$");
delay(100);
bluetooth.println("U,115K,E");
bluetooth.begin(115200);
delay(100);
}
void loop(){
if (bluetooth.available() > 0) { // if the data came
incomingByte = bluetooth.read(); // read byte
Serial.println((char)incomingByte);
}
}
If I send to Arduino a string such as "hello horld" this is what I get in serial monitor in a serie of transmissions:
hel,o wo2ld
hel<o wo2ld
hel,o7orld
hel,o wo2ld
hel,o wo2ld
hel<o7or6d
hel,o wo2ld
hel,o wo2ld
hel,o wo2ld
hel<o wo2ld
hel,o7orld
hel<o wo2ld
This is just example, result depends also on how much often I send the string to Arduino. The most of the times, the fourth and the nineth byte (but not always in the same manner and not always only them) are mismatched.
Transmitting data from Arduino to Android seems to work fine with any particular trouble.
Any help would be greatly apreciated, this thing is making me crazy as far as I need to transmit data longer than 3 bytes. Thanks