LeandroSQ / android-ble-made-easy

An Android Library for handling Bluetooth Low Energy on Android Easy
MIT License
88 stars 29 forks source link

BLE disconnected automatically after 4-5 min. #31

Closed karansingla007 closed 1 year ago

karansingla007 commented 1 year ago

@LeandroSQ @NoelChew When I connect with the BLE device it disconnects after 4-5 mins in Android. But when I use the same device with windows then It does not happen. Something is wrong.

LeandroSQ commented 1 year ago

Hello there, thank you for your issue 🙂

Would you be able to provide mor information a possibly a sample of how you are connecting? Also, is the app on foreground? Are you using a background or foreground service?

LeandroSQ commented 1 year ago

Also, which Android device are you using?

What are you trying to connect to?

karansingla007 commented 1 year ago

@LeandroSQ Thank you for the quick reply. I am testing in Android 13. The app is in the foreground. The Android device is Google Pixel 7. I am connecting like this

ble?.scanFor(macAddress = Constants.bleDevice.macAddress, timeout = 200000)?.let {
                    onDeviceConnected(it)
                    ble?.stopScan()
                }
    private fun onDeviceConnected(connection: BluetoothConnection) {
        connection.apply {
            MainActivity.connection = connection
            onConnect = {
                setDeviceConnectionStatus(true)
                setLoaderVisibility(false)
            }
            onDisconnect = {
             setDeviceConnectionStatus(false)
            }
            setDeviceConnectionStatus(true)
            setLoaderVisibility(false)
        }
    }

And after 4-5 mins onDisconnect callBack call.

LeandroSQ commented 1 year ago

You should not need to call ble?.stopScan for scanFor.

Secondly, would you be so kind to enable verbose mode like ble?.verbose = true after instantiating it, and sharing the logs here?

karansingla007 commented 1 year ago

I already enabled the logs. and i removed ble?.stopScan

image

LeandroSQ commented 1 year ago

I don't see much there, only lost connection. Could you share these logs on a .txt file format?

EDIT: onClientConnectionState() - status=0 clientIf=6 .... This status zero means unknown cause of disconnection. Could be anything, this comes directly from the BLE logic in Android OS.

Also, I don't think this is related to your issue, but It seems you have a ValueAnimator on a BLE callback which is not on the main thread. You can fix that by using:

Handler(Looper.getMainLooper()).post(Runnable {
        // things to do on the main thread
    })
LeandroSQ commented 1 year ago

One thing I think it may help, I will enable more verbose logs on this disconnection scenario. And add it to a version, hopefully it will help us to catch possible causes that are actionable.

Also, in the meantime, I know it does not solve completely your problem, but could you experiment with connecting again after a disconnection? I was planning on adding this auto-connect feature, the library already supports device cache, so re-connecting should be faster.

karansingla007 commented 1 year ago

@LeandroSQ sounds good, but any estimate when can i test again? And i tried to connect again onDisconnect callback but i did not receive data from bluetooth device. I don't why.

See i am enabling a service when connection is successfully connected and then write some bytes to the bluetooth device -> device send me data in packets.

karansingla007 commented 1 year ago

@LeandroSQ i think i found the problem https://stackoverflow.com/questions/54898592/bluetoothgattserver-is-always-disconnecting-after-30-seconds

you are not calling this method gattServer.sendResponse.

LeandroSQ commented 1 year ago

Awesome finding!

I can put this on develop, if you are able to clone the repo and try the provided sample app to test it, then we can do a full release with this.

karansingla007 commented 1 year ago

@LeandroSQ yeah i can test this. Lemme know when its done.

karansingla007 commented 1 year ago

@LeandroSQ can you please try this ASAP because i stuck here. i hope you will understand. Thanks

LeandroSQ commented 1 year ago

First of all, after looking at our source code, we do not use onReadRequest, and not commit transactions to GATT. I'm afraid this won't be helping :/

Not sure if you've seen this stackoverflow question before, and even a google issue, but this is actually a problem on Android on not showing the actual reason on why you were disconnected. It seems we can look at the Bluetooth logs to see what is going on on your case, could you try the following and share the logs here? How to get the Bluetooth Host Controller Interface logs from a modern Android phone

LeandroSQ commented 1 year ago

Also, FYI v1.8.2 is out, which has more logging. Hopefully can help to catch your issue.

karansingla007 commented 1 year ago

I updated the version and i am getting these logs when bluetooth device disconnected.

D/BluetoothConnection: onCharacteristicChanged: android.bluetooth.BluetoothGattCharacteristic@438fa0b
D/BluetoothConnection: onCharacteristicChanged: android.bluetooth.BluetoothGattCharacteristic@438fa0b
D/BluetoothConnection: onCharacteristicChanged: android.bluetooth.BluetoothGattCharacteristic@438fa0b
D/BluetoothConnection: onCharacteristicChanged: android.bluetooth.BluetoothGattCharacteristic@438fa0b

D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=10 device=00:18:DA:31:89:A6
D/BluetoothConnection: onConnectionStateChange: status 0 Code: -1 - Status.Unknown - Unknown state 0 Code: 0 - State.Disconnected - Disconnected
D/BluetoothConnection: Lost connection with 00:18:DA:31:89:A6 STATUS: Code: -1 - Status.Unknown - Unknown
W/BluetoothGatt: Unhandled exception in callback
    android.util.AndroidRuntimeException: Animators may only be run on Looper threads
        at android.animation.ValueAnimator.cancel(ValueAnimator.java:1195)
        at android.view.ViewPropertyAnimator.animatePropertyBy(ViewPropertyAnimator.java:960)
        at android.view.ViewPropertyAnimator.animateProperty(ViewPropertyAnimator.java:916)
        at android.view.ViewPropertyAnimator.rotation(ViewPropertyAnimator.java:526)
        at com.aavaa.fragments.SingleDeviceFragment$onDeviceConnected$1$2.invoke(SingleDeviceFragment.kt:435)
        at com.aavaa.fragments.SingleDeviceFragment$onDeviceConnected$1$2.invoke(SingleDeviceFragment.kt:432)
        at quevedo.soares.leandro.blemadeeasy.BluetoothConnection$setupGattCallback$1.onConnectionStateChange(BluetoothConnection.kt:173)
        at android.bluetooth.BluetoothGatt$1$4.run(BluetoothGatt.java:299)
        at android.bluetooth.BluetoothGatt.runOrQueueCallback(BluetoothGatt.java:864)
        at android.bluetooth.BluetoothGatt.-$$Nest$mrunOrQueueCallback(Unknown Source:0)
        at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:294)
        at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:128)
        at android.os.Binder.execTransactInternal(Binder.java:1285)
        at android.os.Binder.execTransact(Binder.java:1244)
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=10 device=00:18:DA:31:89:A6
D/BluetoothConnection: onConnectionStateChange: status 0 Code: -1 - Status.Unknown - Unknown state 2 Code: 2 - State.Connected - Connected
D/BluetoothConnection: Device 00:18:DA:31:89:A6 connected!
D/BluetoothGatt: requestConnectionPriority() - params: 0
D/BluetoothGatt: discoverServices() - device: 00:18:DA:31:89:A6
D/BluetoothGatt: onConnectionUpdated() - Device=00:18:DA:31:89:A6 interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt: onSearchComplete() = Device=00:18:DA:31:89:A6 Status=0
D/BluetoothConnection: onServicesDiscovered: 4 services found!
D/BluetoothGatt: onConnectionUpdated() - Device=00:18:DA:31:89:A6 interval=36 latency=0 timeout=500 status=0
D/BluetoothGatt: onConnectionUpdated() - Device=00:18:DA:31:89:A6 interval=9 latency=0 timeout=400 status=0
LeandroSQ commented 1 year ago

One question on this last stack trace, did you reconnect before this?

karansingla007 commented 1 year ago

No, these logs when i connect very first time and it disconnected.

LeandroSQ commented 1 year ago

Do you happen to have timestamps on this? How much time before the disconnect?

karansingla007 commented 1 year ago

Sometime after 5 min or around 2 min.

LeandroSQ commented 1 year ago

I also found this: https://stackoverflow.com/questions/60666682/ble-connectivity-issue-when-clientif-value-is-greater-than-7

If you try restarting and turning Bluetooth ON/OFF on your device, and clientIF resets to 0. The issue persists?

karansingla007 commented 1 year ago

yes, the issue is persists in every condition.

LeandroSQ commented 1 year ago

Were you able to get the HCI logs? They will probably have more information specific to your device, which could help us to understand what is going on for you.

LeandroSQ commented 1 year ago

Closing due to inactivity, if you would like this to be re-opened, please let me know.