Bluetooth Programming For Linux
Bluetooth Programming For Linux
for Linux
Marcel Holtmann
Andreas Vedral
BlueZ Project
FH Bochum
Agenda
Short introduction into Bluetooth
History of Bluetooth and Linux
The official Linux Bluetooth stack
Integration into other Linux subsystems
Supported protocols and profiles
The programming interfaces
Protocol decoding
Linux programming tools
2
What is Bluetooth?
Universal interface for wireless communication
Communication over short range ad-hoc networks
Worldwide license free ISM band
Low cost radio modules
Minimal power consumption
Transmission of data and voice
Application profiles
Bluetooth controller
Application
Host
protocol
stack
HCI
HCI
Bluetooth
LM & IO
Bluetooth
Baseband
Bluetooth
Radio
Host system
Voice
Up to 3 voice channels
64 kbit/sec
64 kbit/sec
732.2 kbit/sec
57.6 kbit/sec
433.9 kbit/sec
Data
79 channels
Frequency changed after each
packet, 1600 hops per second
Packet sizes with one, three or
five slots
Data
64 kbit/sec
433.9 kbit/sec
St andby
S
S
M
S
IBM BlueDrekar
25. Juli 2000
http://www.alphaworks.ibm.com/tech/BlueDrekar/
Qualcomm BlueZ
3. Mai 2001
http://www.bluez.org
O
c
N t '0
ov 1
D '01
ec
Ja '01
n
Fe '02
b
M '02
ar
Ap '02
r
M '02
ay
Ju '02
n
'
Ju 02
l'
Au 02
g
Se '02
p
O '02
ct
N '02
ov
D '02
ec
Ja '02
n
Fe '03
b
M '03
ar
'
Ap 03
r
M '03
ay
'
Ju 03
n
'0
Ju 3
l'
Au 03
g
'
Se 03
p
'0
3
700
650
600
550
500
450
400
350
300
250
200
150
100
50
8
BlueZ
Affix
Bluetooth patches
http://www.holtmann.org/linux/kernel/
10
Features of BlueZ
Real hardware abstraction
Generic and vendor specific drivers
Over 150 supported Bluetooth adapters
Up to 16 host adapters at the same time
More features
BSD sockets interface
HCI raw socket
L2CAP sequential packet and datagram
SCO sequential packet
RFCOMM stream
SYNC
OPP
IrMC
PPP
BIP
DUN
LAN
DUN
BPP
GOEP
PAN
CTP
CIP
SPP
HSP
RFCOMM layer
(rfcomm.o)
HCRP
BNEP layer
(bnep.o)
CMTP layer
(cmtp.o)
Audio (sco.o)
13
USB H:2
UART H:4
BCSP
Nokia/Socket
Anycom
3Com/HP
BlueFRITZ! USB
Future protocols
TCS-BIN,
AVDTP, AVCTP
14
Current profiles
GAP, SDAP,
SPP, GOEP,
DUN, FAX, LAN,
PUSH, SYNC, FTP,
PAN, CIP,
HID, HCRP
Future profiles
CTP, Intercom,
BPP, BIP,
HSP, HFP, SAP
Advantages of BlueZ
Full source code is available under the GPL
Socket based interfaces
Simple API for special HCI or SDP tasks
Access to all Bluetooth host layers
Big user and developer community
Very good interoperability with Bluetooth
1.0b and 1.1 devices
Full Bluetooth 1.2 support is planned
15
16
# hciconfig hci0 up
# hciconfig -a
hci0:
Type: USB
BD Address: 00:50:F2:7A:33:78 ACL MTU: 192:8 SCO MTU: 64:8
UP RUNNING PSCAN ISCAN
RX bytes:77 acl:0 sco:0 events:9 errors:0
TX bytes:30 acl:0 sco:0 commands:8 errors:0
Features: 0xff 0xff 0x0f 0x00
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: RSWITCH HOLD SNIFF PARK
Link mode: SLAVE ACCEPT
Name: 'Microsoft Bluetooth Transceiver'
Class: 0x000100
Service Classes: Unspecified
Device Class: Computer, Uncategorized
HCI Ver: 1.1 (0x1) HCI Rev: 0x1f9 LMP Ver: 1.1 (0x1) LMP Subver: 0x1f9
Manufacturer: Cambridge Silicon Radio (10)
17
# hcitool scan
Scanning ...
00:02:C7:1E:1D:B8
iPAQ H3970
00:04:0E:21:06:FD
AVM BlueFRITZ! AP-X
00:0A:D9:5C:75:57
Sony Ericsson T68i
00:A0:57:AD:22:0F
ELSA Vianect Blue ISDN
00:90:02:63:E0:83
Bluetooth Printer
00:80:37:06:78:92
Ericsson T39m
08:00:46:0E:CE:50
SONY Cyber-shot
00:04:61:50:D4:3E
EPox BT-PM01B 80D43E
# hcitool info 00:04:0E:81:06:FD
Requesting information ...
BD Address: 00:04:0E:21:06:FD
Device Name: AVM BlueFRITZ! AP-X
LMP Version: 1.1 (0x1) LMP Subversion: 0x1
Manufacturer: AVM Berlin (31)
Features: 0x2f 0xbe 0x05 0x00
<3-slot packets> <5-slot packets> <encryption> <slot offset>
<role switch> <RSSI> <channel quality> <SCO link>
<HV2 packets> <HV3 packets> <A-law log> <CVSD>
<power control>
19
RFCOMM
Data stream (SOCK_STREAM)
Sockets can be converted to a TTY device
Uses the L2CAP in-kernel socket interface
21
addr.rc_family = AF_BLUETOOTH;
bacpy(&addr.rc_bdaddr, dst);
addr.rc_channel = channel;
if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
close(sk);
return -1;
str2ba("00:a5:b4:c3:d2:e1", &bdaddr);
}
sk = rfcomm_connect(BDADDR_ANY, &bdaddr, 1);
return sk;
}
=
=
=
=
0; /* device hci0 */
8; /* ~10 seconds */
10;
0;
22
free(info);
}
24
mcc_len 2
0
mcc_len 2
7
mcc_len 2
0
mcc_len 2
7
Programming tools
Standard tools and compiler
gcc, ld, libtool, make etc.
autoconf, automake etc.
AC_INIT()
AM_INIT_AUTOMAKE(example-project, 0.1)
CFLAGS="-Wall -g -O2"
AC_PROG_CC
AC_PROG_INSTALL
AC_OUTPUT(Makefile)
# ls -la
total 38
drwxr-xr-x
drwxr-xr-x
-rw-r--r--rwxr-xr-x
-rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--
configure.in
2
21
1
1
1
1
1
1
1
1
1
holtmann
holtmann
holtmann
holtmann
holtmann
holtmann
holtmann
holtmann
holtmann
holtmann
holtmann
staff
staff
staff
staff
staff
staff
staff
staff
staff
staff
staff
296
624
0
68
0
121
18009
30
78
0
0
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
Sep
25
25
25
25
25
25
25
25
25
25
25
18:18
18:04
18:00
18:18
18:00
18:02
18:00
18:04
18:04
18:00
18:00
.
..
AUTHORS
bootstrap
ChangeLog
configure.in
COPYING
main.c
Makefile.am
NEWS
README
bin_PROGRAMS = example
example_SOURCES = main.c
example_LDADD = -lbluetooth
Makefile.am
#!/bin/sh
aclocal \
&& automake -a -c 2> /dev/null \
&& autoconf
25
bootstrap
Any questions
???