Sunday, September 24, 2006

Configure Bluetooth for Linux

Configure and set up a Linux computer to work with your Nokia smartphone over a Bluetooth connection.

Bluetooth support on Linux is a complex issue. As with many emerging technologies, competing implementations of Linux Bluetooth support exist. The two main implementations are Affix and BlueZ. Affix was developed by Nokia and is now hosted as an open source project at SourceForge (http://affix.sourceforge.net/). BlueZ is also available (http://www.bluez.org/) and is the official Bluetooth stack of the Linux kernel.

Although Affix is a mature and functional project, BlueZ receives more testing and has been more widely adopted. For this reason, this hack focuses on the uses of the BlueZ Linux Bluetooth stack and libraries.

Kernel Requirements

Bluetooth support under Linux requires a recent kernel. If your kernel is Version 2.4.22 or better, or if it is a 2.6 series kernel, you're all set. Otherwise, you must upgrade your kernel. Alternatively, if you do not want to upgrade, and you have kernel 2.4.18 or better compiled from source, you can apply the patches from the "kernel patches" area of the BlueZ web site (http://www.bluez.org/).

Installing BlueZ
In addition to kernel support, you must install a set of utility programs to help you manage your Bluetooth devices. Table 2-2 shows the name of the packages and their purpose. You can either install the versions of these tools that come with your Linux distribution or compile and install them from source.

Utility programs for managing Bluetooth devices Package Purpose

bluez-libs
The application library that all other Bluetooth tools require to function

bluez-utils
Main utilities that enable you to initialize and control Bluetooth devices

bluez-sdp
Service discovery protocol tools that enable the advertisement and discovery of Bluetooth services

bluez-pan
Tools that enable personal area networking using Bluetooth

bluez-hcidump
A debugging tool that permits the monitoring of Bluetooth packets

bluez-bluefw
The firmware for Broadcom chipset-based Bluetooth devices

If you are compiling the tools from source code, compile and install in the order shown in Table 2-2 to avoid dependency problems.

You can obtain precompiled versions of the utilities for Red Hat Linux as RPMs, for Debian stable as .deb packages (the latest BlueZ utilities are an integral part of Debian unstable), and as packages suitable for the Sharp Zaurus Linux PDA. You can download them, along with the source code packages, from the BlueZ download page at http://www.bluez.org/.

To determine whether your Bluetooth system is working, you need to install only the bluez-libs and bluez-utils packages, and also bluez-bluefw if your dongle contains a Broadcom chip (you can determine this from Marcel Holtmann's Bluetooth hardware page at http://www.holtmann.org/linux/bluetooth/devices.html). Install the rest when you have verified that everything is working properly.

Some Basic Command-Line Operations
The bluez-utils package contains the tools you need to configure and test your Bluetooth setup. Once you've installed the package, run the init script (/etc/init.d/bluez-utils start on Debian, /etc/init.d/bluetooth start on Red Hat) to start the Bluetooth subsystem. These scripts normally run on boot, so they might have been started already if you installed from RPMs or Debian packages.

The hcid daemon should now be running. This program controls the initialization of Bluetooth devices on the system and handles the pairing process with other devices. I discuss hcid configuration later in this chapter.

The prefix hci derives from the name of the interface between the computer and the Bluetooth device, or the Host Controller Interface.

In this section, I'll show you some of the most basic Bluetooth operations using the BlueZ command-line tools to verify that the system does work. For more operations, please refer to Chapter 7 of Linux Unwired (O'Reilly, 2004).

Examining local devices
The hciconfig tool allows the configuration of the characteristics of your Bluetooth adapter. If you are familiar with the configuration of network interfaces, you will find it parallel in operation to ifconfig. Use -a to display extended information about each Bluetooth device attached to the computer:

# hciconfig -a
hci0: Type: USB
BD Address: 00:80:98:24:15:6D ACL MTU: 128:8 SCO MTU: 64:8
UP RUNNING PSCAN ISCAN
RX bytes:4923 acl:129 sco:0 events:168 errors:0
TX bytes:2326 acl:87 sco:0 commands:40 errors:0
Features: 0xff 0xff 0x05 0x00
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: HOLD SNIFF PARK
Link mode: SLAVE ACCEPT
Name: 'saag-0'
Class: 0x100100
Service Classes: Object Transfer
Device Class: Computer, Uncategorized
HCI Ver: 1.1 (0x1) HCI Rev: 0x73 LMP Ver: 1.1 (0x1) LMP Subver: 0x73
Manufacturer: Cambridge Silicon Radio (10)



2.6.3.2 Scanning for remote devices.
The acid test, of course, is to see if your computer can detect other Bluetooth devices. You can use the hcitool tool to do this. Switch on your other Bluetooth device, and ensure it is in "discoverable" mode. Issue the command hcitool scan and wait. You don't need to be root to run this command.

$ hcitool scan
Scanning …
00:0A:D9:15:CB:B4 ED P800
00:40:05:D0:DD:69 saag-1



The previous listing shows a typical output of a scan. In this case the author's cell phone, ED P800, and second Bluetooth adapter, saag-1, are shown as discoverable.

Pairing
Many devices require that pairing be performed before a Bluetooth connection is established. The computer or the remote device can initiate pairing.

If the computer initiates pairingusually by making an outgoing connectionthe pin_helper program (usually bluepin) will present a graphical dialog box to the user requesting that he input a PIN, which should match the code set on the remote device. If the remote device initiates pairing, the remote device is required to provide a PIN to match the contents of the /etc/bluetooth/pin file.

In some distributions of bluez-utils, the PIN code is set to the alphabetical string BlueZ. This is troublesome, because many Bluetooth devices, including most cell phones, are capable of delivering only numeric PINs. It is therefore recommended that you alter the contents of /etc/bluetooth/pin to reflect a numeric code.

If pairing is successful, the hcid daemon will store the resulting link key, used to authenticate all future connections between the two devices concerned, in the /etc/bluetooth/link_key database file.

Pinging a remote device
The ping command is an incredibly useful tool for discovering whether remote computers are reachable over a TCP/IP network. BlueZ has an analog to ping, called l2ping. Its name refers to the fact that it attempts to create a connection to the device using the logical link control and adaptation protocol (L2CAP), the lowest-level link-based protocol in Bluetooth.

In other words, before despairing because you cannot connect to a device, check it with l2ping. There might be a fault with software higher up the chain; l2ping enables you to determine whether a basic connection can be established with a remote device. Here's an example of l2ping in action (you need to run l2ping as root):

# l2ping 00:0A:D9:15:CB:B4
Ping: 00:0A:D9:15:CB:B4 from 00:80:98:24:15:6D (data size 20) …
0 bytes from 00:0A:D9:15:CB:B4 id 200 time 54.85ms
0 bytes from 00:0A:D9:15:CB:B4 id 201 time 49.35ms
0 bytes from 00:0A:D9:15:CB:B4 id 202 time 34.35ms
0 bytes from 00:0A:D9:15:CB:B4 id 203 time 28.33ms
4 sent, 4 received, 0% loss



GUI Bluetooth Applications
Linux has several popular graphical user interface (GUI) systems, the most well known being KDE and GNOME. Both of these projects have tools that provide an easy-to-use interface to your system's Bluetooth devices. At the time of this writing, neither project is an official part of the KDE or GNOME desktop, but both will be integrated in the future. This section presents a brief survey of the tools available, and where to get them.

Before you can use the GUI frontends for Bluetooth on Linux, you need to install and configure BlueZ.

KDE
The KDE Bluetooth Framework's home page is at http://kde-bluetooth.sourceforge.net/. Its features include:

A control center plug-in to configure Bluetooth devices

An OBEX server application

An OBEX sending client

Graphical exploration of remote devices

Cell phone hands-free implementation using your computer's microphone and speakers

Proximity-based screen locking

You can download the KDE Bluetooth Framework from the project's web page. Figure 2-16 shows KDE's Bluetooth applications in action.


The KDE Bluetooth Framework

GNOME
The GNOME Bluetooth subsystem's home page is available at http://usefulinc.com/software/gnome-bluetooth. Download it from the project's home page. RPM and Debian packages are also available. Features of the GNOME Bluetooth subsystem include:

An OBEX server application

An OBEX sending client

A phone manager application allowing sending and receiving of Short Message Service (SMS) messages

0 Comments:

Post a Comment

<< Home