Monday, October 22, 2012

Look Ma, no Wires! Raspberry Pi Bluetooth tethering

Bluetooth is a relatively low-powered short-range wireless technology, which can among others provide a networking connection between devices and is widely supported in portable devices like laptops and mobile phones.  Low power and ubiquitous support should in principle make Bluetooth an ideal solution for tethering.

Nothing beats the simplicity, robustness and performance of a simple Ethernet cable for a tethered network connection - as long as both devices support it. For the upcoming Raspberry Pi model A or certain ultra-portable devices without Ethernet port, Bluetooth might be serious option.

Wi-fi would probably be the most obvious choice of wireless networking technology. However using the ad-hoc wi-fi hotspot option for tethering on my Android phone turns it into a pocket heater and drains the battery in no time  - while not so much when using Bluetooth tethering instead. Besides wi-fi networking is very well documented, while there is very little documentation for Bluetooth networking on Linux in general and for Raspberry Pi in particular.

For the experiment, I am using a cheap no-name USB Bluetooth dongle which identifies itself as "ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)" with lsusb. Luckily it seems to just work with the standard Raspbian kernel, which isn't always a given for exotic hardware under Linux.

The next steps is installing all the Bluetooth support software:

sudo apt-get install bluetooth bluez-utils bluez-compat

Once the Bluetooth subsystem is started, the adaptor should to start blinking a blue light, if all goes well.

The Bluetooth security model requires that 2 devices must be "paired' by the user before they can connect to each other automatically. Unfortunately, this procedures seem to still be a bit of black-magic on Linux, specially when only using command-line tools. The following two different approaches have worked to pair an Android phone as a device to the Raspberry Pi as well as pairing the Raspberry Pi as a device to a Apple Powerbook.

To pair with Android phone (4.1.2):

  1. make phone visible for Bluetooth scanning (settings -> Bluetooth, click on device name to toggle visibility)
  2. scan for new devices: hcitool scan (note Bluetooth address of phone - e.g. C8:BC:C8:E1:E5:C5)
  3. pair with new device by address: bluez-simple-agent hci0 C8:BC:C8:E1:E5:C5

To pair with Powerbook:
  1. make raspberry pi visible to Bluetooth scans: sudo hciconfig hci0 piscan
  2. accept pairing requests (arbitrary PIN): sudo bluetooth-agent 1234
  3. on powerbook, open Bluetooth setup and scan for new devices, pair raspberry pi
  4. hide raspberry pi from further scans: sudo hciconfig hci0 noscan
In order to show all the devices which are currently paired, i.e. available for connection if within reach:

sudo bluez-test-device list

And finally used pand to create a PAN (Personal Area Network) connection:

sudo pand -c C8:BC:C8:E1:E5:C5 --role PANU --persist 30

which should result in creating a new network interface bnep0 (see with ifconfig -a). Since both Android and OS X Internet connection sharing support address configuration via DHCP, we can add the following line to /etc/netwok/interfaces:

iface bnep0 inet dhcp

After adding the pand connect command to /etc/rc.local, the connection is established on startup if the other device is in reach. With the persist option, the PAN demon will attempt to reconnect whenever the connection is interrupted.

A small but significant disadvantage over the current wired setup is that mDNS zero-conf does not seem to work properly. Even though the Raspberry Pi is sending mDNS announcement from avahi on the PAN interface, the bonjour service database does not get populated on the Powerbook - but maybe this is an issue with my particular configuration.

Overall, the Bluetooth networking setup seems stable enough to establish the connection without user intervention on startup, which is essential to operate in a tethered, headless mode. And the power consumption of the Bluetooth dongle is still low enough to to power the Raspberry Pi with both Bluetooth & Ethernet connections from the laptop USB port...