8.4. Bluetooth — Wireless Connections

Bluetooth is a wireless technology for connecting various devices. Two important characteristics distinguish Bluetooth from IrDA: the individual devices do not need to “see” each other directly and several devices can be connected in a network with a maximum data rate of 720 Kbps (in the current version 1.1). Theoretically, Bluetooth is even capable of communicating through walls. However, in practice this largely depends on the properties of the walls and the device class. There are three device classes with maximum transmission ranges between ten and a hundred meters.

8.4.1. Profiles

In Bluetooth, services are defined by means of profiles, such as the file transfer profile, the basic printing profile, and the personal area network profile. To enable a device to use the services of another device, both must understand the same profile — a piece of information that is often missing on the device package and in the manual. Although some manufacturers strictly comply with the definitions of the individual profiles, others do not. Nevertheless, the communication between the devices usually works smoothly.

8.4.2. Software

To be able to use Bluetooth, you need a Bluetooth adapter (built-in or external), drivers, and a Bluetooth protocol stack. By default, the Linux kernel contains the basic drivers needed for using Bluetooth. The Bluez system is used as protocol stack. Additionally, install all packages associated with Bluetooth (bluez-libs, bluez-bluefw, bluez-pan, bluez-sdp, and bluez-utils), as these provide some necessary services and utilities.

8.4.3. Configuration

The configuration files described in this section can only be modified by the user root. Currently, there is no graphical user interface for setting the parameters. Therefore, the files must be modified with a text editor.

A PIN number provides basic protection against unwanted connections. Mobile phones usually query the PIN when establishing the first contact (or when setting up a device contact on the phone). For two devices to be able to communicate, both must identify themselves with the same PIN. On the computer, the PIN is located in the file /etc/bluetooth/pin. Currently, only one PIN is supported in Linux, regardless of the number of installed Bluetooth devices. Thus, multiple devices cannot be addressed with different PINs. Accordingly, you need to set the same PIN on all devices or deactivate the PIN authentication entirely.

[Important]Security of Bluetooth Connections

Despite the PINs, the transmission between two devices may not be entirely secure.

/etc/bluetooth/hcid.confis main configuration file for Linux Bluetooth. Various settings, such as the device names and the security modes, can be modified in this file. Usually, the settings should be adequate. The file contains comments describing the options for the various settings.

security auto; is one of the most important settings. If necessary, a PIN is activated for the identification. If problems are encountered, the option auto disables the PIN. Depending on your preferences and your security needs, set this option to none never to use PIN numbers or to user to use PIN numbers.

Another important section is the one beginning with device {. In this section, define the name under which the host should be displayed on the other side. The device class (Laptop, Server, etc.), authentication, and encryption are defined in this section.

8.4.4. System Components and Useful Tools

The operability of Bluetooth depends on the interaction of various services. At least two background daemons are needed: hcid (host controller interface), which serves as an interface for the Bluetooth device and controls it, and sdpd (service discovery protocol), by means of which a device can find out which services the host makes available. If they are not activated automatically when the system is started, both hcid and sdpd can be activated with the command rcbluetooth start. This command must be executed as root.

The following paragraphs describe the main tools needed for working with Bluetooth. Currently, only command-line programs are available. Extensions for Konqueror (KDE desktop) and Nautilus (GNOME desktop) are under development. This functionality may have been implemented after the editorial deadline. In this case, the URL sdp:// should display local Bluetooth devices (physically connected to the host) as well as remote Bluetooth devices (accessible by way of a wireless connection).

[Tip]Tip

Other functionalities of the above-mentioned Bluetooth applications can be viewed with man <program_name>.

Some of the commands can only be executed as root. This includes the command l2ping <device_address> for testing the connection to a remote device.

8.4.4.1. hcitool

hcitool can be used to determine whether local and remote devices are detected. The command hcitool dev should list your devices. The output generates a line in the form <interface_name> <device_address> for every detected local device.

The command hcitool name <device_address> can be used to determine the device name of a remote device. If, for example, another computer is detected, the displayed class and device name corresponds to the information in the file /etc/bluetooth/hcid.conf on the remote computer. Local devices addresses generate an error output.

8.4.4.2. hciconfig

Get more information about the local device with /sbin/hciconfig. Search for remote devices (that are not connected physically to the host) with the command hcitool inq. Three values are displayed for every detected device: the device address, the clock offset, and the device class. The device address is important, as other commands use it for identifying the target device. The clock offset mainly serves technical purposes. In the class, the device type and the service type are encoded as a hexadecimal value.

8.4.4.3. sdptool

The program sdptool can be used to check which services are made available by a specific device. The command sdptool browse <device_address> returns all services of a device. The command sdptool search <service_code> can be used to search for a specific service. This command scans all accessible devices for the requested service. If one of the devices offers the service, the program prints the (full) service name returned by the device together with a brief description. A list of all possible service codes can be viewed by entering sdptool without any parameters.

8.4.5. Examples

The following two examples demonstrate some of the capabilities of Bluetooth.

8.4.5.1. Network Connection between Two Hosts

The first example shows the establishment of a network connection between two hosts with pand (personal area networking). The following commands must be executed by the user root. The description focuses on the Bluetooth-specific actions and does not provide a detailed explanation of the network command (ip):

Start pand with the command pand -s on one of the two hosts (referred to as H1). Determine the device address of the second host (H2) by running hcitool inq on this host. Run pand -c <device_address> to establish a connection. If you query the available network interfaces with ip link show, an entry such as the following should be displayed (the local device address should be displayed instead of 00:12:34:56:89:90):

 bnep0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
    link/ether 00:12:34:56:89:90 brd ff:ff:ff:ff:ff:ff

This interface must be assigned an IP address and activated. This can be done with the following two commands. On H1:

ip addr add 192.168.1.3/24 dev bnep0
ip link set bnep0 up

On H2:

ip addr add 192.168.1.4/24 dev bnep0
ip link set bnep0 up

Now H1 can be accessed from H2 under the IP 192.168.1.3. Use the command ssh 192.168.1.4 to access H2 from H1 (provided H2 runs an sshd, which is activated by default in SUSE LINUX). The command ssh 192.168.1.4 can also be run as a “normal” user.

8.4.5.2. File Transfer from a Mobile Phone to the Host

The second example shows how to transfer a photograph created with a mobile phone with a built-in digital camera to a computer (without incurring additional costs for the transmission of a multimedia message). Although the menu structure may differ on various mobile phones, the procedure is usually quite similar. Refer to the manual of your phone, if necessary. This example describes the transfer of a photograph from a Sony Ericsson mobile phone to a laptop. The service Obex-Push must be available on the computer and the computer must grant the mobile phone access. In the first step, the service is made available on the laptop. This is done by means of the opd daemon from the package bluez-utils. Start the daemon with the following command:

opd --mode OBEX --channel 10 --daemonize --path /tmp --sdp

Two important parameters are used: --sdp registers the service with the sdpd and --path /tmp instructs the program where to save the received data — in this case to /tmp. You can also specify any other directory to which you have write access.

Now the mobile phone must “get to know” the computer. To do this, open the Connect menu on the phone and select Bluetooth. If necessary, click Turn On before selecting My devices. Select New device and let your phone search for the laptop. If a device is detected, its name appears in the display. Select the device associated with the laptop. If you encounter a PIN query, enter the PIN specified in /etc/bluetooth/pin. Now your phone knows the laptop and is able to exchange data with the laptop. Exit the current menu and go to the image menu. Select the image to transfer and press More. In the next menu, press Send to select a transmission mode. Select Via Bluetooth. The laptop should be listed as a target device. Select the laptop to start the transmission. The image is then saved to the directory specified with the opd command. In the same way, you can also transfer audio tracks to the laptop.

8.4.6. Troubleshooting

If you have difficulties establishing a connection, proceed as follows:

  • Check the output of hcitool dev. Is the local device listed? If not, the hcid may not have been started at all or the device may not be recognized as a Bluetooth device (either because the driver is not able to do this or because the device is defective). Restart the daemon with the command rcbluetooth restart and check /var/log/messages to see if any errors occurred.

  • Does the computer “see” other devices when you execute hcitool inq? Try the command twice — the connection may have been faulty, as the frequency band for Bluetooth is also used by other devices.

  • Make sure that the PIN in /etc/bluetooth/pin is the same as the PIN of the remote device.

  • Try to establish the connection from the other device. Check if this device sees the computer.

  • The first example (network connection) does not work. This may be due to various reasons. Possibly one of the two hosts does not understand the ssh protocol. Try if ping 192.168.1.3 or ping 192.168.1.4 works. If it does, check if sshd is active. Another problem could be that you already have other addresses that conflict with the address 192.168.1.X used in the example. If this is the case, try other addresses, such as 10.123.1.2 and 10.123.1.3.

  • In the second example, the laptop does not appear as the target device. Does the mobile device recognize the Obex-Push service on the laptop? In My devices, select the respective device and view the list of Services. If Obex-Push is not displayed (even after the list is updated), the problem is caused by the opd on the laptop. Is the opd active? Do you have write access to the specified directory?

  • Does the second example work in reverse order? If obexftp is installed, this should work with obexftp -b <device_address> -B 10 -p <image> on some devices (Siemens and Sony Ericsson have been tested, other devices may or may not support this action).

8.4.7. For More Information

An extensive overview of various instructions for the use and configuration of Bluetooth is available at http://www.holtmann.org/linux/bluetooth/.

Useful information and instructions:

Connection to PalmOS PDA:

http://www.cs.ucl.ac.uk/staff/s.zachariadis/btpalmlinux.html

Official howto for the integrated Bluetooth protocol stack in the kernel:

http://bluez.sourceforge.net/howto/index.html