read

Following the instructions here I decided to try turning my Pi into an iBeacon.

According to the artical most Bluetooth 4.0 dongles should work so I ordered the first one I found:

dongle

And once it arrived plugged it in:

pi and dongle

To get set up we need to install some stuff:

Make sure everything is up to date

sudo apt-get update
sudo apt-get upgrade

And then install the bluetooth package (this will take quite some time as it installs a lot of stuff):

sudo apt-get install bluetooth

Restart your pi and see if your dongle is detected.

lsusb
hcitool dev

Sadly my device shows up with lsusb, but doesn’t show up in hcitool. Forcing it to load makes things work:

sudo su
modprobe btusb
echo "050d 065a" > /sys/bus/usb/drivers/btusb/new_id 
hcitool dev

Devices:
	hci0	00:02:72:C5:DE:61

To get my device to be recognised at boot time and on demand when it’s plugged in we need to add some udev rules - I found a nice guide here.

Create a new file /etc/udev/rules.d/95-belkin-bluetooth.rules

# Rules for hotplugging belkin low power bluetooth 
SUBSYSTEM=="usb", ATTRS{idVendor}=="050d", ATTRS{idProduct}=="065a", RUN="/etc/belkin_bluetooth.sh"

And another file /etc/belkin_bluetooth.sh

#!/bin/sh
modprobe btusb
echo 050d 065a > /sys/bus/usb/drivers/btusb/new_id

This file needs to be executable by root.

Now you’ve got your dongle working you can turn it into an iBeacon!

sudo hciconfig hciO up
sudo hciconfig hci0 leadv
sudo hciconfig hci0 noscan
sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00 00 00 00 C8 00

This will create an iBeacon with the uuid E20A39F4-73F5-4BC4-A12F-17D1AD07A961 and major minor version 0,0.

To monitor for this iBeacon on you would use the following code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"E20A39F4-73F5-4BC4-A12F-17D1AD07A961"];
    self.beaconRegion = [[CLBeaconRegion alloc]                                    
                                    initWithProximityUUID:uuid
                                    identifier:@"test"];
                                    
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager
        didRangeBeacons:(NSArray *)beacons
               inRegion:(CLBeaconRegion *)region {
    
    if ([beacons count] > 0) {
        CLBeacon *nearestBeacon = [beacons firstObject];
        
        self.beaconInfo.text = [NSString stringWithFormat:@"%fm",nearestBeacon.accuracy];
        
        NSLog(@"%d", [nearestBeacon.minor intValue]);
    }
}

It works! (not the most exciting app I admit…)

test

Why not subscribe to my mailing list - I'll send out regular emails with new content (don't worry I won't spam you!)

* indicates required
Blog Logo

Chris Greening


Published

Image

Chris Greening

Blogging about random stuff

Back to Overview