Screen initialized when BT connected/disconnected and therefore resets variables. Any idea, please?

I want to measure working time of bluetooth earphones, so timers should stop when bluetooth is disconnected, keeping running time in variable. But when bluetooth state is changed, screen is initializing and therefore it reset variables. Any idea what can be wrong?

If you want to accumulate values over more than one run of an app, save those values in TinyDB continually, and reload them from TinyDB at Screen1.Initialize time.

I would like to leave app it running till earbuds will go disconnected due to a low battery. But when Bluetooth change changes, screen goes to initialize and resets counters. Why, the hell???? :slight_smile:

Don't use global variables.

Update your TinyDB values continually, and refer only to them.

OK, solved. Even little bit more complicated than expected, but it works. Thanks for a hint!

You could just record current milliseconds and milliseconds on Bluetooth disconnect, that would be more accurate and the Clock Timer would not run-down your battery.

Also, no need for Logic, just this:
If Bluetooth is Connected

Thank you for your answer, but in my case, I don´t care about phone battery. I just want to measure battery working time of BT earbuds. And BluetoothClient1.IsConnected always returns FALSE. Don´t know why.

So your code does find the BT earbuds and does connect to them, is that right? They may have a propriety connection, in which case you would need their API.

It is in fact very difficult to verify a Bluetooth disconnection, Android and Bluetooth are not that smart, depending on the version of each.

'BluetoothClient Is Connected' works with microcontrollers, but earbuds are entirely different and not all earbuds work in the exact same way. Post the make and model of your earbuds, let's see if the manufacturers specification can give some clues. We also need to know the Android version and BT version of your phone. It's possible that the phone and the earbuds are using BLE.

You should care, because the phone battery could die before the earbuds battery does.

I wonder if the BlueTooth isConnected block is testing what you want to test?

If the BlueTooth headphones are talking to a separate player app, does that really count as a connection from the point of view of a separate AI2 app?

I suspect what you want is impossible.

I have testing audio file included in AI2 app, play it thru Player element and I want to know when headphones will disconnect (due to empty battery or whichever other reason) and then stop the timer. As far as I have testing phone on charger permanently, I don´t care about phone battery... But Bluetooth.IsConnected says "FALSE".

There are no specifications available, those are new OEM models (30 types :unamused:) and I should check their running time to confirm suppliers parameters)) Thats why I am trying to develop some easy application rather than walk around and check, next, check, next...))

There are surely some specs available from the manufacturer - if only to say what type of Bluetooth is supported and certifications (WEEE and CE). I'm guessing these earbuds are from China - the Seller should be able to contact the manufacturer with your query.

I have already explained that Bluetooth.IsConnected will not necessarily work with such devices, but if the devices are modern then they could be using BLE and not BT. If they are BLE, your smartphone must be BLE too.

See my website: ProfessorCad: Tips & Tricks