Coding
Android and Bluetooth
May 14th
I’ve been tinkering around with the Android SDK for a while and I find myself liking it more and more, despite the fact that the visual editor for views is still somewhat lacking. I even have an update ready for DroidPartridge! That is still being tested and the UI being ‘jazzed up’ a little more.
Anyway I was asked to write some code for Android to communicate with a bluetooth device that sends data via OBEX Push. Now the problem was despite Android apparently having the bluetooth profiles to support this, the device always signaled failure when trying to push a file over to the phone. The device has no user interface bar a set of LEDs and a machanism to cause vibrations (Wanted to say vibrator, but I couldn’t stop giggling)
Anyway, in Android you have to register a listening BluetoothServerSocket with the UUID (The Java GUID for you Microsoft people) of the service that you are listening for and an arbitrary name that is then put in to the local SDP database and used to compare with incoming connections. This is similar but slightly different to J2ME where you specify the address, what mode it will be operating in and it if times out, which is all taken care of by Android to avoid conflicts and instead, you just tell Android what to listen out for and pass you a BluetoothSocket back once a matching connection has been found.
mBluetoothAdapter.listenUsingRfcommWithServiceRecord(serviceName, uuidofService);
Now whilst the Android documentation is very good and in depth, it didn’t inform me or even point to a resource that specified which UUID I should use for listening for a particular protocol, so in this instance I was going to emulate OBEX over SPP (Serial Port Protocol/Profile) which meant listening for an incoming SPP connection.
I spent hours and hours running around in circles in Google’s search results reading the same thing mirrored over several sites (which was increasing the profanity by an order of magnitude each time I encountered a mirror site) until I eventually stumbled across the answer which would return a BluetoothSocket when a connection was attempted. I almost wept.
So to listen for incoming SPP connections on Android you need to specify the following UUID;
00001101-0000-1000-8000-00805F9B34FB
and the example snippet:
mBluetoothAdapter.listenUsingRfcommWithServiceRecord(serviceName, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
So when you call the blocking method:
BluetoothSocket connection = mBluetoothServerSocket.accept();
you get a BluetoothSocket back and the thread continues! Awesome eh ? Time to break out the victory smoke ?
Sadly my troubles had only begun.
The BluetoothSocket provides a standard InputStream and OutputStream for IO operations with the device, the problem was that I could read the first x bytes, formulate a response and flush it through the OutputStream and then the device would stop sending data and abort the connection. Again, profanity increased by a large amount. I spent quite a significant amount of time stepping through the code watching variables, ensuring that the response was correct and I was specifying the correct length in the headers and skipping the appropriate number of bytes. (Whilst trying to make sure that the bluetooth connection didn’t time out from the remote device’s perspective, which I can say is no mean feat.) Still no joy.
The answer it seems was simple. For some really odd reason, my responses were not being formatted correctly as I was writing variables of types short and byte to the OutputStream which for some reason were causing problems for the device to continue sending data after the initial connect op codes were sent. Wrapping the input and output streams with their respective DataInputStream and DataOutputStream classes allowed me to use the writeByte() and writeShort() methods which seemed to have corrected the problem. The device would now no longer disconnect after sending my response back.
So anyway, those were my Android bluetooth woes in a nutshell, the UUID problem was the most heartbreaking as it would had been fairly simple if there was some documentation on which UUID was for a particular profile. Part of this post is for reference for this UUID in the event that someone else needs the information and doesn’t have to spend a long time looking for it. Enjoy!
Note: The mBluetoothAdapter is an instance of the BluetoothAdapter class which can be instantised by Android using the following documentation.
Installing Trac on 64 Bit Ubuntu 8.04 (Hardy)
Mar 9th
This is a fairly basic guide which is mostly for my reference, but feel free to use it. I do not take responsibility for server failure, data loss or if your switch starts sending toast over the internet to your computer after following this guide. Follow this rough guide at your own risk.
NOTE: This guide is roughly based on a more complete guide here
Some assumptions are going to be made here.
- You have subversion installed already and your repos are in /var/svn
- You have set this up for access via apache. (Quick Guide coming soon)
- Your websites are in their own directory in /var/sites/
First install the required programs from the universe repository. If you are using ubuntu (Hardy) server, you will need to edit the /etc/apt/repos.d file.
sudo nano /etc/apt/sources.list
Enter the following lines to the end of the file
deb http://archive.ubuntu.com/ubuntu/ hardy universe deb http://archive.ubuntu.com/ubuntu/ hardy-updates universe
Save the file and now do the following to refresh the package list.
sudo apt-get update sudo apt-get upgrade
This will ensure that you have the latest and greatest of your currently installed packages to avoid any unforeseen problems. Now we need to install trac, you may get away with the following and apt will get any other dependencies for you.
sudo apt-get install trac
Or I used the command similar to the guide I saw.
sudo apt-get install enscript libapache2-mod-python python-docutils trac db4.3-util
Now once this is complete, create the directory for your trac instance and change the owner to the web server user. Depending on which distribution this could be httpd, apache or ww-data. In Ubuntu it is www-data.
sudo mkdir /var/sites/trac.yourdomain.com sudo chown -R www-data:www-data /var/sites/trac.yourdomain.com
And now we use the trac-admin program to initialise the environment, just follow the steps and remember where your svn repos are kept.
trac-admin /var/sites/trac.yourdomain.com initenv
Now to create the apache site, we are using named virtual hosts. So create the file and then open it for editing.
sudo touch /etc/apache2/sites-available/trac sudo nano /etc/apache2/sites-available/trac
Now the following site configuration will vary from person to person, the referenced guide at the start of this tutorial will show you more advanced features like self signed certificates for ssl rather than authenticating in plain text like the configuration below.
NameVirtualHost trac.yourdomain.com
<VirtualHost trac.yourdomain.com:*>
ServerAdmin adm...@yourdomain.com
DocumentRoot /var/sites/trac.yourdomain.com
DirectoryIndex Index.html
ServerName trac.yourdomain.com
ServerSignature Off
ErrorLog /var/log/apache2/trac_error_log
TransferLog /var/log/apache2/trac_access_log
<Directory "/var/sites/trac.yourdomain.com">
Options +Indexes +MultiViews
Order allow,deny
Allow from all
</Directory>
<Location />
SetHandler mod_python
PythonHandler trac.web.modpython_frontend
PythonInterpreter main_interpreter
PythonOption TracEnv /var/sites/trac.yourdomain.com/
PythonOption TracUriRoot /
AuthType Basic
AuthName "Trac"
# Use the SVN password file.
AuthUserFile /etc/subversion/passwd
Require valid-user
</Location>
</VirtualHost>
The Auth User File for subversion is generated when setting up the repos, which will be included in another guide.
Save and close the file (Ctrl + O and then Ctrl + X). Now before we start the new site we need to compile a python module (clearsilver) for python 2.5 as this is broken in hardy 64 bit. You will need the compilation tools installed and the required dependencies, which is just a single command.
sudo apt-get install build-essential zlib1g-dev python-dev autoconf build-dep python-clearsilver
Now download the latest version and extract the compressed file into a directory.
wget http://www.clearsilver.net/downloads/clearsilver-0.10.5.tar.gz tar zxvf clearsilver-0.10.5.tar.gz
Now we need to edit the configure scripts for compiling the module to allow it to build for python 2.5 so using our favourite text editor nano do the following:
nano clearsilver-0.10.5/configure.in
Now press Ctrl + W and type ‘python_versions’ and press enter.
Change the line to be the following
python_versions="2.5 2.4 2.3 2.2 2.1 2.0 1.5 24 23 22 21 20 15"
Save and close the file. Now we repeat the process with the configure file
nano clearsilver-0.10.5/configure
press Ctrl + W and type ‘python_versions’ and press enter.
Change the line to be the following
python_versions="2.5 2.4 2.3 2.2 2.1 2.0 1.5 24 23 22 21 20 15"
and Save and close the file. You can now build and install the module by doing the following
./configure make sudo make install
Now we need to copy the module binary into the python directory to replace the broken one.
sudo cp -bi python/neo_cgi.so /usr/lib/python2.5/site-packages/neo_cgi.so
Enable the trac site in apache.
sudo a2ensite trac
Now you can start the site up by either restarting or reloading apache
sudo /etc/init.d/apache2 reload
OR
sudo /etc/init.d/apache2 restart
Now surf up trac.yourdomain.com enter the user name and password (if you have set this up) and away you go!
Anyway I hope this is of some help to someone.
Special thanks to fluffypixies for the guide on fixing the python problem which can be found here.
Mobiles, Development and Tethering
Nov 17th
Well, I have decided to do some development with Windows Mobile (or Windows Phone as Microsoft seem to be wanting to call it now) as it seems to need some love in the applications arena. Also it will be nice to see what Microsoft offer up against the competition with Apple, Google and Symbian. The application I will be building will be nothing ground breaking/earth shattering/world dominating, just a little Twitter application called TwittTwoo! Probably not an original name, but that can change and I am terrible at naming things (the server is called ZeroGravitas). So there should be a good amount of swearing and pleading in the near future, which is good
November … wait, what?!
Nov 16th
October seems to have run away and already I’m half way through November! Time flies when you are having no fun at all. Much fun is indeed guaranteed in the run up to Christmas, with the Brummies in full Christmas spirit and trying to run over as many cyclists as possible. The German Christmas market is also here and the city centre is full of wide eyed first year students and everyone who owns a pram/buggy seems to think it’s a wonderful idea to take it up and down New Street.
London Midland have also kept up their consistently terrible service, cancelling trains at random with the rather lame excuse of ‘staff shortages’. It’s going to be fun getting to Shrewsbury for Christmas, I might just cycle there instead!
Looking around to see if there are any interesting groups in Birmingham or at least some sort of hobby/development/project I can work on as the usual day in day out handwriting recognition development gets rather dull after a while, plus it’s been a long time since I have done something that is rather nifty. Any suggestions are welcome. On that note, I am loving jQuery which is bundled with ASP.NET MVC. It makes writing javascript painless/bearable and the bonus is, it’s clean! Web Forms is so 2002 and I for one will be glad to see the death of it, it’s post-backs and its ridiculously hideous html and JavaScript outputs. (along with other things.)
Ah well the weekend will again grace us with a break from the week and slip away just as quickly as it arrived. On the plus side, I will be seeing Eddie Izzard on Wednesday and I am looking forward to it.
Now off to find me something to do that isn’t work related that will keep me out of trouble for now!
P.S. VB.NET must die! (along with IE 6 and FTP)
NHibernate and IIS
Oct 14th
Recently it was found that IIS throws a wobbler from time to time. More often than usual. What we are left with is the following wonderful exception message: Illegal operation attempted on a registry key that has been marked for deletion and IIS stops serving from that application untill a full server reboot occurs.
For Microsoft’s production server software this is completely unacceptable! Plus if this was on a server cluster IIS across all of your machines would slowly stop serving that application, and it would be hit or miss for any request to be directed at the problem servers!
Anyway so far the stack trace has pointed to NHibernate creating a proxy instance of an object on the fly and attempting to compile it and failing spectacularly. Sadly Google has yet to provide an answer to it and full server reboots must take place if it is caught in time. The problem is a complete nightmare, however one must wonder if other administrators out there running demanding applications on IIS have the same problem.
What would be really interesting to see is if MVC with Mono and Apache server can pick up the severe deficit IIS suffers from.

The Nexus One and Android
Mar 31st
Posted by Will in Coding
2 comments
I seem to be blogging quite a bit this month and I appear to have a lot to say!
Anyway, I took the plunge and forked out for a shiny new Nexus One from Google and bought the desktop dock so I could show off to my boss. After using it for two weeks I can only say that my experience so far has been blissful. However I will state that it has not been entirely perfect, with a few quirks along the way.
The phone feels well built with a very responsive AMOLED touch screen, which is also very nice to look at and it leaves my old Nokia E71 eating it’s dust when it comes to performance. The 1GHz Snapdragon Processor might have something to do with that.
The Android Operating system is very swish and fluid, using it is very simple however sometimes drilling down to the settings is a little tedious. When I set-up the phone, I entered my Google Account details and before I could finish a cigarette all my contacts, e-mail and calendar were synced with the phone and any updates on the handset would propagate back to Google. Awesome. Next I set-up Facebook which also synced with my contacts adding addresses, e-mail addresses and profile pictures. At this point I was having to calm myself down with the sheer amount of awesomeness that this phone was delivering.
I have yet to get the phone to crash, freeze or slow down even slightly and I sync my Google, Facebook, Twitter and my work information along with running games, weather and news widgets, there seems to be no end to what this phone can do. I have apps for going walking in the country, to augmented reality browsers to find things that I need and even an app to scan bar codes and purchase the item on ebay.
I did run in to a problem when I switched networks from three to O2. The short story for this was because three was:
Anyway when my O2 SIM arrived, I popped it in the phone and found that the cell reception was utterly rubbish and the 3G connectivity non existant. I had activated the SIM and spoke to technical support several times. However I could not seem to register on the cell network for more than five minutes before being disconnected. It seems that the Nexus One stores the network that you are registered to and doesn’t offer a clear option to change the default network. I ended up doing a factory reset and seeing as all my settings and data were backed up to Google’s servers, I was up and running again within five minutes with an awesome cell reception and stable 3G connection. Goodbye three! Despite being a loyal customer, you took advantage and insulted me, so I wont be missing you.
Another minor annoyance is answering calls. Sometimes when the phone rings the screen does not wake up, which means I have to press the top button and then swipe to answer, which sometimes does not register for what ever reason. I would like to make answering calls less fiddly. Obviously the main complaint is the battery life. If you run everything and play about a lot, which is natural when you get a new toy, the battery will last around a day, which when compared with the Nokia E71 is just appalling. My solution was to have the auto brightness adjustment on, I turned off my 3G APNs with APN Droid and can manually enable them when I want mobile internet and I usually turn off GPS and Bluetooth until they are needed. So far, Including taking/making calls, reading e-mails, checking Facebook sending and receiving SMS and listening to music to and from work the battery will go down to around 74% after a day which is far, far better. I decided to give the music player a test run and went into Birmingham on the bus which is 45 minutes each way. I spent around 3.5 hours listening to music and by the time I got home the battery was at critical ( < 15% ) which disappointed me slightly as it rules out the phone for use on long distance journeys as a music player.
After a week or so, I got curious about the Android SDK, so I decided to take some time out and have a quick peek at what was going on. Turns out that Android applications run on the Dalvic virtual machine and the code is all Java. So a quick install of the ADT plugin for my device emulators, a quick install of Eclipse and Subclipse (for Subversion) and a download of the Android SDK and I was nose deep in documentation.
It turns out that Developing Android Applications is very easy and uses a similar paradigm as MVC for web applications. So I spend last Sunday tinkering around and wrote my first proper application after Hello World! which was Droid Partridge. It is a very simple application which plays Alan Partridge’s classic moments and has a dialog box. I eventually released this on to the world via the Android Marketplace, so others can download and install and enjoy the pocket comedy for those socially appropriate moments where “Jurassic Park!” is needed to be said out loud. Over the next few weeks I will be updating this application to include more sounds with better compression and iron out any bugs.
Developing on Android was fun and quick, however the tool kit does have some drawbacks. The interface designer which creates the view layouts in XML is rather lacking and is not quite up to the standard that VE (Visual Editor) was for Eclipse all those years ago, nor is it up to scratch with the WPF / Silverlight visual designer. (which also defines it’s interfaces in XML) It is a shame really as that part makes the whole process much more difficult for newcomers who just want to define a very basic UI to play around with.
Another drawback is the mobile device emulator itself, on a Quad core machine with 4GB of ram (My development machine at work) it is still as painful as trying to run through a swimming pool of treacle, it is so terribly slow! Also if the program does throw an exception during runtime, I often get the ‘Source Not Found’ window open up in the debugging perspective and I can only hazard guesses where it has gone wrong from the not too helpful stack traces that seem to have no mention of any calls to my code.
Despite this, I am quite committed to keep on tinkering with Android. True, I did pay $25 USD for the Android Marketplace account, but it is also a very nice development experience, very much unlike Windows Mobile development. I have a few nifty ideas that I want to try, for which I have yet to see a solution for and also a couple of colleagues have thrown suggestions at me so at least I have something to keep me out of trouble and off the streets of Meriden!
A while back I did mention that I was developing a Windows Mobile Twitter application, that has been abandoned completely as I’ve jumped on to the Google bandwagon and riding off in to the sunset to get as far from Microsoft and it’s stagnation as possible. I just hope that for all our sakes, that Google stick to their “Don’t be evil” ideals.
P.S.
Just in-case you didn’t notice, like the new theme ?