Mobile Tasker: drive to your favourite songs
Guide

Mobile Tasker: drive to your favourite songs

User Kurt asked: how can I use Tasker to play music in my car? What began as a harmless little experiment has become a maelstrom of old-school technology and bleeding edge innovation.

Kurt, a user, recently wrote us an e-mail: he doesn't want to activate the audio player of his tablet manually every time he gets in his car. His routine is always the same: get in, start the car, activate Poweramp on his FCC launcher, drive off.

My goal is for the tasker to start up Poweramp while the radio turns on.
Kurt, by e-mail.

The problem is easily solved. Kind of. Because while Android apps always work the same way, the car industry hasn't yet agreed on standards. Cars built before 2016 don't have an Android Auto interface. Newer cars have their own software, which must first be switched to Android.

If you're working with imported smart tech from China, chances are that you're not dealing with Android, but with the FCC Launcher. There are legal reasons for this, as Google and its services are either censored or prohibited in China. That's why Kurt has this tablet thing from China instead of a car radio. Or a cell phone. Or a conventional tablet. In other words: there are a myriad of variables here that don't exactly apply to your Smart Tech or your car.

But the one constant here is still Android. Therefore, I'll leave out the peculiarities of cars and Russian launchers and instead focus on app automation: this is where Tasker comes in.

Tasker, Android Auto, FCC launchers, Poweramp and Spotify... oh my

If you're about to ask, «Can Tasker...» I can stop you right there. The answer's «Yes». Basically, Tasker can do everything. The question's usually just «How?». Because while Tasker offers many options out of the box, you'll often have to interact with foreign program interfaces, called API.

  • Background information

    Tasker: The app that can really do everything

    by Dominik Bärlocher

Tasker always works according to the same scheme: if, then. Or in programming languages: if/then. In other words, if software event A happens, then Tasker will execute automated sequence B.

  • Events in Tasker are called «Profiles».
  • Automatisms are called «Tasks».

Tasker is the linchpin of our current project. On the hardware side, I tested with the following equipment:

realme X2 Pro (128 GB, Neptune Blue, 6.50", Dual SIM, 64 Mpx, 4G)
Smartphones

realme X2 Pro

128 GB, Neptune Blue, 6.50", Dual SIM, 64 Mpx, 4G

Lenovo Tab M10 FHD Plus (2nd Gen) TB-X606F (WLAN only, 10.30", 64 GB, Iron grey)
Tablets

Lenovo Tab M10 FHD Plus (2nd Gen) TB-X606F

WLAN only, 10.30", 64 GB, Iron grey

And for software, I used the following apps. I purchased any apps that work better with a paid version:

After programming everything, I tested the setup on two vehicles:

The vehicles were selected to be as far apart as possible in terms of software. With the Ford, I can plug in the tablet or smartphone and work with Android Auto. With the Dodge, I have to combat UConnect. The software isn't bad, but it hasn't been updated in years and is missing modern interfaces.

For this project, I'll try to automate Poweramp or Spotify Music in every halfway modern car with a decent smart audio system before driving off. You need a car that can either play music via Bluetooth from your phone or access Android Auto with a cable.

I will also of course take driving safety into account for this project. This is particularly important for a Bluetooth connection between your car and smart device. The basic idea is clear. Interaction with the phone or tablet should be minimized as much as possible.

Before the code comes understanding

Before we set off, let's make some things clear. Namely, we need to decipher the black magic of car software, and iterate some mechanical details of cars. Because in the context of how Tasker works, we need to find the profile, i.e. the «if» statement in «if/then». To do this, we have to break down what exactly happens to a car to trigger the «if» component.

Classic: a car with an ignition key

It's possible to equip an old car with an ignition key with a modern audio system. When the motor is off, no power is supplied to any system. The clock may be digital, as it contains a small battery. But even in an analogue watch, the battery always remains.

When you turn on the ignition, the first thing you'll encounter is ACC. This is the power mode that supplies the electronics of the car with power from the battery. This is followed by ON, where the engine is turned on. Between ACC and ON, current flow to the audio system is interrupted again, as the current is used to start the engine.

In between: Dodge Charger Bluetooth

Test car: the Dodge Charger...
Test car: the Dodge Charger...
...with UConnect
...with UConnect

The Dodge Charger works differently. But the ignition and the interaction between the engine and the on-board electronics work exactly the same. No power while the engine's off, power and ignition while it is.

But I can't control the audio system via a cable. At least not in any useful way. The most sensible and easiest way to work is with Bluetooth. As soon as the car's computer starts up, it connects to the smart device via Bluetooth. This being your smartphone or tablet. I can now make phone calls and use the car as a kind of Bluetooth speaker via an audio source. Differences to a normal Bluetooth speaker are negligible.

Smart: Android Auto on the Ford F-150

Test car: Ford F-150 Shelby...
Test car: Ford F-150 Shelby...
with Ford Sync 3
with Ford Sync 3

When you start up the Ford, a computer controls the ignition. When the engine stops, the computer isn't supplied with power. This would discharge the battery overnight. The 2.8-tonne behemoth won't be going anywhere the next day. A mini computer is supplied with power, which essentially remembers the time.

After ignition and starting the computer, I have to manually switch on Android Auto on the screen. There's an icon on the touch screen. Once I press it, the software on my smartphone takes over and displays it on the truck's screen.

Project structure in Tasker

With these three functions, we see three «If» statements. If you want to copy one of these projects, you can simply search for the profile and its number, i.e. «Profile #1» with CTRL/⌘+F.

  • Profile #1: If Power Supply...
  • Profile #2: If Bluetooth connected...
  • Profile #3: If Android Auto enabled...
  • Profile #4: If FCC Launcher enabled...

These are defined in the Profiles screen. It should appear on-screen by default when you start Tasker.

This is followed by two tasks:

  • Task #1: ... start Poweramp.
  • Task #2: ... start Spotify.
  • Exit Task: Kill App.

You can find it in a tab on the right side of the profile screen.

But we don't just need the so-called Entry Tasks. The Exit Task is vital. This ensures that after parking the car, music changes back to your Smart Device with a battery and continues playing.

Profiles as well as tasks can be modified and aren't only useful in the car. Profile #2 works with any Bluetooth speaker, for example.

In terms of workflow, it's worth defining the tasks first and then the profiles. This isn't vital, but over the years of working with Spotify it has become commonplace.

Poweramp: The Setup

Later in this article, Poweramp and its settings will become important. I've tried to simplify this process here. I copied about 60 MP3 files from my hard disk to the «Music» folder of the Android system.

After that, I put all songs into a playlist and christened it «001». This'll make creating an automatic setup easier, as the name contains no spaces. So «Good Songs for Rainy Weather!» is less suitable. Names like this one don't just contains spaces, but also special symbols. If you want to call the playlist something about rain and music, I recommend «GoodSongsForRainyWeather» or «RainSongs».

Profile #1: When the power's off...

In an old car with an audio system that allows connection to the smartphone or tablet via USB, we can set the profile like this: music is played as soon as the smart device detects that there is a power supply.

  1. Open Tasker.
  2. Go to the «Profiles» screen.
  3. Press the plus symbol.
  4. Press «State».
  5. Press «Power».
  6. Press «Power» again.
  7. Select «Any» as an option.
  8. Press the back arrow at the top left.

This is by far the simplest version of «If» in this manual. From now on, we will gradually incorporate more variables and programming knowledge.

Tasker profiles and tasks can be exported. You can import this syntax into your Tasker app. Tasker displays it like this at the top:

   Profile: Poweramp_on (5)
        Restore: no
        State: Power [ Source:Any ]

Profile #2: When Bluetooth is connected...

In a semi-smart vehicle, you can connect your smart device to the audio system via Bluetooth. However, complete integration into the vehicle software is absent. When connecting, the vehicle transmits a unique identifier, the name of the Bluetooth connection and a Bluetooth address using the format 00:11:22:33:FF:EE. The Dodge Charger identifies itself to me by the identifier «UConnect».

It's possible to extend the profile to any Bluetooth connection, but this is impractical given the vast number of Bluetooth devices. You don't want music to start when you connect your Smartwatch to the phone.

  1. Connect the smartphone to the car manually. Tasker can read the Bluetooth address on its own. That makes it a little easier for us.
  2. Open Tasker.
  3. Go to the «Profiles» screen.
  4. Press «Net».
  5. Press «BT Connection».
  6. Press the magnifying glass next to the «Name» icon.
  7. Dial in the connection of your car, «UConnect» in my case, followed by an address.
  8. Press the back arrow at the top left.

This means that music will only play when you connect your smartphone to this car. This trigger only works on this one Dodge, not its friend from the same series. Because the address is unique. So if you want the same function for another car, you have to duplicate the profile and adjust the name of the connection.

   Profiles: Poweramp_bt_direct (18)
        Restore: no
        State: BT Connected [ Name:SRS-XB31 Address:00:11:22:33:FF:EE ]

Profile #3: When Android Auto is active...

In a smart car, Android Auto will not activate until you tell either the smart device or the car to activate. In the Ford F-150, this is an icon on-screen. In other cars, the command can come from your mobile phone.

Android Auto isn't so easy to identify on your smartphone or tablet. The app isn't really an app in its own right from a technological point of view. Essentially, it simply changes the view on-screen so that it's optimized for use in the car. But as soon as something changes on an Android system, something is adjusted in the background. In the case of Android Auto this is a variable.

%UIMODE

This variable is set to the value «car» if Android Auto is active. And this is exactly what we can intercept in our Tasker Profiles screen.

  1. Plus symbol.
  2. «Event».
  3. «Variables».
  4. «Variable Set».
  5. Enter «UIMODE» into the upper field beginning with the % sign without quotation marks.
  6. In the «Value» field, type «car».
  7. Back arrow.

Here's what Tasker will throw at you:

   Profiles: Poweramp_androidAuto (24)
        Restore: no
        State: Variable Value [ %UIMODE ~ car ]

Profile #4: when FCC Launcher is active

FCC Launcher and Android Auto are both apps. The icons are very similar, but FCC can make do without Android and vice versa. Therefore, if a setup is running on FCC Launcher – Hi Kurt! – then the app must be intercepted separately. This is even a bit easier than monitoring the temperamental Android Auto: FCC Launcher isn't deeply integrated into Android, but sits at surface level. Back to the Profiles screen:

  1. Plus symbol.
  2. «Application».
  3. «FCC Launcher», you'll see two icons. Makes no difference which one you choose. And if it did, just correct the profile and take the other one.
  4. Back arrow.

    Profiles: Poweramp_fcc (25)
         Restore: no
         Application: FCC Launcher

    Now we've dealt with probably the most used variants of starting a car. And if you have a different app or a variable that Tasker should look out for, you know how to do it. It's not that difficult.

    Task #1: start Poweramp

    Poweramp has an open program interface, called API. We can address this API directly and pass commands to the app without having to call up the graphical user interface. In FCC Launcher, you'll see the album in a big circle, but you don't have to interact directly with the software.

    However, we're still adding a modifier: a waiting period. This is necessary because a Bluetooth connection takes some time to negotiate the audio output settings. Waiting time is also useful with a wired connection, as it allows you to skip the audio system's false start in ACC mode during normal engine start-up and the music won't start playing until the engine is running.

    For this task we switch to the task screen in Tasker. And this is where it gets really cool:

  5. Plus symbol.
  6. Name the task «Poweramp_play».
  7. Plus symbol.
  8. At the bottom you'll see a filter, we'll use it for all task elements, otherwise you'll just click around foolishly.
  9. Filter: Wait.
  10. Adjust the settings to 5 seconds.
  11. Back arrow.
  12. Plus symbol.
  13. Filter: Send Intent.

This is where I have to strike out. An Intent is a command that we send directly to Poweramp's API. This is quite complex to describe. Hence this little interlude.

Poweramp and Tasker: Intents

Poweramp allows you to send commands to the app from Tasker. For this, we have to fill in three lines in the Tasker task screen «Send Intent». You can ignore the rest of the overwhelming screen. At least for our present project.

In the «Action» line, the top line, we tell Tasker where the API is and that it should talk to it. We then enter the following:

com.maxmpz.audioplayer.API_COMMAND

With «com.maxmpz.audioplayer», we address the Poweramp app. This is the system internal name for Poweramp. «API_COMMAND» is the part that tells Poweramp: «Now comes a command you can execute».

In the «Data» line, we have to tell Poweramp that the app should work with our playlist. The one we called «001». That's why we write:

content://com.maxmpz.audioplayer.data/playlists/001/files

If the playlist was called «HelloKurt» now, it would say

content://com.maxmpz.audioplayer.data/playlists/HelloKurt/files

With this, we tell Poweramp that we want to talk to a playlist and its files in Poweramp. So Tasker now knows that we want to talk to Poweramp and Poweramp knows that we want to talk to the playlist «001» and its files.

From here, we can directly give commands to the playlist. Maxim Petrov, sole developer of Poweramp, has made the commands public.

1 Play/Pause
2 Pause
3 Resume
4 Next
5 Previous
6 Next Category
7 Previous Category
8 Repeat
9 Shuffle
10 Start Fast Forward
11 End Fast Forward
12 Begin Rewind
13 End Rewind
14 Stop
15 Seek
16 Request Current Track Position
17 Sleep Timer
20 Open and Play
50 Set Equalizer Preset
51 Set Equalizer nach String
52 Set Equalizer Band
53 Set Equalizer Enabled
100 Stop Service
200 Minimum Time in between Seeks

Since we don't want the same song to play every time the music comes on, we activate shuffle. In the first line with the description «Extra» we enter the following:

cmd:9

With this, Poweramp mixes all 60 songs but doesn't play them yet. To do this, we enter «Extra» in the second field:

cmd:20

Tasker needs one more piece of information. Namely that the Intent must be sent to the Service. In the «Target» field further down on the screen, you must select the «Service» option.

There’s nothing more to say. Arrow back until you're back on the Tasks screen.

In Poweramp syntax it looks like this:

   Poweramp_play (4)
        A1: Wait [ MS:0 Seconds:5 Minutes:0 Hours:0 Days:0 ] 
        A2: Send Intent [ Action:com.maxmpz.audioplayer.API_COMMAND Cat:None Mime Type: Data:content://com.maxmpz.audioplayer.data/playlists/001/files Extra:cmd:9 Extra:cmd:20 Extra: Package: Class: Target:Service ]

With this Intent, the Smart Device plays a random track from the playlist. In combination with the Wait command above, the music starts after five seconds.

Task #2: Start Spotify

Unlike Poweramp, we can talk to Spotify much easier.

  1. Plus symbol.
  2. Name the task «Spotify_play».
  3. Plus symbol.
  4. At the bottom you'll see a filter, we'll use it for all task elements, otherwise you'll just click around foolishly.
  5. Filter: Wait.
  6. Adjust the settings to 5 seconds.
  7. Back arrow.
  8. Plus symbol.
  9. «Media».
  10. «Media Control».
  11. «Play (simulated only)».
  12. App/Package Name: Spotify.
  13. Back arrow until you're on the Tasks screen.

In Tasker it should look like this:

   Spotify_play (22)
        A1: Wait [ MS:0 Seconds:5 Minutes:0 Hours:0 Days:0 ] 
        A2: Media Control [ Cmd:Play [Simulated Only] Simulate Media Button:On Package/App Name:Spotify Use Notification If Available:Off ]

Life Hack: Bluetooth and driving safety

If you work with Bluetooth audio often, then you know about its problem with volume. Bluetooth devices have their own volume control. Bluetooth headphones have their own volume control, which is completely independent of the volume control of the smartphone in bad cases. This may cause the phone to output audio volume 5, but the car radio will need to be turned up to full volume. Otherwise you won't hear anything. In addition, some car radios have the stupid effect that at very high volume levels, a hissing noise can be heard under the music.

In traffic, this is annoying at best and dangerous at worst. And it's illegal to play around with a cell phone while driving. So I've created a task that turns the media volume of the phone to the maximum level after a trigger. This allows us to control the volume of the music from the controller in the car.

On the task screen:

  1. Plus symbol.
  2. Plus symbol.
  3. Filter: Media Volume.
  4. Level: 15.
  5. Back arrow until you're in the Tasks screen.

In the exit task, when choosing the Bluetooth version for the whole event, I would create a Media Volume Subtask that sets the volume back to 5 or so.

    Poweramp_play_bt (16)
        A1: If [ %BtConnected ]
        A2: Wait [ MS:0 Seconds:5 Minutes:0 Hours:0 Days:0 ] 
        A3: Media Volume [ Level:15 Display:Off Sound:Off ] 
        A4: Send Intent [ Action:com.maxmpz.audioplayer.API_COMMAND Cat:None Mime Type: Data:content://com.maxmpz.audioplayer.data/playlists/001/files Extra:cmd:9 Extra:cmd:20 Extra: Package: Class: Target:Service ]

Exit Task: Kill App

With this, we've finished the playback aspect of our mission. What's missing now is the Exit Task. It's important that the music stops playing when the power supply is cut off, Android Auto is deactivated or the Bluetooth connection is terminated. It's possible that Poweramp or Spotify or any other app decides to just keep playing music from the phone or tablet. We can prevent this with an exit task.

Poweramp Exit Task: end app via Intent

In the Tasks Screen, we create a new task with the name «poweramp_kill».

  1. Plus symbol.
  2. Filter: Send intent.
  3. Action: com.maxmpz.audioplayer.API_COMMAND
  4. Extra: cmd:2
  5. Back arrow until you're in the Tasks screen.

    ``` Poweramp_kill (8) A1: Send Intent [ Action:com.maxmpz.audioplayer.API_COMMAND Cat:None Mime Type: Data: Extra:cmd:2 Extra: Extra: Package: Class: Target:Service ]

 
### Spotify Exit Task: Media Controls
 
Spotify can be interrupted in the same way as it's called up. With a simulated button press. We do this in the task «spotify_kill».

1. Plus symbol.
2. «Media».
3. «Media Control».
4. Cmd: Toggle Pause
5. Package/App Name: Spotify.
6. Back arrow until you're on the Tasks screen.
Spotify_kill (28) A1: Media Control [ Cmd:Stop Simulate Media Button:On Package/App Name:Spotify Use Notification If Available:Off ]

```

A short conclusion

After defining the tasks, we can combine them in any order. That's pretty neat. In the Profiles screen, you define your «if» statement. Add «When Bluetooth connected» or «When Android Auto active», then add a task.

  1. Define profile.
  2. Add a music playing Task.
  3. Long click on the music task in the profile screen.
  4. «Add Exit Task».
  5. Define your Exit Task.

Ta-dah! Make sure that the profile slider is on and you can get into a car and it works great. Attention, if you work with Spotify, the tablet or phone must be online. Otherwise, it's useless.

Conclusion: a complex approach.

The complexity of this project isn't necessarily in playing the music, but correctly recognising the environmental factors and taking them into account. That's what I like about working with Tasker. After Kurt's mail, I thought to myself «it'll be done in ten minutes», but in the end I had to take a few hours. I spent a large part of the time analyzing and questioning the mechanical processes in a car. Thus, the simple start of a vehicle became a technologically complex process. Although knowledge about it is completely useless in most cases, I understand more about everyday life.

Even if the project may seem overwhelming and infinitely complex at the beginning, solid research in advance finally makes things simple. Thanks to the variety of Tasker's functions, we can intercept or manipulate just about anything. It's fun. And simplifies our lives greatly in the end.

Then there's the importance of APIs and their clean documentation. It's much easier to interact with Poweramp than with Spotify. Complex commands like shuffle and then play can be handled easily where interaction with Spotify only allows play/pause.

In the end, I hope that I've created workable solutions for your situation. Kurt's problem is certainly solved. And even if your Use Case is not covered: I hope I've guided you safely in the right direction.

Finally: Downloads

Tasker has the cool feature that Profiles can be exported in XML format. I uploaded these to my GitHub account. So they're freely available for you. Have fun with them!

You're welcome to use and modify the data in the repositories for yourself. If you do anything cool with it, let me know.

Got another project you can't handle? Let me know. I'm happy to do it.

And that’s it for today. Have fun and keep on hacking.

49 people like this article


User Avatar
User Avatar

Journalist. Author. Hacker. A storyteller searching for boundaries, secrets and taboos – putting the world to paper. Not because I can but because I can’t not.


These articles might also interest you

Comments

Avatar