Linux Guide/Linux and Bluetooth

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Forewords[edit | edit source]

The following is just a simple example on how to connect a Bluetooth device, in this case a Nokia phone, in a Linux environment.

For the purpose of this article we assume to have installed Linux Mandrake 10.0 (Kernel 2.6.13, X86) on our computer.

Configuration and Connection[edit | edit source]

First we need the Bluez protocol, usually available as a RPM package on your distribution, then we have to start bluetooth service:

[ user]# service bluetooth start

now it's necessary to setup the PIN code and (if you want) the local device name link, for that purpose we edit (e.g. using JOE) the file /etc/bluetooth/hcid.conf:

[ user]# joe /etc/bluetooth/hcid.conf

modify the following excerpts:

excerpt for PIN:

# PIN helper 
pin_helper /etc/bluetooth/;  

where is a file created ad-hoc.

Excerpt for name:

# Local device name
# %d - device id 
# %h - host name
name "userdomain (%d)";

N.B.: file contains:

echo "PIN:XXX"

where XXXX means your PIN.

Now it's time to search for devices:

[ user]# hcitool scan

that should returns something like:

12:34:56:78:90:12 user1

in other words bdaddr (BT address) and device name, please take note of bdaddr, now it's time to discover available services an the remote device and on which channels:

[ utente]# sdptool records 12:34:56:78:90:12

depending on the type of device we are analysing the request returns a long list of profiles, in this case we are interested in the dialup one, so let see it available on channel 1:

Service Name: Dial-Up Networking
Service RecHandle: 0x10007
Service Class ID List:
  "Dialup Networking" (0x1103)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 1
Language Base Attr List:
  code_ISO639: 0x454e
  encoding: 0x6a
  base_offset: 0x100
Profile Descriptor List:
  "Dialup Networking" (0x1103)
    Version: 0x0100


[ user]# rfcomm bind 0 12:34:56:78:90:12 1

in other words I bind a virtual serial port, rfcomm0 (/dev/bluetooth/rfcomm/0), by means of the tool rfcomm to the remote device modem, not connected yet but ready for software's requests, let see:

[ user]# rfcomm show 0

that results in:

rfcomm0: 12:34:56:78:90:12 channel 1 clean

BT modem ready as serial peripheral at /dev/bluetooth/rfcomm/0

No need to mention that you in the meantime have already accepted the connection on your remote device.

Or to connect from the linux side, prepare an agent to respond properly to a pin request, then connect:

[ user]# bluetooth-agent 1234 12:34:56:78:90:12
[ user]# rfcomm connect 0 12:34:56:78:90:12

Then, in another terminal, you can for example open a terminal on /dev/rfcomm0:

[ user]# minicom /dev/rfcomm0

Bluetooth device as a modem[edit | edit source]

To use this ready to use device as a modem I suggest as dialer the program wvdial, for this purpose we need to edit it's configuration file /etc/wvdial.conf, like this:

[Dialer Defaults]
Modem = /dev/bluetooth/rfcomm/0
Baud = 460800
Dial Attempts = 1
Init1 = ATZ
Init3 = AT+CGDCONT=1,"IP",""
Phone = *99#
Carrier Check = no
Stupid Mode = yes
Username = ""
Password = ""

N.B.: this example is from a working configuration for Vodafone Italy on a Nokia phone, for different countries/operators/brand configuration, please google with keyword +CGDCONT and operator's name, mostly you need to modify just the string

Init3 = AT+CGDCONT=1,"IP",""

according to your operator's specifications, and

Phone = *99# 

according to your phone's brand.

Dialup and connect...:

[ utente]# wvdial

Good Luck!!!

Other useful commands[edit | edit source]

  • hciconfig
  • hcitool scan
  • hcitool info BT_ADDRESS

BT_ADDRESS may be like, 00:89:34:62:67:52

  • l2ping BT_ADDRESS
  • sdptool browse BT_ADDRESS
  • sdptool search DUN
  • hciattach -l

external links[edit | edit source]