I have a Bluetooth device which is connected to Android smartphone over Bluetooth SPP profile.
Once connection is successful, device opens the rfcomm device
handle = open("/dev/rfcomm0", O_RDWR | O_NOCTTY | O_SYNC);
After successful connection smartphone sends data to device. It sends 3 consecutive messages of few bytes to device. This is verified after looking at snoop log of Bluetooth device.
However the blocking read at Bluetooth device side is never able to read initial 2 messages, after that it reads all other messages.
Same experiment is tried by writing a standalone script which starts reading data on rfcomm device as soon as SPP connection is successful, but here also outcome is same.
Please guide what might go wrong.
More or less it was a timing issue.
The app on device requests Bluetooth stack to create SPP connection with Android phone. On successful SPP connection /dev/rfcomm0 device is created by Bluetooth stack.
As soon as rfcomm channel is created, Android phone sends data to device but by this time application on device was not updated with the creation of rfcomm device. As there are no readers for rfcomm, Bluetooth stack flushes all data it received from Android phone.
Performed this experiment multiple times, once it worked.