I'm using a Bluetooth socket to send information from my phone to an Arduino. The problem is that btSocket.getOutputStream().write(bytes);
is sending information too fast and the Arduino can't catch up with the emptying up and overflows. The code running on my Arduino slows down the Arduino's ability to receive the info from the phone and the receiving Arduino buffer gets overflowed (the incoming data gets bugged because the buffer is emptied much slower than it is filled). So a solution would be to slow the rate at which the phone sends the info.
This is the function I use to send info from the phone to the Arduino:
public void send_string_to_lim(String s) {
if (btSocket != null) {
try {
byte[] bytes = s.getBytes();
btSocket.getOutputStream().write(bytes);
} catch (IOException e) {
quick_toast("Error: " + e.toString());
}
}
}
And this is how the btSocket is created: (not sure if it's needed for the question)
if (btSocket == null || !isBtConnected) {
myBluetooth = BluetoothAdapter.getDefaultAdapter(); //get the mobile bluetooth device
BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address); //connects to the device's address and checks if it's available
btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID); //create a RFCOMM (SPP) connection
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect(); //start connection
}
How can I slow the btSocket.getOutputStream().write(bytes);
so it sends information slower? Add some type of delay so the Arduino doesn't overflow.