How to delay getOutputStream().write() in Android Studio? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to delay getOutputStream().write() in Android Studio? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

Answer

You send a byte array to OutputStream but you can also send bytes one at a time – simply loop through your byte array, and after sending each byte, delay for a bit.

I’ve used an AsyncTask for this but since its deprecated you might want to use Threads or something similar to not lock your UI thread.

Once you have an AsyncTask framework or Thread in place, your working code (which is in doInBackground or your worker function) should be something like:

for (int i=0; i<bytes.length();i++)
{
    btSocket.getOutputStream().write(bytes[i]);
    Thread.sleep(2); //2 ms delay
}

There’s a few exceptions that I haven’t handled but you can put it in a try-catch block for that if you want.

An example template for AsyncTask (which, again, is your choice) is here: https://stackoverflow.com/a/9671602/3550337. I only bring it up because that’s what I’ve used and it worked for me.

We are here to answer your question about How to delay getOutputStream().write() in Android Studio? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji