blopker / superduper

iOS and Android app to manage your ebike.
MIT License
35 stars 6 forks source link

Turn on Bluetooth automatically when the app starts #23

Closed AxelFougues closed 4 months ago

AxelFougues commented 1 year ago

Since Bluetooth being on is essential to using the app and most people don't leave it on all the time, you might as well turn it on automatically when the app is started. This is natively possible on Android, not sure about Apple. This would allow for further automation: ie. scan an NFC tag to launch the app, Bluetooth is automatically turned on so the bike settings are being set with a single tap. Currently, this is only possible by using Tasker to first turn on Bluetooth and then launch the app.

Also thank you. The serious privacy issues and software issues of the proprietary app are just no acceptable. You're helping so many people out!

blopker commented 1 year ago

Hey! I took a few minutes to see if this was possible, but I can't find anything promising for either iOS or Android. For Android, it looks like apps were able to turn on Bluetooth at some point, but it no longer seems to be true. Do you have any links to documentation I could look at? This would be a better user experience. Although, I do leave my Bluetooth on at all times, so I don't know how many people do turn it off regularly.

AxelFougues commented 1 year ago

I'm confident it's possible (probably requires permission) because automation apps like tasker can toggle Bluetooth. Let me look at the docs real quick

AxelFougues commented 1 year ago

Found this (step 2): https://developer.android.com/develop/connectivity/bluetooth/setup

Looks like it will ask for permission the first time and then be able to toggle it. The tip might also be interesting to you. I'm not familiar with flutter and dart but I believe you could implement that in your main activity: https://github.com/blopker/superduper/blob/main/android/app/src/main/kotlin/io/kbl/superduper/MainActivity.kt More ressources : https://stackoverflow.com/questions/5735053/toggling-bluetooth-on-and-off

And a couple questions: I was looking through your code trying to reverse engineer and found the uuids here: https://github.com/blopker/superduper/blob/main/lib/services.dart#L21 I see the bottom ones are used to get the current state and get notifications but I can't find where the other ones are used and what for?

Also when trying to connect to the bike with a bt terminal (https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal) The notifications characteristic uuid shows up and works but not the current state one... Any idea why?

blopker commented 1 year ago

Nice, yeah, this is what I originally found: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#enable(). Sadly, as of Android 13 (API 33) it no longer works. Looks like the new preferred way is this dialog, which seems like it can be added, though not ideal.

The services file in SD was used to document the API. Only a few of those UUIDs are actually used though. To read the state it's a little weird. You need to 'set' the metric characteristic by writing [3,0], then reading. You can see the little dance here: https://github.com/blopker/superduper/blob/main/lib/repository.dart#L183

AxelFougues commented 1 year ago

I did see the [3,0] , that's interesting. Can you point me to the bit where you write a new state to the bike? I thought this was it https://github.com/blopker/superduper/blob/7ce3af72b31e862cca673d1708f6ee6e0dcfd80c/lib/bike.dart#L87 but I'm not too sure

AxelFougues commented 1 year ago

Ah I think I understand. You're writing with the by-default uuids here: https://github.com/blopker/superduper/blob/7ce3af72b31e862cca673d1708f6ee6e0dcfd80c/lib/repository.dart#L158 https://github.com/blopker/superduper/blob/7ce3af72b31e862cca673d1708f6ee6e0dcfd80c/lib/repository.dart#L159 Sorry I'm not familiar with dart at all 😂

blopker commented 1 year ago

Yep! That bike.dart method is calling: https://github.com/blopker/superduper/blob/7ce3af72b31e862cca673d1708f6ee6e0dcfd80c/lib/repository.dart#L153

Dart does take some getting used to, but I like it.

blopker commented 4 months ago

The new update I just put out now does this. Thanks!