Search code examples
androidbluetoothdelaypic

Add delay between data transferred by Bluetooth


I am trying to transfer some simple data between my Android smartphone and a PIC Micro-controller. Everything is working sending with this code:

try { 
    mBluetoothOut.write(new byte[] { 
            StartByte,                  // Start byte
            //(byte)(x1Distance & 0xFF),            // x1 -> LSB
            //(byte)((x1Distance >> 8) & 0xFF), // x1 -> MSB
            (byte)(y1Distance & 0xFF),          // y1 -> LSB
            (byte)((y1Distance >> 8) & 0xFF),   // y1 -> MSB
            (pad1.isYNegative()) ? (byte)1 : (byte)0,
            //(byte)(x2Distance & 0xFF),            // x2 -> LSB
            //(byte)((x2Distance >> 8) & 0xFF), // x2 -> MSB
            (byte)(y2Distance & 0xFF),          // y2 -> LSB
            (byte)((y2Distance >> 8) & 0xFF),   // y2 -> MSB
            (pad2.isYNegative()) ? (byte)1 : (byte)0,
            (byte)(servo1Angle & 0xFF),     // servo 1 -> LSB
            (byte)(servo2Angle & 0xFF)          // servo 2 -> LSB
    });
 } catch (IOException e) { 
    e.printStackTrace(); 
 }

The problem is that my Android device is sending the data too fast and my Microcontroller mess up. Is there any way to put a delay of about 2-3mS between each byte being send ? Or I have to send byte by byte and use a postExecute() with Handlers or something like that ?

Thank you


Solution

  • Assuming that you are using a standard Bluetooth profile like SPP to send the data, On the receiving side you should be able to receive at the rate you want (one byte at a time and next after detay) The bluetooth internally buffer or apply flow control to stop the sender if it is too fast. I dont see how the data can be messed up if using a proper Bluetooth stack