Bluetooth For Programmers: Albert Huang

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Bluetooth for Programmers

Albert Huang
albert@csail.mit.edu

Larry Rudolph
rudolph@csail.mit.edu
Bluetooth for Programmers
by Albert Huang and Larry Rudolph

Copyright © 2005 Albert Huang, Larry Rudolph

TODO
Table of Contents
Preface .......................................................................................................................................................vi
1. About this book .............................................................................................................................vi
2. Audience .......................................................................................................................................vi
3. Organization of This Book............................................................................................................vi
4. Acknowledgments.........................................................................................................................vi
4.1. Albert’s acknowledgments ............................................................................................. vii
4.2. Larry’s acknowledgments............................................................................................... vii
1. Introduction............................................................................................................................................1
1.1. Understanding Bluetooth as a software developer ......................................................................1
1.2. Bluetooth Programming Concepts ..............................................................................................1
1.2.1. Choosing a communication partner................................................................................2
1.2.2. Choosing a transport protocol ........................................................................................4
1.2.3. Port numbers and the Service Discovery Protocol .........................................................6
1.2.4. Communicating using sockets ........................................................................................9
1.3. Useful things to know about Bluetooth.....................................................................................11
1.3.1. Communications range.................................................................................................12
1.3.2. Communications Speed ................................................................................................12
1.3.3. Radio Frequencies and Channel Hopping ....................................................................13
1.3.4. Bluetooth networks - piconets, scatternets, masters, and slaves...................................13
1.3.5. Bluetooth Profiles + RFCs............................................................................................14
2. Bluetooth programming with Python - PyBluez...............................................................................16
2.1. Choosing a communication partner ..........................................................................................16
2.2. Communicating with RFCOMM ..............................................................................................17
2.3. Communicating with L2CAP....................................................................................................19
2.3.1. Maximum Transmission Unit .......................................................................................20
2.3.2. Best-effort transmission................................................................................................21
2.4. Service Discovery Protocol.......................................................................................................21
2.4.1. Dynamically allocating port numbers ..........................................................................23
2.4.2. Advertising a service ....................................................................................................23
2.4.3. Searching for and browsing services ............................................................................25
2.5. Advanced usage ........................................................................................................................26
2.5.1. Asynchronous socket programming with select .......................................................26
2.5.2. Asynchronous device discovery ...................................................................................27
2.5.3. The _bluetooth module ............................................................................................28
3. C programming with libbluetooth ................................................................................................31
3.1. Choosing a communication partner ..........................................................................................31
3.1.1. Compiling the example.................................................................................................32
3.1.2. Representing Bluetooth addresses................................................................................32
3.1.3. Choosing a local Bluetooth adapter..............................................................................33
3.1.4. Scanning for nearby devices.........................................................................................33
3.1.5. Determining the user-friendly name of a nearby device ..............................................34
3.1.6. Error handling...............................................................................................................35
3.2. RFCOMM sockets ....................................................................................................................35
3.2.1. Addressing structures ...................................................................................................38
3.2.2. Establishing a connection .............................................................................................38

iii
3.2.3. Using a connected socket .............................................................................................39
3.3. L2CAP sockets..........................................................................................................................40
3.3.1. Byte ordering ................................................................................................................42
3.3.2. Maximum Transmission Unit .......................................................................................43
3.4. Service Discovery Protocol.......................................................................................................43
3.4.1. Dynamically assigned port numbers ............................................................................44
3.4.2. SDP data structures.......................................................................................................44
3.4.3. Advertising a service ....................................................................................................46
3.4.4. Searching and browsing for a service...........................................................................50
3.5. Advanced BlueZ programming.................................................................................................55
3.5.1. Best-effort transmission................................................................................................55
3.6. Chapter Summary .....................................................................................................................57
4. Bluetooth development tools ...............................................................................................................59
4.1. hciconfig ...............................................................................................................................59
4.2. hcitool....................................................................................................................................62
4.3. sdptool....................................................................................................................................63
4.4. hcidump....................................................................................................................................65
4.5. l2ping ......................................................................................................................................66
4.6. rfcomm ......................................................................................................................................67
4.7. uuidgen....................................................................................................................................68
4.8. Obtaining BlueZ and PyBluez ..................................................................................................68
5. Other platforms and programming languages .................................................................................69
5.1. Microsoft Windows...................................................................................................................69
5.2. OS X..........................................................................................................................................69
5.3. Symbian OS / Nokia Series 60 Smartphones............................................................................69
5.4. Java............................................................................................................................................69

iv

You might also like