Reddit – Dive into anything
Edit: V2.0, with long button press detection and more reliable button press detection linked below.
I had a Bluetooth remote that came with a selfie stick lying around the house a while back (don’t ask) and I wondered if it’d be possible to use it to launch Tasker tasks.
After a bit of frustration I worked out a pretty straightforward method that uses Autoinput.
Some potential uses for this that I’ve come up with are:
-
Launching Google voice search, since my phone doesn’t wake up reliably when I say “OK, Google”
-
Liking or disliking a song
-
Finding out the Artist and title of song that’s currently playing
-
Any activity you’d normally launch with a voice command but can’t because you’re on public transport or in a quiet area
This should work with most bluetooth devices with buttons that can connect with your phone (such as selfie stick remotes, car stereos, audio receivers, video game controllers, etc).
Setting up your profile and finding your keycodes
Create a new profile that has a single condition in it of Event > Plugin > Autoinput > Key. You can keep all of the settings the way they are for now. When you turn this on, Tasker sees a variable called %aikeycodename which contains the name of the button pressed.
Next you’ll need to create a task. The first action is Alert > Flash. In the text, put You just pressed %aikeycodename. When you push a button, it’ll show you the name of the button you pressed – sometimes they’re mapped to weird things, and other times they won’t show up at all.
Create a second action of
Plugin > Autoinput > Modes
and choose
Key Suppress > disable.
Without doing this the buttons on your device (and your phone itself) may stop doing what they’re meant to do after you’ve finished using the task.
Back out of tasker to save your changes, then press a button on your bluetooth device. Take note of the key name that’s displayed when you hit each button.
Setting the individual buttons to monitor
Go back into your profile and go into the Autoinput Key configuration. Put a checkmark next to the keys that you want to use – any other keys will be ignored.
Go back into your task. If you don’t want the button to perform its normal action, add an action called
Plugin > Autoinput > Modes
and choose
Key Suppress > Enable.
(That means if you are using the Volume Up key, for example, hitting the key won’t increase the volume. ) Then click Keys and put a check next to the keys you want to react to – these will be the same ones you chose in your profile. Move this action to the first spot in your task.
Assigning tasks to run
After that you can disable the Flash we set up earlier and insert a task to launch by adding an action
Task > Perform Task
and choosing the task(s) you want to run. Make sure the Disable Key Suppress action is the last one in your task.
Things to note
-
Your phone can’t tell where a keycode has come from – so it’ll see the Volume Up button on your phone, car stereo, and wired headset as the same command. So choose the buttons that you use carefully. If you’re using the Volume up button and turn on key suppression, the Volume up button on your phone won’t turn the volume up either.
-
You can add a second condition to your profile like BT Connected to make sure it only fires when you’re connected to a particular BT device.
-
You can react to multiple buttons in the same task by putting an if condition on the individual actions so that it only runs if the correct %aikeycodename is created.
Link to sample project
Edit1… Here’s a new version that works with double clicks. This is useful if you want to assign an action to a button on double click but you still want to be able to single click to use the button’s normal function.
I’ve modified the task to check the time that the current click happened and to compare it to the time of the last click. If it’s less than 4 seconds between the 2 clicks it will count as a double click. You can change that to a lower amount of time after experimenting with your BT device and phone to see how closely apart it can detect the clicks.
The task uses Collision detection to kill the existing task if a new one starts before it finishes (which would happen if you double clicked).
In the Profile I turned on detection of the home button for an easy way to test this, you can remove that key when you’re done testing.
Edit 2 Version 2.0.
This version puts the Key suppression and release in its own task, making it fire more reliably. I’ve set the task to fire when BT is connected to the specific device we’re using, and the key suppression is disabled as an exit task.
Now onto the good part. If you have volume up and volume down buttons you’d like to map, for example, but you don’t want to lose the button’s normal functionality, you can set separate tasks for a regular button press and a long button press.
The way it works is that one task fires when a button enters the down state, and we write the time that it happens into a variable. Another task runs when the button is released – this task grabs the time when it’s released and compares the two numbers to see how long it was held down for. If the button was held down for more than 2 seconds, it runs a task in the long press section. If there’s less than a 2 second difference it runs a task in the regular click section. As long as you can replicate what the button normally does with a Tasker action, you won’t lose the button’s normal functionality.