weliem / blessed-android

BLESSED, a Bluetooth Low Energy (BLE) library for Android
MIT License
557 stars 120 forks source link

Android 11 BLE Connection Disconnected Frequently #77

Closed Sasikumars41 closed 3 years ago

Sasikumars41 commented 3 years ago

In Mi devices with android 11 BLE connection not stable. It disconnects frequently. I'm writing characteristics every 10 seconds. I got this error - Connection Time out, Error code 8, Error code 61.

weliem commented 3 years ago

The connection timeout on Android 11 is 5 seconds in most cases. So if you wait 10 seconds it will indeed disconnect...

Sasikumars41 commented 3 years ago

Within 5 seconds it's connected. While reading or writing a service descriptor it disconnects.

weliem commented 3 years ago

A connection timeout means that there has been no BLE communication for some time, usually 5 seconds, and hence the peripheral disconnects.

So this is 'normal behaviour'....unless your peripheral has been designed to keep the connection open for a long time

Sasikumars41 commented 3 years ago

image_2021_02_25T05_50_05_000Z Check My log file.

Sasikumars41 commented 3 years ago

Without notification state change, my device got disconnected.

weliem commented 3 years ago

You are doing very strange things. Why on earth, are you calling setNotify continuously? And the you keep writing the same value....

Normally you only call setNotify once. And then you do nothing for 5 seconds so the connection times out....

Sasikumars41 commented 3 years ago

So how to prevent the connection from closing. I'm using set notify to do some actions to prevent connection closing. But I cannot achieve.

weliem commented 3 years ago

Normally with BLE you don't keep connections open longer than needed. The normal flow is to connect, do whatever you need to do, and disconnect as soon as possible.

What peripheral are you using and why do you want to keep the connection open? It seems your peripheral isn't designed to keep connections open as it uses a 5 second connection timeout.

Sasikumars41 commented 3 years ago

It's not disconnected on android 10 and below devices. The problem is on android 11. It's a temperature sensor.

weliem commented 3 years ago

If it is a temperature sensor I would expect it to send a value every second or so. If it doesn't do that you could read the temperature value every second. That way the connection would remain.

Sasikumars41 commented 3 years ago

My devices use 49535343-8841-43f4-a8d4-ecbe34729bb3 to perform data exchanges. There are 4 requests that use the same UUID.

weliem commented 3 years ago

That is a proprietary characteristic, so you have to consult the documentation of the manufacturer on how to properly use the peripheral to get the temperature.

Sasikumars41 commented 3 years ago

Refer to this link -- https://microchipdeveloper.com/wireless:ble-mchp-transparent-uart-service

weliem commented 3 years ago

That doesn't help much since it is just a transparent uart service. You still need to know what commands you can give to read the temperature.

Sasikumars41 commented 3 years ago

I'm trying to write a byte to service(49535343-8841-43f4-a8d4-ecbe34729bb3) within 5 seconds to prevent connection close. But not able to achieve this.

weliem commented 3 years ago

I suggest you look into the Android class called Handler. That is what you should use to do something periodically.

Closing this issue since it is not related the Blessed library