Archive for the ‘Android’ Category

DIY Hands Free Kit

Tuesday, May 10th, 2011

A few years back I came up with the CarPC as a cheapass hacky way of getting MP3 playback integrated into my unspectacular car. Since then, the world of mobile has moved on a whole lot and the laptop stashed in my boot actually pales in comparison with the features (and power) of my Desire Z Android phone. Plus, I’m already syncing music with my phone, like I used to with my car PC and with Google Navigation I have a powerful satnav solution already at hand. All I really needed at this juncture was some kind of hands free setup.

Bluetooth control?

Grom Audio, who made the CD autochanger interface that I used for my car PC also make an autochanger interface which extends with a bluetooth module. This seemed like an unnecessary expense however – I already had the Car2PC autochanger interface which still acts as a high quality stereo input even without the USB control via the car head unit.

The official HTC headset

The headset that came with my phone has an integrated mic with three buttons: answer call/play/pause, next track and previous track. Wouldn’t it be cool if someone had worked out what the circuit inside that thing was? I googled around a while but in the end, one of my friends pointed me to George Smart’s Wiki, where he’d already sacrificed his headset and worked out a circuit diagram. This was massively useful, and given that the circuit consisted of a few cheap components, I thought I’d see

if I could give it a go and make a working copy.

Circuit prototype

If, like me you haven’t done any electronics since you were at school, it’s a good idea before you start soldering to get a prototype board. This is the Lego of electronics – you can stick components into the board without solder and switch things around if needed. I got the smallest prototype board I could find from Maplin for a couple of quid and had a circuit which gave the right resistances fairly quickly. The idea was to produce something I could plug my phone into on one end and would have stereo output and a mic connection so that any generic mic with a 3.5mm plug could be used with it. In addition to the components list on George’s wiki I needed an extra 3.5mm microphone socket, a 3.5mm stereo socket, a 3.5mm 4-pole plug (for connecting to the phone).

Now, it turns out the connecting to the phone part was problematic. The audio connection on HTC phones is pretty much the same as an iPhone. It’s compatible with a standard stereo heaphone connection (3.5mm 3-pole plug) but it has a third “control” ring on the plug which is used to carry the signal for the microphone. Shorting this signal (with the play button) sends the signal to the device to play music and the next/previous buttons simply send approximate voltage signals down the wire by varying the resistance slightly. In order for the controls to work I needed a 4-pole plug. You can get 3.5mm 4-pole plugs from Maplins, but holy crap are they fiddly to solder. I spent a couple of really frustrating hours trying to get four lots of wire to stick to the really unhelpful contact surface on the plug I bought. These were not designed for people whose GCSE in Technology a distant memory.

In the end I gave up and managed to find a much more solder-friendly 4-pole socket on eBay and got a pre-made male-to-male 4-pole plug cable (which you can even pick up in HMV these days).

Two resistances and a microphone

Here’s the part where I’m glad I used a prototype board first. I selected a reasonable looking tie-clip mic online that would handle calls and voice commands for the phone and plugged it into the circuit. Suddenly, the “next track” button didn’t work. What had transpired was that by adding what was effectively a 1.4 kilo-ohm resistor to the circuit in parallel, I’d lowered the effective resistance when I pressed the next track button. This meant the phone interpreted it as “previous track”. To work around this, I needed to raise the resistance on the “next track” button. If you are trying this and get the problem, my method was to measure resistance across the mic with a multimeter, then using the resistors in parallel equation plug in the target resistance on the control wire (560 ohms) and the value of the other parallel resistor on the circuit. With a little help from ElectroDroid, this gave me a new resistor value to plug in (1 kilo-ohm, I think I went for) which then made all the buttons work with the mic attached.

Constructing the device

I guessed an approximate size for the circuitry, buttons and sockets and got a small project box from Maplin. For my first attempt at this, I figured I’d make life easy for myself and just put all the connections on top so tha

t I could work on cutting the holes to the right shape and getting everything to fit in, then I could just pop the circuit board into the box and screw the lid down.

The photo is what I ended up with. From the left, the interface is:

  • Stereo audio out (to the car audio system)
  • 4-pole 3.5mm stereo audio and control I/O (the phone)
  • 3.5mm 2-pole mic socket (goes to tie-clip mic, which is going above the door frame in my car)
  • Previous track button
  • Play/answer button
  • Next track button

Seamless automobile integration AKA “The part with the duct tape”

So that’s all the bits working. Now to find a space in my car that I can fit this where it’s in easy reach, won’t get in the way and can be secured easily. In the end I went for the space just in front of my gear stick, mounting it next to the 12v power socket, over the top of the ash tray. The stereo cable for the car audio actually goes through the back of the car and plugs into the Grom Audio CD autochanger emulator. The mic cable is taped around the dash up the side of the windscreen and sits near the top of the door, in clear audible range. The phone is held in a generic phone mount on the top of the dashboard and plugged in to the 12v power (converted for the phone of course).

I might neaten it up. I probably won’t.

Extending the controls

Having the standard audio controls, plus the ability to take calls is all well and good, but what about being able to do other useful stuff with those 3 buttons? I’ve previously blogged an example of using Tasker with the Headset Button Controller app, so I won’t labour the point, but suffice to say with these two little gems you can pretty much tie or automate any Android task you need to with only 3 buttons at your disposal. I keep my Tasker profile checked into SVN if you want to borrow bits of code, and I’m changing it all the time but here’s some of the things I’ve used this setup for:

  • Press a button to activate voice commands (“navigate to cambridge”, “call home”, “listen to beastie boys” etc)
  • “Love” the currently playing track
  • Automatically start playing music when headset and power is attached (poor man’s dock mode)
  • Automatically read out incoming texts using text-to-speech
  • Announce callers using text-to-speech
  • Re-read the last text message
  • Call a specific person using a button press
  • Launch navigation to a specific place with a button press (i.e. the “oh shit, I’m lost” button)
  • Lock onto heat signatures and engage stinger missiles

So there we go. That’s the kind of car setup that you’re only just starting to see in new cars at the moment (at time of writing, ahem) and for complete pocket money.

“Love” tracks on with one click of a headset button

Saturday, April 30th, 2011

Here’s a handy little trick for keen users on Android that lets you mark the current track as “Loved” with a headset button combination. What you will need is:

  1. The official client for Android
  2. Tasker
  3. Headset Button Controller
  4. A headset remote button or the headphones that probably came with your phone if it’s got buttons on it

Set up

Nothing much to do other than log in and make sure your media player of choice scrobbles tracks correctly. I’m using Winamp which works fine, but make sure you’ve turned on scrobbling in the settings. The widget has a “love” icon which due to the awesomeness of Android’s intents architecture we will be calling on. Add the widget somewhere on your home screen and make sure that bit works.

Create a “Love Track” task in Tasker

In Tasker, click “Tasks” -> “New Task” (call it “Love Track”) and in the task edit window add “Misc” -> “Action Intent” and in the “Action” setting, enter “”. Leave the other fields blank and make sure “Target” is set to “Broadcast Receiver”. You could also add an action which says “You love this track” by using “Misc” -> “Say”.

Start a track playing and come back to the edit screen and then hit “Test”. The app should create a little toast notification (one line of text overlayed on the screen) saying the track has been marked as played. After a short while it should appear in your profile.

Optional: Create a home screen shortcut

Long-click on a space on your Android home screen and choose “shortcuts” -> “Task” -> “Love Track”. You can now click on that to mark the currently playing track.

Headset button controller

Headset button controller is a stupendously useful app that allows you to assign actions to headset buttons. If your headset only has one button this is great because you can do more than answer/hang up and play/pause by assigning double, triple, quadruple and long-click actions to a button.

Once you’ve installed this set up your usual button actions, simply set one to “Tasker Task” -> “Love Track” and you’re done!

Remote Wireless Music Syncing Android and Linux

Sunday, February 13th, 2011

Recently, an Android version of Winamp was released and included in its feature list was very useful wireless syncing with the Windows version of Winamp. Unfortunately, my file server at home doesn’t run Windows and Winamp under Wine is an unstable mess, so attempting to use that feature was out. Like a good Linux user, I didn’t take this lying down – we don’t sit around like Windows users waiting for someone to build the solution for us, we cobble something together using tools already available! What I decided to do was to emulate the mechanism that I came up with when I built my Car PC. Basically, the core of wireless syncing in the Car PC was rsync, combined with a little bit of logic to get a list of music track locations to pull over. By offloading some of that logic onto the server, this seemed possible.

First, I installed an rsync app for Android and tested it out. Rsync backup is just a front end for rsync, which is perfect because it lets you use all the normal rsync options, allows public key authentication (so you never need to enter a password) and displays rsync’s output to a log window. I wanted to pull files from my server so in my rsync profile I selected “rsync in reverse” and added the command line options “-vHrtDL --chmod=Du+rwx,go-rwx,Fu+rw,go-rw --no-perms --progress --partial” most of which is the default for a new profile, but I added:

  • -L, to follow symlinks
  • --progress, to display file transfer progress
  • --partial, to allow file transfer resuming

Android won’t know any of the new music files have been added yet, so they won’t show up in any music players (which all look up available tracks in the built-in Android media database), so you need to install something like SDrescan to run after the files have copied over which will magically make them visible in the music app of your choice.

That’s the phone side sorted out, I just need something sensible to point it at – I don’t want rsync recursing over my entire music collection wirelessly, it would take ages.

On the server, I’m running the (somewhat ageing) Firefly Media Server, which indexes and serves music for my Roku Soundbridge. I’d previously written a data abstraction class in PHP for the sqlite database it runs on, so I used that to produce a script (to run periodically under cron) which picks a list of albums from a playlist (created by another script) and creates a bunch of symlinks to them in the folder that I set as the target in my rsync profile, the idea being I have a regularly changing folder which one rsync command can look at to pick up new music.

I now have wireless syncing that works anywhere with an internet connection, as well as on the local network (unlike Winamp) and the idea of keeping a static rsync profile and using a source folder full of symlinks provides a cunning way of pushing content onto your phone. It would be quite easy to create something like a web interface to pick files on the server and have symlinks of them created on a single rsync source folder. On top of this, Rsync backup provides hooks for Tasker to link into, so you could set this up to run automatically at some opportune moment.