Wednesday 3 July 2013

What I learned about bluetooth with Gadgeteer

The Project

My recent Gadgeteer project was to investigate the Bluetooth module with a view to having a hassle-free way to communicate either between 2 Gadgeteer devices or between Gadgeteer and a Windows client.

I decided the best way to learn how the bluetooth module worked was by using Gadgeteer -> Windows, as it would make debugging easier. I came across the Bluetooth library from 32feet - this is a lovely layer of abstraction over the complexities of the bluetooth protocol, and ultimately gives you access to any serial data via a NetworkStream.

The goal of this experiment was to take a picture with the camera, convert it to a windows-compatible bitmap, and stream that over bluetooth to the client, which would create a bitmap from the bytes and display it on the screen. What could be easier?

This is the gadgeteer project:
The Windows client was not fancy; it consisted of a form with a picture box and progress bar.

What I Learnt

  • You should chunk your data. After some experimenting, I found the most reliable way to transfer a 320x240 pixel bitmap (totalling 230,454 bytes) was to split it into 93 chunks of 2,478 bytes each. I hadn't been able to transfer more than around 8k successfully, and my cut-off for reliably transferring data was around 3k.
  • Base64 encodings change between implementations. Don't assume that the .Net Framework will be able to unencode a Base64 string that was encoded on the .Net MicroFramework; I haven't got to the bottom of this one yet; there'll be another post when [if] I do. ** UPDATE ** I think this was how I was using the Base64 data when I received it, I was trying to extract chars rather than bytes.
  • Bluetooth isn't quick. Transferring a 230k bitmap takes around 90 seconds.
  • The Gadgeteer Bluetooth module needs time to warm up. Standard operating procedure for the bluetooth module is to create a timer that fires after 3 seconds of the modules initialising, so it can then enter pairing mode.

The Code

The code is available on Github. The Gadgeteer code is here and the windows client is here

The Result

Here's a picture showing the Gadgeteer LCD screen and the windows client side-by-side; you'll have to take my word for it that the image was bluetoothed...

No comments:

Post a Comment