Proximity Detection Perfection: Bluetooth + Asterisk + iPhone

If impressing your friends happens to be your thing, then you'll enjoy watching them all wet their pants today after you show them this little trick. It's been almost two years since we first introduced Bluetooth Proximity Detection to the Asterisk® community. Because we can't seem to get enough programming torture lately, we decided to revisit Follow Me Phoning by taking it to a whole new level with an all-new set of devices. If you're new to all of this, what we're talking about is the ability to stroll out of your home or office and have your telephone calls follow you on your cellphone automatically... without touching anything. And, when you return, the home or office phones start ringing again just as if you never left. Won't your boss or spouse be thrilled? What makes all of this possible, of course, is Bluetooth which happens to be running on your TrixBox/Asterisk server and on your brand new iPhone.

Since our original article, TrixBox has replaced Asterisk@Home as the leading (free) turnkey Asterisk system. freePBX has replaced the Asterisk Management Portal as the best Asterisk shell on the planet. And, of course, the iPhone has blown the cellphone industry out of the water. For long-time readers of our column, you may recall that we whined that the Treo 650 used in the original article was lousy for proximity detection because you always had to turn the display on to activate Bluetooth. It also drained the battery quickly which is probably why Palm left it turned off most of the time. You won't have either of those problems with the iPhone. The iPhone Bluetooth Adapter is always broadcasting away assuming you have turned it on. And we left the iPhone near our Asterisk server for 8 hours with virtually no battery drain on the phone. So, yes, it's damn near perfect for this task... and so many others.

NOTE: This article has been updated. For the current article, click here.

Our plan today is simple enough. We want to design a phone system so that, when you walk into your home or office, the phones ring in the office when there's an incoming call. And, when you walk out of your home or office carrying your iPhone with Bluetooth, calls to your home or office extension ring on your iPhone. If you're using one of our turnkey TrixBox systems at the top of this page, this project should take you about 20 minutes to complete once you have a dLink DBT120 USB Bluetooth Adapter in hand.

System Requirements. As mentioned, you'll need a Linux-based Asterisk server (we're working on Windows and Mac support), and we recommend our turnkey system because it has most of the software you'll need for this project preinstalled. That includes loads of Linux utilities, the Linux Bluetooth software toolkit, freePBX, Asterisk 1.2.x, and WebMin. But any TrixBox server will work. Other than your Asterisk server, the only other hardware you'll need is a dLink DBT120 Bluetooth Adapter. They run $30-$40 from most electronics stores although many retailers put them on sale regularly. CentOS 4.2 and above (used in the TrixBox builds) comes with the Bluetooth software preinstalled. If you're using some other Linux flavor, consult our original article for installation instructions.


DBT120 Bluetooth Adapter Installation. Boot up your Asterisk server if it is not already running and log in as root. Plug the DBT120 into an available USB slot. Now issue the following command:

/etc/init.d/bluetooth restart

You'll probably be told that your system couldn't stop bluetooth (because it wasn't running) and then it'll restart. Now let's make sure everything is running that should be:

/etc/init.d/bluetooth status

You should see messages that look like this:

hcid (pid somenumber) is running ...
sdpd (pid somennumber) is running ...

If you're alerted that some other application isn't running, we don't care. Now let's be sure the system has found your bluetooth adapter:

hcitool dev

Assuming you get a response telling you the system found device hci0 with the MAC address of the adapter, you have successfully installed your DBT120. So it's time to move on.

Configuring Linux Bluetooth Software to Start Automatically. You don't want to have to manually start up your Linux Bluetooth software each time you reboot your server. The easiest way to automatically start it is to use WebMin which comes preinstalled with our TrixBox builds. While logged in as root, make sure WebMin is running:

/etc/webmin/start

Then point your web browser to port 10000 on your Asterisk server: http://192.168.0.108:10000/. When prompted for username and password, type root and the password you assigned the root user on your system. From the Main Screen of WebMin, click the System button and then the Bootup and Shutdown link. Find bluetooth in the list of applications and click on it. The Action Details screen should show that bluetooth is running. Now click the Yes button beside "Start at Boot Time?" and then click the Save button to reconfigure your server. That wasn't hard, was it? Go back to the command prompt on your Asterisk server and stop WebMin by issuing the following command: /etc/webmin/stop. No need to waste processing cycles for a tool we're not using.

Deciphering the MAC Address of Your iPhone Bluetooth Adapter. We're going to be communicating with your iPhone to determine when you're in and when you're out. In order to do that, we need the MAC address of the iPhone Bluetooth Adapter. Here's how to find it. Move your iPhone within 10 feet or so of your Asterisk server. Then put your iPhone in Bluetooth Discovery Mode by choosing Settings, General, Bluetooth from the Main Screen on your phone. Be sure Bluetooth is set to ON. Once you've done that, the iPhone will report that it is Now Discoverable. Put the iPhone down near your Asterisk server. Now jump back to your Asterisk server console and issue the following command. Try it several times until you get the MAC address of your iPhone's Bluetooth Adapter:

hcitool scan

The system will whir away for a few seconds and then will report back the Bluetooth MAC address and name assigned to the adapter. It's probably your name. Write both of them down. We'll need the MAC address in a minute.

[root@asterisk1 ~]# hcitool scan
Scanning ...
    00:1D:64:C9:58:BA      Ward Mundy

Proximity Detection Design. Now we've got all the hardware information we need to make proximity detection work. We'll download the Proximity Detection Software in a minute.But first, sit down with a pencil and write down the other information you'll need to configure the Proximity Detection Software. To make the software as flexibile as possible, we've reworked the code a bit since the original article. With the new code, it's possible to manage multiple extensions of multiple people with multiple iPhones. So what you'll need is the extension numbers of the people that want to use this and the iPhone numbers of those people. For example, you may want to forward extension 200 to 6782345678 and extension 202 to 6783456789. Just make sure that the forwarding numbers are in the correct format for the default outbound dialing rules on your Asterisk server. If your server expects numbers to always begin with a 9 or a 1, be sure to include it in the dial string, or the calls won't be completed when they are forwarded. Obviously, you'll also need the MAC address for each of your iPhone Bluetooth Adapters so just repeat the drill above with each iPhone until you have all of the addresses. Finally, you'll need to assign an 8-character (or less) name to each user. So make yourself a nice little chart:

WARD 00:1D:64:C9:58:BA 200 6782345678
MARY 00:2D:54:C9:59:AB 201 6783456789

Today's installment assumes you are using a single Asterisk server both for your phone system AND proximity detection. We're still working on a multi-server solution. The only drawback with the current design is that the iPhones need to be placed close to that server when you arrive at your home or office. You can experiment on the distance the iPhones can be away from the server. Different Bluetooth adapters and iPhones have slightly different ranges. The bottom line is you always want to leave the iPhones close enough to the server with the Bluetooth adapter so that the proximity detection works reliably all the time. And, yes, we know it would be nice to be able to do all of this using a VMware or Parallels build of TrixBox running on a Windows or Mac desktop. Then you could detect the iPhone at the actual desktop where you hang out most of the time. We're working on it, but we just couldn't get it done for today's article. Stay tuned!

Proximity Detection Software Installation. All that remains to be done is to download and configure the proximity detection script and then put it in motion on your Linux machine. Log into your Asterisk server as root and move to the /root directory to download and unzip the script:

cd /root
wget http://nerdvittles.com/trixbox123/proximity.zip
unzip proximity.zip
chmod +x proximity

If you're going to be setting up proximity detection for multiple people, just make copies of the proximity script, e.g. cp proximity proximity1. Then edit each of the scripts and fill in the data from the little chart you made: nano -w proximity

deviceuser=WARD
devicemac=00:4B:63:D5:62:AB
myextension=200
mycellphone=6783456789

Save your changes and exit the editor: Ctrl-X, Y, then Enter.

Setting Up the Crontab Jobs. The last step is to set up a crontab entry for each script so that it gets run once a minute during whatever hours each day you want to monitor your iPhone. While still logged in as root, edit /etc/crontab: nano -w /etc/crontab. Insert a line like the following at the bottom of the existing file. This code would monitor your iPhone from 6 a.m. to 9 p.m. each day. To monitor your phone 24 hours a day, replace 6-21 with an additional asterisk.

* 6-21 * * * root /root/proximity > /dev/null

Save your changes and exit the editor: Ctrl-X, Y, then Enter. Repeat the drill for the other phones you want to monitor substituting the correct script names, and you're done.

Incidentally, if you ever want to disable the Proximity Detection System, just edit the crontab file and comment out the lines you want to disable by inserting # at the beginning of the line(s). If it happens that your calls are still being forwarded to your iPhone, you can cancel the forwarding from any Asterisk extension by dialing *74. Enjoy!


Nerd Vittles Demo Hot Line (courtesy of les.net). You now can take a number of Nerd Vittles projects for a test drive... by phone! The current demos include (1) MailCall for Asterisk with password 1111 (retrieve your email by phone), (2) NewsClips for Asterisk (latest news headlines in dozens of categories), (3) Weather Forecasts by U.S. Airport Code, and (4) Weather Forecasts by U.S. ZIP Code. You're not prompted for #4 yet, but it does work! Just call our number (shown in the left margin) and take any or all of them for a spin. The sound quality may not be perfect due to performance limitations of our ancient Intel 386 demo machine. But the price is right.

Nerd Vittles Fan Club Map. Thanks for visiting! We hope you'll take a second and add yourself to our Frappr World Map. In making your entry, you can choose an icon: guy, gal, nerd, or geek. For those that don't know the difference in the last two, here's the best definition we've found: "a nerd is very similar to a geek, but with more RAM and a faster modem." We're always looking for the best BBQ joints on the planet. So, if you know of one, add it to the map while you're visiting as well.


Some Recent Nerd Vittles Articles of Interest...

Be Sociable, Share!

21 Responses to “Proximity Detection Perfection: Bluetooth + Asterisk + iPhone”

  1. Here we go, another great use for the magnificent iPhone, and the very best and most flexible PBX in history, AND I CANNOT USE IT! I hate Puerto Rico’s ATT infrastructure that cannot support the iPhone’s data requirements! If only I could… Does anyone knows if you can get free roaming in Puerto Rico with an iPhone plan???

    Oops, I got carried away, sorry!

    Keep up the great articles Ward!

  2. Kamache says:

    Sorry guys but seems not working…

    Looks like the .last file was not created.

    Best regards;

    Kamache

    [WM: Try again. Sorry. Downloads before 4 pm on July 19 probably won’t work. But all is well now. Rule #1: Never publish new software on your way out of town for a long weekend.]

  3. Charlie Pearce says:

    Thanks for your continuing useful articles and software. I’m new to Asterisk -and everything I read here gives more exciting ideas to try.

    However I have a couple of questions – first, I presume this system will work with other bluetooth phones – or is it just the iPhone? It’ll be a while before I can afford to get one here in the UK!

    Also, why use Webmin to start bluetooth services at bootime? As TrixBox is based on CentOS then wouldn’t the command “chkconfig bluetooth on” set bluetooth to start in the current runlevel? Though I guess this would only work for RedHat based distros such as TrixBox.

    Though unfortunately this will all be no good to me as I run TrixBox on an old Compaq Proliant server -the howl it makes forces me to keep it running in the out-house, which is too far away for Bluetooth. Shame, but maybe I can rig a serial-line connected, weighted switch to my key holder….. but that’d probably be going too far! Thanks again.

    [WM: Any Bluetooth phone that keeps Bluetooth activated all the time should work fine. Thanks for the RedHat activation tip. We use WebMin for almost everything out of stupidity more than anything else. Good to learn something new.]

  4. KatmaiAD says:

    Actually,won’t going to Settings > General > About and scrolling down give you the Bluetooth MAC address a little more easily?

    [WM: Good tip. Thanks.]

  5. Adam says:

    I got this working with a blackberry 8703e. Thanks for the great articles. On to the next app!!!

  6. Chris Cantey says:

    Will this only work with the D-Link Adaptor? Our could I use one of the Class I (100M) adaptors such as the TRENDnet or Billionton one?

    [WM: Try it and let us know.]

  7. AG says:

    Very very nice. Now I need to go pick up a BT adapter.

    Brainstorming – This would scale somewhat easily, just take a small bit of additional coding, ssh keys between multiple servers, and scp.

    Have a remote server with good bluetooth coverage do this pseudocode:
    hcitool name $devicemac > /tmp/$deviceuser.remoteserver
    if $deviceuser.remoteserver != $deviceuser.remoteserver.last;
    then scp $deviceuser.remoteserver asteriskserver:/tmp ; fi

    Then have the asterisk server compare $deviceuser.remote* with $deviceuser.last

  8. Jody says:

    Has any one had any success in getting bluetooth proximity to work if the bluetooth dongle is not connected directly to the server. My Asterisk server is in an office in a secured server room with multiple SIP extensions. I would prefer to not have my users sitting in the server room :-)

    I can see comment 7 is directed to achieving this, does anyone know of an available guide with a little more information to do this as well as anything similar for a windows desktop?

    Also, Ward, your guides are second to none, keep ‘em coming.

  9. Tim P. says:

    This works great on the Palm 650. I bought a “Extended Range” $2.99 bluetooth adapter off ebay and I get a good 40 feet, tied this into the home automation X10 so my lights and call forwarding are automatic. Thanks!

  10. Tim P. says:

    Forgot to mention about the bluetooth on two servers, I used the proximity script to execute a bash script that connects to my home automation box over a passwordless ssh connection and executes a bash script on the home automation box that controls the x10. I guess this is a cheap fast way to get either dual servers or a single server not directly connected :-)

  11. Dave says:

    I decided a slightly different approach would make sense. Once a minute, the job checks the status file to see if the text has changed. If it has and the phone is OOA, it telnets to my asterisk server management port and marks my phone DND. Since the DND process is used in follow-me, the phone automatically does the right thing. Note that this convention could be used for LOTS of different applications (when I go to another office, by cell phone tells a local server (anything with Bluetooth) that I am there, and then contacts the Manager API to set the forward on and to forward my calls to the phone in the local network. When I’m gone again, it turns off the forward. If the DND is still set, the phone automagically goes back to my cell until I get back home.

  12. Carey says:

    I’m wondering if someone can tell me why this doesn’t work properly with more than one BlueTooth phone.

    If you have two BT phones set up using separate proximity files in root, with completely different user name, mac address, extension and forward numbers, and you have the proximity files listed separately in the crontab file, they will both be forwarded at the same time and they will both contain the same BlueTooth device name in the user.now files.

    * * * * * root /root/proximity_user1 > /dev/null
    * * * * * root /root/proximity_user2 > /dev/null

    If you enter the proximity files like the following in crontab it will work correctly, with each user.now file having it’s correct BT device name and each user being forwarded individually:

    * * * * * root /root/proximity_user1 && /root/proximity_user2 > /dev/null

    Has anyone else tried this with more than one BT phone? If so did you have the same results?

    Can anyone explain to me how and why having a similar script with different user data entered separately in crontab, to run at the same time, can seemly get the same output?

    I’m a Windows guy still learning a lot about Linux, so I’m obviously missing something.

    Thanks for the feedback.

  13. Rajj says:

    can i use asterisk on my Iphone now ? mean SIP calling on IPHONE. wow.. Thx….

  14. Yoav Aner says:

    I’m using a slightly different set-up. The bluetooth dongle is on my desktop and Asterisk is being updated remotely via ssh. I’ve been using BlueProximity tool to essentially switch off my voip phone and redirect to voicemail when my mobile is switched off / or bluetooth disabled.

    See http://blog.gingerlime.com/guilty-pleasures

  15. ralph says:

    I have 2 nokia phones and I setup both of them . My problem is that the phones ring but both of them rings when the caller chooses the second extension.

  16. Riz says:

    Using this, would it be possible to direct calls coming to the Iphone to an extension on my PiaF system, conversely use the IPhone as a trunk to make outgoing calls from extensions?.

  17. mike says:

    Has anyone had an issue with the iPhone bluetooth signal going to sleep when the iPhone does??

  18. derek says:

    mike, i’ve had no problem, been using a similar setup for 1+ years. i use it for controlling motion detection cameras in a home, disabling when i am in range.

  19. rob says:

    Is this still current for a PIAF-Purple download ?

    [WM: Have you tried it?]

  20. Vincent Emma says:

    I am extremely impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you customize it yourself? Either way keep up the excellent quality writing, it is rare to see a nice blog like this one nowadays..

  21. Frank says:

    Maybe it is just me, but I don’t know how useful Bluetooth is with its very limited range. Wouldn’t Wifi work much better. This will obviously mean using a Wifi Adapter, but this will solve the range issues as Wifi has a much larger area, and can be expanded with extenders. This is just my thought.

    [WM: The whole idea is to detect when you arrive at and depart from your desk. So the short range is actually part of the design.]

Ringbinder theme by Themocracy