Pi, Bluetooth, R and Twitter…. gotta be fun in here somewhere.

Yup I found it.  Scan and tell who’s discoverable. No use whatsoever to the outside world (that I’m aware of) but the basis of some funky retail marketing startup who think Bluetooth might be the way forward.

170px-Bluetooth.svg

Installing Bluetooth

First of all if you haven’t got a Bluetooth dongle get yourselves to Tesco and buy their little Technik nano bluetooth dongle for a fiver. Works a treat.

Back to the Pi…

apt-get install bluetooth

Have a couple of games of Mario Kart or make a drink while it installs. Could take a while.

Twitter OAuth in R

If you were reading the Pi Twitter Sentiment Server posts then you’ll have all this set up. If not you can read it here.  One thing you need to do is make sure your Twitter developer app can read/write to Twitter.

Scanning Bluetooth Devices

From the command line all you need to run (as root) is:

hcitool scan

And it will produce a list of the MAC address and the name of the device.

We can run within R….

So, all together.

load("/path/to/credentials/RData")
registerTwitterOAuth(cred)
t<-try(system("hcitool scan", intern=TRUE))
ts<-strsplit(t,"\t")
tweet(paste("Test BT Scan: ",ts[[2]][3]))

The strsplit command will give you a two dimensional vector with three elements: space, MAC address and the discoverable name.  So [[2]][3] means give me value of the second row and third column of the vector.

jbbluetooth

Taking it from here.

Plenty of scope from here to save the MAC addresses to a datastore so the same names don’t keep appearing everytime the scan happens.  Also if you read back on previous posts you’ll see it’s easy to knock up a cron job to run this every ten minutes or so.

It looks like the hcitool caches the scan for at least 30 seconds so there’s no use pounding the scanning for new devices for any duration less than that.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s