Peripheral shutdown causing unexpected behavior

Using the Android 4.3 / Samsung BLE 2.0 SDK, it is observed that when the peripheral device is turned off, the SDK will accept onConnectionStateChange (DEVICE_DISCONNECTED) either immediately or after a delay of ~ 20 s. In my experience, this depends on the peripheral implementation, some of them will try to report that they are disabled, and some simply do not, so the SDK should wait ~ 20 seconds for a timeout.

To remove this behavior, I tried using a timer to check if I can read a specific symptom. If the timeout is disconnected, I will call disconnect (Android 4.3) / cancelConnection (Samsung) to end the connection. The call itself is successful, and the callback onConnectionStateChange returns the GATT_SUCCESS status. Then I turned on the peripheral device and immediately connected to it, discovered the services and ran into a problem when I tried to read / write / notify about any notification. Using LightBlue in iOS, I can confirm that the peripheral device is not connected.

After exactly 20 seconds from turning off the peripheral device, I get a DEVICE_DISCONNECTED callback. After that, I reconnect and everything works fine.

There are two questions: 1. Do we need to connect to peripherals within a 20 second delay? 2. Is there a way to get notified when the peripheral is turned off?

Thanks in advance.

+7
android android bluetooth bluetooth-lowenergy
source share
1 answer
  • Should we connect to peripherals during a delay of 20 seconds? Not. It seems that due to the OS taking some actions on the phone to manage DEVICE_CONNECTED or DEVICE_DISCONNECTED , this may be due to several reasons, such as device architecture, security reasons, or the callback itself, is delayed to save energy

  • Is there a way to get notified when a peripheral device is turned off? No, I do not think so. The event received for any action is broadcast in such a way that a signal is waiting for it from the device, but it does not receive it, the only thing you can do here is send it through some other broadcast (or HTTP request) from the other end of the device to his achievements

+1
source share

All Articles