Socket coding in Android

So I’ve recently been playing about with Android for ‘Not So Super Secret Startup Idea’ and needed some code to communicate data such as Phone Calls/Text messages and other things from my phone to another device using TCP/IP. This is a bit harder on Android then it is on a PC as Android is bassed on apps having something called Activities and Services.
An Activity is the part of the Application which handles user input and can be destroyed at any time by the OS. This makes it pretty much useless for running anything which will communicate with the outside world without user intervention.
The other part part of an Android app is called a service which can be used to perform tasks in the background, process events from Activities, etc.
So what I currently have is an Android app which has a Service to handle TCP/IP communications from a telnet client on a PC. The service will also send data such as GPS location, phone calls and text messages (currently work in progress) over the socket to the PC.
One problem with the default TCP/IP functions is that they block (do not return until the right amount of data is received). I choose to use threads to handle blocking I/O and post Intents (a form of IPC) to the main activity to update it’s UI.
There is however one problem with using Threads in that when the Service is stopped there is no clean way to Kill a thread. This can be gotten around by closing the socket and setting a variable inside the threads main loop to tell it that it needs to shutdown cleanly.
So here’s some quick and dirty code which output’s GPS location and the phone no. of any calls received to a socket:

GPSTest

Going Underground

IMG_4463
A few months ago I saw that the London Transport measeum where offering tours of Awdych tube station so I managed to book me and S a couple of tickets before they sold out.
Awdych is one of the many abandoned tube stations on the London Underground network and which you don’t often get the chance to visit.
The tour started at the booking hall where we were told about the stations history and ultimate downfall. We then descended the 120 stairs down to the bottom of the lift shaft. We then looked around the lift shafts before proceeding to the platform level and the first of the platforms at the station.

IMG_4464

IMG_4465

We also saw the second platform which was used as a storage area for actifacts during the war, had a look at the tunnels beyond the station (to protect against trains missing the station from derailing). We then finally saw some of the unfineshed tunnels from the lift to the platform.

More pictures are on Siobhán’s blog.

RS485 networking

A couple of years ago I built the first version of my tube time display which shows the time of the next train at my local Underground Station.  This was connected to a Raspberry PI via a USB connection.  I found the Raspberry PI not the most reliable piece of hardware in the world and it was also a bit of an over kill for a simple display so I’ve been working on a new version of my display that uses RS485 to communicate with a computer which can be a long way away from it.

The original tube time display thing.
The original tube time display thing.

For this I decided on a token bus type network where each device sends out a token to the next device telling it that it can use the communication channel. This allows me to attach other AVR/Arduino based devices to the same piece of cable (I’m using CAT5 ethernet cable). I also have one device on the network acting as a superviser which will create tokens for a device if the token is given to a device that doesn’t exist.

So far I’ve managed to send text between two AVR32U4 boards over the bus so it’s going quite well so far. I think things can be further optimised so the bus is faster but it’s certainly good enough for my tube time display.

IMG_4440