MOBILE APPLICATION
PROGRAMMING AND DESIGN
Chapter 5: Bluetooth Control Programming
MS. DAO TO HIEU
Office: A4 704 Mobile: 0389959524
Email: hieu.daoto@phenikaa-uni.edu.vn
11/09/2023 1
CONTENTS
Bluetooth and Environment
Bluetooth on Android
Program BLE
Exercise
11/09/2023 2
What’s Bluetooth?
✓ Wireless protocol for exchanging data over short
distances from fixed and mobile devices
✓ Initially proposed as a wireless equivalent to RS-
232 (COM)
✓ Developed and maintained by Bluetooth Special
Interest Group (Bluetooth SIG)
11/09/2023 3
What doest Bluetooth mean?
❑ Named after Harald I Bluetooth (Danish
Harald Blåtand) who united Scandinavian
tribes – just like bluetooth unites different
devices.
❑ The Bluetooth logo unites the Runic
alphabetic characters "H", which looks similar
to an asterisk, and a "B", which are the initials
for Harald Bluetooth. If you look close enough
you can see both embodied in the logo.
11/09/2023 4
TIME LINE
The Bluetooth
Bluetooth
Bluetooth Bluetooth 3.0 + HS
5.3
appeared at 1.0 Bluetooth
(High Speed)
Bluetooth Release.
Swedish Releases Releases
2.0 + EDR 5.0
electronics (Enhanced
Data Rate)
Releases
company
Ericsson Releases
5.2021
1994 1999 2004 2010 2016
1998 2002 2007 2013 10.2020
Bluetooth SIG Bluetooth Bluetooth
formed of five 2.1 + EDR 5.2
Bluetooth
companies Releases Releases
4.0
Bluetooth (Bluetooth
1.2 Low Energy)
Release Releases
11/09/2023 5
Radio Layer
✓ Equivalent to Physical Layer of the Internet Model
✓ Operates in the 2.4 GHz ISM band (May interfere with
802.11b and 802g and DECT telephones)
✓ Bandwidth is divided into 79 RF channels which are ordered
from channel number 0-78 and are spaced 1 MHz beginning
at 2402 GHz
✓ Lower Guard Band – 2MHz, Upper Guard Band - 3.5 MHz
11/09/2023 6
Baseband Layer
✓ Basic Bluetooth communication is performed in a
Master-Slave manner
✓ Each Master and its connected slave(s) form a Piconet
clock
✓ A Master can have up to 7 active slaves
✓ Slave may send data only if was poled by a Master
11/09/2023 7
Piconet Clock
✓ All timing and scheduling activities in the Piconet shall
be done according to single clock - the Master clock.
✓ The clock is derived from the native device’s clock by
adding an offset
✓ All clocks in the devices run at the same nominal rate,
but mutual drift causes inaccuracies .
✓ The offsets in the slaves must be regularly updated
11/09/2023 8
Setting up a Piconet – Paging Mode
➢ The device formerly performing inquiry enters page
mode
➢ In the page mode the paging device continuously
transmits an ID packet containing slave’s access code
➢ The device that responded to an inquiry may enter the
page scan mode
➢ On receiving a page message with its access code the
scanning device responses with an FHS (Frequency
Hopping Synchronization)
➢ Master then responses with another FHS
FHS
s
FHS
11/09/2023 9
Connection Established
Paging device becomes master
Scanning device becomes slave (roles may be
exchanged)
Clocks and hoping sequences are synchronized
Master Slave
Clean my
buttons!
Okay…
11/09/2023 10
Bluetooth Packet Format
Basic Rate Packet (Bluetooth 1.0-3.0)
Enhanced Data Rate Packet
Acess code
11/09/2023 11
Packet Exchange Examples
Page Scan Physical Channel
Same Frequencies used in the first and second 635ns periods
11/09/2023 12
Addressing and Access codes
Bluetooth (BD_ADDR) including 48 bits address:
➢ 3 Type of access codes derived from the address
➢ Device access code (DAC) (Device’s LAP)
➢ Channel access code (CAC) (Piconet Master’s LAP)
➢ Inquiry access code (IAC) (Paged Device’s LAP)
UAP
LAP( 24 bit) NAP (16 bit)
(8 bit)
11/09/2023 13
Physical Channels
Pseudo-random RF channel hopping sequence
Sequence is algorithmically derived from the Piconet
Master’s address
The frequency hopping in the Piconet physical channel is
determined by the Piconet clock
The maximum hop rate is 1600 hops/s in the connection
state and 3200 hops/s in the inquiry and page substates
4 Physical channel types are defined
11/09/2023 14
Physical Channel Types
Basic Channel
Adapted Physical Channel •The basic Piconet physical channel is
defined by the master of the Piconet.
•May be based on less than • The master controls the traffic on the
the full 79 frequencies of the Piconet physical channel by a polling
basic Piconet physical scheme
channel. •Characterized by a pseudo-random
hopping through all 79 RF channels.
Inquiry Scan Physical Page Scan Physical Channel
Channel
•Used in Page and Page Scan Modes
•Used in Inquiry and Inquiry
Scan Modes •Follows a slower hopping pattern than the
basic Piconet physical channel
•Same characteristics as
Page Scan Physical Channel •Short pseudo-random hopping sequence
through the RF channels.
11/09/2023 15
Active Mode
❑ Both master and slave actively participate on the channel.
❑ Up to seven slaves may be in the active mode at any given time.
❑ The master schedules the transmission based on traffic demands
to and from the different slaves. In addition, it supports regular
transmissions to keep slaves synchronized to the channel.
❑ Slaves in the active mode listen in the master-to-slave slots for
packets. These devices are known as active slaves. If an active
slave is not addressed, it may sleep until the next new master
transmission.
❑ Slaves can derive the number of slots the master has reserved for
transmission from the packet header
❑ Only the slave that is may return a packet in the next slave-to-
master slot. If no valid packet header is received, the slave may
only respond in its reserved SCO or eSCO slave-to-master slot
11/09/2023 16
Park Mode
❑ When a slave does not need to participate on the Piconet
channel, but still needs to remain synchronized to the
channel, it can enter PARK state
❑ The parked slave wakes up at regular intervals to listen to
the channel in order to re-synchronize and to check for
broadcast messages.
❑ Synchronization and channel access of the parked slaves
is done by means of a Beacon Train
11/09/2023 17
Beacon Train
Transmission of master-to-slave packets which the parked slaves
can use for re-synchronization
Carrying messages to the parked slaves to change the beacon
parameters
Carrying general broadcast messages to the parked slaves
Unparking of one or more parked slaves
11/09/2023 18
L2CAP Layer
✓ Abbreviation for Logical Link Control and Adaptation Protocol.
✓ A link-layer protocol between Bluetooth devices with multiple
services
✓ Makes use of ACL logical transport (does not support SCO).
✓ Passes data to the HCI, or to the directly to the Link Manager
in a hostless system
✓ Note that L2CAP transports data, not audio (though protocols
like VoIP are regarded as data and would use L2CAP if
transferred over BT systems)
11/09/2023 19
L2CAP Configuration Options
✓ The L2CAP_ConfigReq commands packet can negotiate terms with
other L2CAP layers, those options will affect the MTU, Flush
timeout and QoS.
✓ Maximum Transmission Unit: The maximum transmission size in
bytes the device is willing to accept, it will reject any payload larger
with a reject message containing the MTU it can handle
✓ Flush Timeout :The ammount of time in ms the device will try to
transmit an L2CAP packet segment before it gives up. If the
segment can't get through during that period – the entire packet is
flushed.
✓ Quality of Service): The quality of service option can select best
effort, or guaranteed QoS. Values such as token rate, token bucket
size, peak bandwidth, latency and delay variation can also be
negotiated.
11/09/2023 20
L2CAP Diagram
11/09/2023 21