iBeacons – Let your Mac broadcasts

iBeacons are built on top of Core Bluetooth so I wondered whether it would be possible to use a MacBook running Mavericks to create an iBeacon transmitter. As iBeacon is just the name Apple gives for using Bluetooth 4.0 Advertising mode with a specific data format. So transmitting iBeacons should not be very difficult.
A Bluetooth 4.0 Advertising packet consist of the following information:

  • Preamble – 1 byte. Always 0xAA
  • Access Address: 4 bytes. Always 0x8E89BED6
  • CRC: 3 bytes checksum calculated over PDU
  • PDU (Protocol Data Unit): 39 bytes

The first three parts of the packet are handled by the Bluetooth Stack, so we should care about what information it is being sent in the Protocol Data Unit. Inside PDU there are some fields reserved for information, including Advertising channel, Manufacturer Specific Information, and so on. But the last 4 fields are of our interest. They are:

  1. Transmitter UUID (16 bytes)
  2. Major number (16 bit uint)
  3. Minor number (16 bit uint)
  4. Measured power (1 byte encoded as U2)

Major and Minor numbers help us differentiate between transmitters with the same UUID.
Matthew Robinson created an app for OSX called BeaconOSX that uses Mac’s Bluetooth 4.0 to transmit iBeacons, and a very simple class where it is easy to see how to encode information into the PDU and broadcast it.

- (NSDictionary *)beaconAdvertisement {

NSString *beaconKey = @”kCBAdvDataAppleBeaconKey”;
unsigned char advertisementBytes[21] = {0};
[self.proximityUUID getUUIDBytes:(unsigned char *)&advertisementBytes];
advertisementBytes[16] = (unsigned char)(self.major >> 8);
advertisementBytes[17] = (unsigned char)(self.major & 255);
advertisementBytes[18] = (unsigned char)(self.minor >> 8);
advertisementBytes[19] = (unsigned char)(self.minor & 255);
advertisementBytes[20] = self.measuredPower;
NSMutableData *advertisement = [NSMutableData dataWithBytes:advertisementBytes length:21];
return [NSDictionary dictionaryWithObject:advertisement forKey:beaconKey];

-(void)startAdvertising {

_manager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
NSDictionary* beaconAdvertisement = [self beaconAdvertisement]
[_manager startAdvertising:beaconAdvertisement];
More information about Bluetooth Advertising mode can be found at: https://www.bluetooth.org/en-us/specification/adopted-specifications

How Android sees an attached USB Device

In the long way that my project must walk, I decided to start testing some small things in order to understand how the Android USB Host API and Open Accessory API (two different things) work.
Basically the USB Host API lets your Android device acts as a computer. With this API you can connect from a Bluetooth USB adapter to any standard USB device that you can find in a store. The Open Accessory API works the other way, your Android acts as a device, so you have to connect a USB Host controller in the other side. I don’t find this second approach very useful. First because not all the microcontrollers in the world support USB Host (Remember, your Android is a Device for the Open Accessory API) Second because it does not allow seemless development of a product using a computer and a natural port to Android using the USB Host API. I still cannot find three nice examples of when I will prefer to use my Tablet as a USB Device, but I can find millions of application for my Tablet (and Phone) to be a USB Host.
So in this short post, I will show you how it a USB Device attached to a  Honeycomb-enabled device looks like. Take special attention to the fact that even though Android 2.x has an option USB toolkit, and the classes have the same name, they belong to a different package (so the imports are not the same).
The Motorola Xoom with Android 3.2 is USB Host capable, and even thought I connected a USB keyboard and it didn’t work in the OS, the following short example demonstrates that the Motorola Xoom detects a USB Bluetooth adapter.

When attaching the USB Bluetooth adapter the following message can be seen using “logcat” (connected via TCP as the USB port is busy with the adapter)


Bluetooth in pre-Android 2.0 devices

Android OS introduced Bluetooth as an open API in version 2.0, but early versions like the well known Donut (1.6) and, one of the most famous in Motorola devices, Cupcake (1.5) included Bluetooth support in the OS for hands free communications, files exchange and communication with PCss (and Macs).
Of all projects around the web that enable Bluetooth in pre-2.0 devices, I liked android-bluetooth (http://code.google.com/p/android-bluetooth/) very much. Basically because it was design for compatibility with 2.x releases of Android. This was archived by keeping an API as similar as the official one but changing the package name of the classes (Android does not allow 3rd parties classes inside android package).

It is very important to check if the Android devices allows Bluetooth comms, either because the module is not present or because it is not available. For doing so, just use this piece of code:

If everything goes right, you have to register for broadcast messages like “discovering devices”, “discovering finished”, “did finish connection with device” and so on. That is very easy to do in Android

and then, it is necessary to implement the Broadcast interface

Some other configurations are needed by this framework, including a configuration file and registering receivers and activities in the Android Manifest. For a complete working example check out my BTExample project.