This post explores my attempt to get bidirectional bluetooth communication going between an android device and a linux computer (in this case a Raspberry Pi running Raspbian). This post is part of the documentation of knowledge acquired while building an “AquaPi” – a raspberry pi that will act as a control center for an aquarium. Here is a video showing a working example:

https://www.youtube.com/watch?v=hvykOO73LbM



Hardware:

Android 4.3 based tablet/phone

Raspberry Pi running Raspbian

Belkin Bluetooth dongle (http://www.mytrendyphone.co.uk/shop/belkin-bluetooth-dongle-35083p.html)

Software:

On the raspberry pi: setting up and using pybluez

Note: these instructions have also been confirmed to work on an ubuntu workstation with in-built bluetooth support.

First, plugin the bluetooth dongle and fire up the raspberry pi. Next, install the pre-requisites to get bluetooth support on the raspberry pi by running:

sudo apt-get install bluez python-bluez

There are plenty of guides in the internet on how to get bluetooth working, but the only method that worked consistently for me is the following:

1. Disable bluetooth pnat support as there seems to be a bug which stops proper operation with pnat enabled. Full details can be found here: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=690749

A workaround is to add the following to /etc/bluetooth/main.conf: DisablePlugins = pnat

Not following the above will give errors similar to:

(111, 'Connection refused')

Incidentally, the “Steps to Reproduce” section of the bug link are a very good way of testing your bluetooth stack and making sure you have the proper groundwork in place

2. Make note of the following two commands which will be useful when pairing your raspberry pi to the android phone:

sudo hciconfig hci0 piscan [make your device discover-able] sudo hciconfig hci0 name 'Device Name' [change your device name to something else you fancy]

3. Download the following python script someplace on your raspberry pi. The script is a modified version of the “simple rfcomm server” script found under /usr/share/doc/python-bluez/examples/simple/rfcomm-server.py

Note that as previously stated, this script stems from a build to control an aquarium. So you may need to modify the if/elif/else statements beginning at line 60 to do something more meaningful in your particular case. Simply run the server via:

sudo python /path/to/script.py

On android: testing bluetooth and writing an app

Before proceeding, you must make sure to pair your android device to your raspberry pi. To do this, ensure that your raspberry pi is discover able (see step 2 above), and search for the device in the bluetooth settings in your android. After successful pairing, you should be able to continue.

1. Install “BlueTerm” from the playstore (https://play.google.com/store/apps/details?id=es.pymasde.blueterm). This will allow you to test your bluetooth setup up this stage. Your raspberry pi should accept connections and even respond to typed commands

2. Write an android app to establish a bluetooth rfcomm channel and send commands to the raspberry pi as well as read information. Following is a sample main activity from the mobile app written for the video demo:

Notes on the android script: