In addition to what @Ghica and @Taifun have already siad, the way that I've used in my app's is to make a handshake between the client and the server. In other words the connected device shall send periodically (i.e. once each minute) a "live" character that the app shall read. If within two minutes the app does not receive the "live", it is intended as a BT disconnection and the "Disconnection warning" is issued. To actuate the reading every two minutes the app shall set a specific clock.
In detail:
- The device sends every 1 minute a '$' (with a dedicated sending not the same of the data frame).
- A fast clock (clock1) in the app receives anything from the BT. If the character received is a '$' it sets a flag: BTlive = TRUE.
- A slow clock (clock2) in the app (each two minutes) checks the BTlive.
if BTlive == FALSE => declares BT failure
if BTlive == TRUE => declares the BT is live, and sets the flag to FALSE, so the next tick if no '$' has been received in the meanwhile, on the next clock2 tick it declares BT disconnection.
Hoping it helps you.