Wait for Bluetooth to connect?

Share and discuss your flows and ideas with other users.

Moderator: Martin

Post Reply
mrHS
Posts: 4
Joined: 07 Oct 2018 05:11

Wait for Bluetooth to connect?

Post by mrHS » 08 Oct 2018 09:29

Hi,

I'm having problems creating a flow which does the following:

In my car I have a wireless charger. Now, I want a flow that recognizes the wireless charging, then enables Bluetooth, then waits something like 120sec for my car to connect and then - if connected to car starts Google Maps, if not, disables Bluetooth again.

The trigger is clear and I also found the necessary Bluetooth Action, but how can I realize that Automagic waits like 2 minutes and then checks if car is connected via Bluetooth and then continues?

Thank you very much

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Wait for Bluetooth to connect?

Post by Desmanto » 08 Oct 2018 10:25

Why don't just use action Connect/disconnect bluetooth devices? Then check if the bluetooth is connected. If not, use sleep 10 seconds and loop back to try Connect again. Use execution count to limit it to 6-10 times.

Your flow probably will be similar to this one : viewtopic.php?f=5&t=7314
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Wibbly
Posts: 418
Joined: 17 Mar 2014 09:02

Re: Wait for Bluetooth to connect?

Post by Wibbly » 08 Oct 2018 12:00

mrHS wrote:Hi,
I'm having problems creating a flow which does the following:

In my car I have a wireless charger. Now, I want a flow that recognizes the wireless charging, then enables Bluetooth, then waits something like 120sec for my car to connect and then - if connected to car starts Google Maps, if not, disables Bluetooth again.
As Desmanto says, or, literally:

- recognizes the wireless charging,
http://automagic4android.com/en/help/co ... _connected
- then enables Bluetooth,
http://automagic4android.com/en/help/co ... ooth_state
- then waits something like 120sec for my car to connect and then
http://automagic4android.com/en/help/co ... tion_sleep
- if connected to car starts Google Maps, if not, disables Bluetooth again.
http://automagic4android.com/en/help/co ... _connected
http://automagic4android.com/en/help/co ... launch_app
http://automagic4android.com/en/help/co ... ooth_state

mrHS
Posts: 4
Joined: 07 Oct 2018 05:11

Re: Wait for Bluetooth to connect?

Post by mrHS » 11 Oct 2018 10:22

Thank you both - I created such a flow and in general it seems to work.
However, on every second launch of the flow, the bluetooth connection won't work and I don't know why. This is the log:
11.10.2018 11:38:01.798 [Autofahrt-Start] Start executing action 'Bluetooth Gerät verbinden/trennen: Verbinden Headset Audi MMI'
11.10.2018 11:38:01.925 [Autofahrt-Start] Action 'Bluetooth Gerät verbinden/trennen: Verbinden Headset Audi MMI' Could not connect the bluetooth device
11.10.2018 11:38:01.925 [Autofahrt-Start] End executing action 'Bluetooth Gerät verbinden/trennen: Verbinden Headset Audi MMI' and exception Could not connect the bluetooth device
11.10.2018 11:38:01.925 [Autofahrt-Start] Flow ends execution due to errors
11.10.2018 11:38:01.926 [Autofahrt-Start] Error:
ch.gridvision.ppam.androidautomagiclib.util.m: Could not connect the bluetooth device
at ch.gridvision.ppam.androidautomagic.model.a.p$1$1.run(SourceFile:184)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7002)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

11.10.2018 11:38:01.932 [Autofahrt-Start] Flow ended.
The flow ends and won't start Google Maps...

Any ideas what the problem could be?

Thank you!

Wibbly
Posts: 418
Joined: 17 Mar 2014 09:02

Re: Wait for Bluetooth to connect?

Post by Wibbly » 11 Oct 2018 11:03

Not sure. Maybe the MMI simply won't accept the connection request sometimes. I've not found Audi's MMI Bluetooth implementation (with my 2015 vintage MMI in an A3) all that great in general.

But I don't use that Action. In my case I take a different approach to this which may or may not work for you. Several flows are used to do the following:

Turn Bluetooth off whenever the display goes off or no Bluetooth devices are connected and then things are still the same after 5 mins and no power is connected. This works in general and is nothing to do with specifically being in the car or not. It just saves a little power when not using Bluetooth, otherwise I'd leave Bluetooth on all the time anyway.
Screenshot_20181011-121639.png
Screenshot_20181011-121639.png (174.26 KiB) Viewed 18073 times
When I get in the car, Bluetooth gets turned on (if it's not already on) because another flow turns on Bluetooth when the power gets connected or the display turns on. I then find the phone automatically gets reconnected to the MMI as the MMI powers up. I don't use an Action to explicitly connect to the MMI. I think the phone and MMI automatically find each other as previously connected devices and connection is attempted. Not sure if it's the phone or MMI that initiates this automatically - I know I don't!

I use the Bluetooth connection state to the MMI to trigger what happens when I enter/exit the car with more flows

User avatar
xpdev
Posts: 59
Joined: 16 Sep 2014 06:20
Location: Italy

Re: Wait for Bluetooth to connect?

Post by xpdev » 18 Oct 2018 04:46

Execution policy for this flow ?
Xiaomi MI 6

Rom Miui 10 EU STABLE

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Wait for Bluetooth to connect?

Post by Desmanto » 18 Oct 2018 06:14

Since you are dealing with trigger that you use too in action, better use SKIP. This minimize the possibility of loop-triggering the flow.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Wibbly
Posts: 418
Joined: 17 Mar 2014 09:02

Re: Wait for Bluetooth to connect?

Post by Wibbly » 18 Oct 2018 07:02

Desmanto wrote:Since you are dealing with trigger that you use too in action, better use SKIP. This minimize the possibility of loop-triggering the flow.
For my flow above I use "Stop the current executing instance and then execute the newer instance". This is because, if the flow is re-triggered, it means a Bluetooth device has just disconnected again so I want the 5 min delay restarted. Otherwise it's possible Bluetooth will get disabled "early" if Bluetooth connections are a little unstable.

The loop-trigger scenario is unlikely (and will only likely cause one extra execution of the flow) and occur only in the case if Bluetooth reconnected in the few milliseconds between the final condition that all Bluetooth devices are disconnected and Bluetooth being switched off :-)

... and just thinking further, I should probably add an extra trigger for 'Power Source Disconnected', so the flow also starts when power is disconnected. This would be for the use case where all Bluetooth devices were disconnected with the power still connected so Bluetooth stays on - but then I disconnect the power - and I would want Bluetooth turned off 5 mins later if there were still no Bluetooth devices connected, the screen was off, and I was still on battery power.

Post Reply