Monday, January 28, 2013

Bluetooth


What a mess.

But finally, it works.

Yeah, yeah, I know, it's not much of a screenshot gallery, but I figured it was better than nothing! I probably should have made a small video clip, but since the only video recording device I currently possess happens to be the very phone that I want to tape, you'll have to make do with the images below.

When you tilt the phone by different angles, the server simulates a movement of the robot accordingly. Looks simple, sounds simple, but was hell to make!
Starting the server...
Connection established, client side
Moving forwards to the right!
Connection established!
Moving forward to the right!

I am currently taking a class called Electronics Project, which is just about what it sounds like. We're working in groups of five to seven people and each group gets an assignment that has to be finished before the summer. All of the assignments are about building robots of some kind. Our robot will be one that shall find its way through a labyrinth, guided by stickers on the ground. Somewhere in there it will find an item, pick that item up, and autonomously (without stickers guiding it!) bring it back out of the maze. That means, it needs to remember where it came from.

Anyway, before we get to the point where the robot can actually orient all by itself, we need a means to control it manually. And this is where Bluetooth comes in. It's mandatory to use this very protocol for whatever data is to be transmitted between the robot and the controller.

So, what this is, is an Android app that can be used as a mobile stand-in for the computer that will otherwise control the robot, and a robot simulator. I figured we could use these two as debugging tools when we create the real thing later on.

Man, has this been a challenge! I have made some very (very!) simple Android apps in the past, so at least I knew where to begin, and I did know that Google has tried quite hard to make things as easy as possible for you to program Android, so I figured that this Bluetooth thing shouldn't be much of a headache.

Wrong.

It would probably have been much easier had I stuck to the same platform for both the client and the server, but since Bluetooth is such a widely supported standard I thought that shouldn't be an issue. And besides, since the robot uses Atmel megaAVR computers that are to be programmed in C, I hoped that having done the server in C once would be helpful once time comes to program the actual robot. I've realized afterwards that this might not be true, since the APIs are completely different in each implementation. And I have as of yet no idea how much of a library is provided on an AVR...

Anyway, the simulator is running on a Linux host. It uses the BlueZ stack for Bluetooth communication and ncurses for the very beautiful user interface.

BlueZ is a very complete Bluetooth stack. Only problem is the severe lack of documentation! There are quite a few tutorials on the Internet to get you started, but once you want to dig deeper into the return values of the more obscure functions and the less commonly used arguments of its helper functions, you're on your own. I know, the source code is available, but I'm really not confident enough in C to go there.

Then I found Bluetooth Essentials for Programmers by Huang and Rudolf. I would never have made it without this book! Everything is laid out

It also turned out that the Andriod Bluetooth API wasn't all that complete. And it also had its fair share of undocumented methods. For some reason I do not yet understand, the SDP requests always failed, and I was forced to use one of those undocumented methods (createRfcommSocket(int channel);) in order to bypass SDP completely and open a RFCOMM connection on a specified channel directly. Not a very beautiful solution. I'll have to find out a better solution to this before the end of the project.

I'll post more of the technical details later on my website, including the source code of all the software involved, but for now I just wanted to make a quick demonstration of what I have done so far.

David

Tuesday, January 15, 2013