ponewheel / android-ponewheel

pOneWheel Android app
MIT License
71 stars 25 forks source link

Identify + fix BLE re-connection issue #63

Closed kwatkins closed 6 years ago

kwatkins commented 6 years ago

Whenever there is a disconnect to OW device that isn't done by the user - for example, turning off the OW without hitting the "disconnect" button, the app has problems trying to reconnect. The workaround could be either fully closing and restarting the app and/or turning on and off bluetooth in the device settings.

I'm not sure what is going on, logs aren't showing much, so adding verbose logs in the BluetoothImpl may shed light. If there is a solid open sourced + maintained library that deals with the Android BLE buggy af stack I'm all about moving to it...

ebabel commented 6 years ago

I have a friend working on one and has investigated all of them he can find. He says there is sadly no silver bullet.

kwatkins commented 6 years ago

Sad panda.

On Mon, Mar 12, 2018, 10:58 AM Erik Babel notifications@github.com wrote:

I have a friend working on one and has investigated all of them he can find. He says there is sadly no silver bullet.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ponewheel/android-ponewheel/issues/63#issuecomment-372405405, or mute the thread https://github.com/notifications/unsubscribe-auth/AA0LrePDpLfin_VkAFMStmL1xRTsI_e_ks5tdrczgaJpZM4SnGZ1 .

twyatt commented 6 years ago

As @ebabel mention, It isn't easy to get bluetooth right on Android, but this might help...

In your BluetoothUtilImpl:

https://github.com/ponewheel/android-ponewheel/blob/bbc9cd217252aeb8ef5b313ce923b3934c64b180/app/src/main/java/net/kwatts/powtools/util/BluetoothUtilImpl.java#L328-L345

Upon disconnect you reconnect using BluetoothDevice.connectGatt:

https://github.com/ponewheel/android-ponewheel/blob/bbc9cd217252aeb8ef5b313ce923b3934c64b180/app/src/main/java/net/kwatts/powtools/util/BluetoothUtilImpl.java#L315-L318

When using BluetoothDevice.connectGatt you should clean up the previous connection using BluetoothGatt.close, or instead of BluetoothDevice.connectGatt use BluetoothGatt.connect to reconnect.

Basically, you're creating a new connection every time the connection is dropped without closing the previous, and Android BLE has a very limited number of connections that are allowed at a time.

kwatkins commented 6 years ago

Thanks Travis, I'll give this a try!

On Tue, Mar 13, 2018, 12:09 AM Travis Wyatt notifications@github.com wrote:

As @ebabel https://github.com/ebabel mention, It isn't easy to get bluetooth right on Android, but this might help...

In your BluetoothUtilImpl:

https://github.com/ponewheel/android-ponewheel/blob/bbc9cd217252aeb8ef5b313ce923b3934c64b180/app/src/main/java/net/kwatts/powtools/util/BluetoothUtilImpl.java#L328-L345

Upon disconnect you reconnect using BluetoothDevice.connectGatt:

https://github.com/ponewheel/android-ponewheel/blob/bbc9cd217252aeb8ef5b313ce923b3934c64b180/app/src/main/java/net/kwatts/powtools/util/BluetoothUtilImpl.java#L315-L318

When using BluetoothDevice.connectGatt https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context,%20boolean,%20android.bluetooth.BluetoothGattCallback) you should clean up the previous connection using BluetoothGatt.close https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#close(), or use BluetoothGatt.connect https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#connect() to reconnect.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ponewheel/android-ponewheel/issues/63#issuecomment-372567768, or mute the thread https://github.com/notifications/unsubscribe-auth/AA0LrQNOdf2fUJ70E7qnvQkiOlLw4fxWks5td3CrgaJpZM4SnGZ1 .

kwatkins commented 6 years ago

@Travis that did the trick. Now onto the next bug after fixing that one, the board reconnects but the UI thread doesn't. That should be easy compared to this one, big thanks again for spotting it.

On Tue, Mar 13, 2018, 8:44 AM Kevin Watkins kwatkins@gmail.com wrote:

Thanks Travis, I'll give this a try!

On Tue, Mar 13, 2018, 12:09 AM Travis Wyatt notifications@github.com wrote:

As @ebabel https://github.com/ebabel mention, It isn't easy to get bluetooth right on Android, but this might help...

In your BluetoothUtilImpl:

https://github.com/ponewheel/android-ponewheel/blob/bbc9cd217252aeb8ef5b313ce923b3934c64b180/app/src/main/java/net/kwatts/powtools/util/BluetoothUtilImpl.java#L328-L345

Upon disconnect you reconnect using BluetoothDevice.connectGatt:

https://github.com/ponewheel/android-ponewheel/blob/bbc9cd217252aeb8ef5b313ce923b3934c64b180/app/src/main/java/net/kwatts/powtools/util/BluetoothUtilImpl.java#L315-L318

When using BluetoothDevice.connectGatt https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context,%20boolean,%20android.bluetooth.BluetoothGattCallback) you should clean up the previous connection using BluetoothGatt.close https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#close(), or use BluetoothGatt.connect https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#connect() to reconnect.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ponewheel/android-ponewheel/issues/63#issuecomment-372567768, or mute the thread https://github.com/notifications/unsubscribe-auth/AA0LrQNOdf2fUJ70E7qnvQkiOlLw4fxWks5td3CrgaJpZM4SnGZ1 .

kwatkins commented 6 years ago

This is fixed, thanks again. This actually made the last release the most solid yet,