Channels ▼
RSS

Tools

Voice Throwing with Python and Android


Configuring the Task

Once you have Tasker running on your Android phone, create a new task by selecting the Task tab, then selecting the plus icon. Assign the name "Read Notification" to the new task. Then add a new action to the Read Notification task by selecting the plus icon. This will display a Select Action Category overlay with a number of Android features or actions that Tasker can call upon or activate. Select the Variables action category. This will display another overlay showing the list of actions available in the Variables action category. Choose the Variable Set action. Doing so will display the Action Edit Variable Set form.

In this form, create a Tasker variable called %NOTIFY and assign it to %NTITLE. %NTITLE is Tasker's built-in name for Notification Title (the text that shows up in Android's notification area). Your Android screen should look like Figure 1.

Android Tasker
Figure 1: Variable set.

Select the Action Edit Variable Set to save the action. Create another action to submit a POST to the Bottle server. Do this by selecting the plus icon and choosing the Net Action Category, followed by the HTTP POST action.

In the Server:Port field, enter the IP address of the computer that is running the desknotify.py script. If you plan on using the script frequently, it's a good idea to set that computer to a static IP address so you don't have to keep adjusting the Server:Port value. Next, set the Path value to /say, and the Data/File value to message=%NTITLE. The form should look similar to the one shown in Figure 2. Note that your server IP address value will likely be different from mine given the IP range of your own local area network.

Android Tasker
Figure 2: HTTP Post action.

Save the action by selecting the Action Edit HTTP Post label in the upper left corner of the form. With both actions configured, your Read Notification task should look like the Figure 3.

Android Tasker
Figure 3: Read notification task.

Now that the task is configured, I need to trigger it whenever a new Android notification is posted to the notification area. To do so, I create a Tasker Profile that listens for a notification area event. When one is detected, the profile will execute the Read Notification task. To set up this profile, select the Profile tab from the main Tasker screen and create a new profile by selecting the plus icon in the Tasker toolbar. Select the Event from the First Context pop-up menu that appears. A Select Event Category overlay will then be displayed. Select the UI category, followed by the Notification event. You can give the profile a name, though I usually just leave mine blank. Select the Event Edit Notification label in the upper left corner to save the profile. Doing so will prompt Tasker to ask which task you want to assign to the profile. Select the Read Notification task that was created earlier. Your Profiles screen should now have the Notification profile displayed as shown in Figure 4.

Android Tasker
Figure 4: Notification profile.

You may have noticed that when you initially save the profile, Tasker displays a warning that it needs to be granted accessibility access for it to read notifications. This is handled by going into the Android Settings application, selecting the Accessibility category, and turning on the Tasker - JB service as shown in the Figure 5.

Android Tasker
Figure 5: Tasker accessibility.

With the Read Notification set and the Notification profile activated, it's time to test this Android-to-Computer talking notifications project.

Trying it Out

If everything has been wired up correctly (and your computer speakers are on or your headphones are plugged in with the proper volume), you should be able to hear your computer speak any notifications received on your Android phone. You can verify that properly formatted requests were successfully received by monitoring the terminal or command-line window running the desknotify.py script, as is shown below.

192.168.1.4 - - [31/Jan/2014 19:54:46] "POST /say HTTP/1.1" 200 0
192.168.1.4 - - [31/Jan/2014 19:59:20] "POST /say HTTP/1.1" 200 0
192.168.1.4 - - [31/Jan/2014 20:02:37] "POST /say HTTP/1.1" 200 0
192.168.1.4 - - [31/Jan/2014 20:14:32] "POST /say HTTP/1.1" 200 0

If you're getting a 404 instead of a successful 200 value, make sure that you're sending requests to http://YOURCOMPUTERIPADDRESS/say. If you're receiving a 200 value, but are not hearing any output, make sure your volume is turned up and you are correctly passing the notification string via the message variable name. If you're using Linux, be sure to have your preferred text-to-speech library (such as Festival) installed and working. Given how simple this setup is, you shouldn't have too much difficulty tracking down and resolving any problems you might encounter.

Conclusion

This brief project shows just how powerful an Android utility like Tasker is when combined with a scripting language like Python. With just a few short lines of code and a couple finger taps, you can create a really useful service that provides a foundation for many other phone-to-computer automation tasks.

You could further improve this project by adding a cryptographic signature to a request key so that only those authorized to send and process a POST request to your Bottle server may do so. You could also employ a check in your Tasker notification task to see if our Bottle server is available before making a request. Adding a test a condition to see if your Android phone is on the same LAN as your computer before sending notification strings to the Bottle server would also be prudent. And if you want to get really creative, you could call upon other text-to-speech voices installed on your computers and assign particular voices based on notification keywords or events. It's all up to you!

If you enjoyed this article, check out more Tasker-related Android automation projects in my book Developing Android on Android.


Mike Riley is a blogger for and frequent contributor to Dr. Dobb's.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video