Hack 16 Using Bluetooth with Linux

figs/expert.giffigs/hack16.gif

Get Bluetooth up and running quickly under Linux 2.4.

As you might expect, getting Bluetooth to work under Linux takes a little more work than other operating systems. First, it should be noted that there are actually three distinct Bluetooth protocol stacks for Linux: Affix, OpenBT, and BlueZ, each with varying support for different Bluetooth adapters, and each with somewhat distinct means of configuration. Since BlueZ has been crowned as the "official" Linux Bluetooth stack, that's the one we'll focus on here.

First, make sure you have a supported Bluetooth adapter. You can find a reasonably current list of BlueZ-supported hardware at http://www.holtmann.org/linux/bluetooth/devices.html.

Next, you'll need to make sure that your kernel has Bluetooth support enabled. Kernels shipped with both the Red Hat 9.0 and Debian "Sarge" distributions already include Bluetooth support. You can test your kernel for Bluetooth supports by running modprobe rfcomm as root. If the modprobe fails, you'll need to rebuild your kernel.

In the event of a failure, build and install a fresh copy of the Linux kernel at Version 2.4.21 or better (or 2.4.20 with the -mh6 patch). When you configure the kernel, select all of the options under "Bluetooth support" to be built as separate modules. However, be sure that the "USB Bluetooth support" option under "USB support" is disabled this compiles in UART support the OpenBT protocol stack, which will interfere with the BlueZ stack. Newer versions of the kernel disable this last option for you automatically, if BlueZ is selected.

One additional note: if you are running a newer Toshiba or Sony laptop and want to use the built-in Bluetooth adapter, you will need to enable the Toshiba- or Sony-specific kernel options under the "Processor type and features" and "Character devices" sections. You will also need special userspace utilities to enable the Bluetooth adapters on these laptops; these utilities are beyond the scope of this book, but more information about them can be found on the BlueZ-supported device listing referred to earlier in this section.

Next, add the following lines to your /etc/modules.conf:

alias net-pf-31 bluez alias bt-proto-0 l2cap alias bt-proto-2 sco alias bt-proto-3 rfcomm alias bt-proto-4 bnep alias tty-ldisc-15 hci_uart alias bluetooth off

Run /sbin/depmod -a as root.

These options tell the kernel which modules to load when Bluetooth support is requested. The last option, alias bluetooth off, is included to tell modprobe not to load the OpenBT UART module, in case it was installed by accident.

You also need the BlueZ userspace utilities. Again, Red Hat 9.0 comes with these tools installed. You can get the source code, as well as RPMs and .debs, from the BlueZ homepage at http://bluez.sourceforge.net. Be sure to build and/or install the bluez-libs, bluez-utils, bluez-sdp, and bluez-hcidump packages. You may be able to simply run apt-get from Debian, except that Debian refers to "bluez-libs" as "libbluetooth1" and offers "libsdp2" as a separate package.

This next bit is for UART-based (that is, non-USB) devices only, so if you're running a USB Bluetooth adapter, you can skip it. Serial-style devices, which include serial dongles and PCMCIA cards, need to be explicitly "attached" to the Bluetooth host controller interface, using the hciattach utility. When you connect the device, the appropriate kernel driver may be loaded automatically, leaving a log entry in /var/log/messages. If you're using a UART-based device, you may see a reference to a /dev/ttySn serial device, where n is some integer. In any event, you can try attaching the device to the Bluetooth host controller device by running /sbin/hciattach /dev/ttySn any from the command line. Like any good Unix utility, you know that hciattach worked if it returns without printing anything. If it doesn't work, make sure you have the right device and check the manpage for other options.

Assuming that the hciattach command did work, you will want to add a reference to this device to your /etc/bluetooth/uart file, so that the device can be appropriately attached to the Bluetooth host controller interface at boot time. If this file doesn't exist, create it. Add a single line to this file that reads "/dev/ttySn any", replacing "n" with the appropriate serial device number. If you're using a USB adapter, of course you don't need to bother with this step.

Now that you have everything installed, plug in your Bluetooth adapter and try running /etc/rc.d/init.d/bluetooth start as root. In Debian, start Bluetooth with /etc/init.d/bluez-utils start; /etc/init.d/bluez-sdp start. You should see some appropriate status messages in your /var/log/messages. If your install of BlueZ didn't include the /etc/rc.d/init.d/bluetooth script, you can copy a suitable version from the scripts/ subdirectory of the bluez-utils package. Assuming everything works, you may wish to add the Bluetooth script to the appropriate rc.d directory for your default runlevel with the chkconfig utility or via a manual symlink.

Now run hciconfig from the command line. You should see something like:

hci0:   Type: USB          BD Address: 00:11:22:33:44:55 ACL MTU: 192:8  SCO MTU: 64:8         UP RUNNING PSCAN ISCAN          RX bytes:99 acl:0 sco:0 events:13 errors:0         TX bytes:296 acl:0 sco:0 commands:12 errors:0

If you don't see anything like this, make sure that hcid is running, and that there aren't any error messages in /var/log/messages. The BD Address shown is the unique Bluetooth identifier for your adapter, much like an Ethernet MAC address.

Now bring another Bluetooth device within range of your computer, and make sure that the device is visible to Bluetooth scans. Then run hcitool scan from the command line. It may take up to 15 or 20 seconds to complete its scan, and then it should display something like:

$ hcitool scan Scanning ...         00:99:88:77:66:55       Nokia3650

You can now test the device to see which services it supports, using sdptool browse 00:99:88:77:66:55. You should see a lengthy list of supported services, providing information that can be used to configure access to those services.

Schuyler Erle



Wireless Hacks. 100 Industrial-Strength Tips and Techniques
Wireless Hacks. 100 Industrial-Strength Tips and Techniques
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 158

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net