Detecting presence of particular bluetooth device with MAC address

user1117057 picture user1117057 · Dec 10, 2014 · Viewed 14.1k times · Source

my ultimate goal is to allow my raspberry pi detect when my iphone or pebble watch is nearby. I am presently focusing on the pebble as I believe iphone randomizes the MAC address. I have the static MAC address of the pebble watch.

My question is how to detect the presence of the MAC address through bluetooth?

I have tried hcitool rssi [mac address] or l2ping [mac address] however both needs a confirmation of connection on the watch before any response. I want it to be automatic...

I also tried hcitool scan, but it takes awhile, presumably it is going through all possibilities. I simply want to search for a particular Mac Address.

EDIT: I just tried "hcitool name [Mac Address]" which return the name of the device and if not there it returns a "null" so this is the idea... is there a python equivalent of this?

I am new to python, so hopefully someone can point to how I can simply ping the mac address and see how strong the RSSI value is?

Answer

Martijn Thé picture Martijn Thé · Dec 10, 2014

Apple iDevices do use private resolvable addresses with Bluetooth Low Energy (BLE). They cycle to a different address every ~15 minutes. Only paired devices that have a so called Identity Resolving Key can "decipher" these seemingly random addresses and associate them back to the paired device.

So to do something like this with your iPhone, you need to pair it with your raspberry pi. Then what you can do is make a simple iOS app that advertises some data (what does not matter because when the app is backgrounded, only iOS itself gets to put data into the advertising packet). On the raspberry pi you can then use hcitool lescan to scan for the BLE advertisements. If the address of the advertisement can be resolved using the IRK, you know with high certainty that it's the iPhone. I'm not sure if hcitool does any IRK math out of the box, but the resolving algorithm is well specified by the Bluetooth spec.

Pebble currently does indeed use a fixed address. However, it is only advertising when it is disconnected from the phone it is supposed to be connected to. So, for your use case, using its BLE advertisements is not very useful. Currently, there is no API in the Pebble SDK to allow an app on the Pebble to advertise data.


FWIW, the commands you mentioned are useful only for Bluetooth 2.1 ("Classic") and probably only useful if the other device is discoverable (basically never, unless it's in the Settings / Bluetooth menu).