First, the basic knowledge of Bluetooth
1. Bluetooth is a wireless technology standard that enables short-distance data exchange between fixed devices, mobile devices, and building personal domain networks. Bluetooth based device low-cost transceiver chip, transmission distance near, low power consumption.
2. Microwave Band: UHF radio waves using ISM bands from 2.402GGHz to 2.480GHz (including protection bands)
3. Bluetooth uses frequency hopping technology. The transmitted data is cut into a packet, and data packets are transmitted via 79 specified Bluetooth channels respectively.
Each channel has a bandwidth of 1MHz.
4. Bluetooth is a packet-based protocol with a master-slave architecture.
A master device can communicate with up to seven slave devices in the same Pico network. All devices share the clock of the master device.
5. The Bluetooth machine can be connected with up to seven slave devices when it is used as a main device.
The device is generally connected to only one master device. The role can be converted between devices through protocols.
6. Power Range
Category |
Maximum power capacity |
Range (m) |
(MW) |
(dBm) |
1 |
100 |
20 |
~100 |
2 |
2.5 |
4 |
~ |
3 |
1 |
0 |
To |
7. Data throughput
Version number |
Data rate |
Maximum application throughput |
1.2 |
1mbit/s |
>80 kbit/s |
2.0 + EDR |
3 Mbit/s |
>80 kbit/s |
3.0 + HS |
-Mbit/s |
Please refer to 3.0 + HS |
4.0 |
-Mbit/s |
Please refer to 4.0 LE |
7. About range: Most Bluetooth applications are designed for indoor environments, because the attenuation of the wall and the signal fading due to signal reflection make the range far less than the range specified by the Bluetooth product. The two 1-class devices with high sensitivity and transmitting power are connected to a range that is much higher than the average 100m, depending on the throughput required by the application. Some devices have a range of up to 1km or higher in open environments.
8. Latest Version number: Bluetooth 4.2, published on December 2, 2014 (until March 22, 2016)
9. Each device has a unique 48-bit address.
However, these addresses are not displayed in the connection request.
However, users can name their Bluetooth device (Bluetooth device name), which can be displayed in the scan results and paired devices list of other devices.
10. Bluetooth frame
11. The Bluetooth core system consists of two parts: host and controller. The controller includes the primary controller and the secondary controller.
12. Bluetooth BASIC Specification
There are four basic specifications (profiles) for Bluetooth, and these specifications are used by other specifications.
They are:
(1) GAP profile:generic Access profile. This profile ensures that different Bluetooth products can discover each other and establish a connection.
(2) SDAP profile:service Discovery application profile, through which a Bluetooth device can find services provided by other Bluetooth devices. and query-related information.
(3) SPP profile:serial Port profile. Analog serial communication
(4) Goep Profile:generic Object Exchange profile, general purpose exchange.
The name of this profile is somewhat convoluted. It defines the transmission of data, including synchronization, file transfer. or push other data. Can understand the content-independent Transport layer protocol. Can be used to transfer custom data objects regardless of the application.
13. Bluetooth Application Specification
Br/edr profile (Android support)
(1) A2DP (Advanced Audio Distribution Profile)
Advanced Audio, stereo specifications, including A2DP sink and A2DP Source;
(2) AVRCP (Audio/video Remote Control profile)
Audio and Video remote control specification. Music playback, pause and so on.
(3) HFP (Hands-free profile)
Hands-free specification for Bluetooth calls;
(4) HSP (Headset profile)
Headset specifications. for Bluetooth headset;
(5) HID (Human Interface Device profile)
Human interface Equipment specification, divided into HID host and HID device;
(6) OPP (Object Push profile)
The object push specification. Used to transfer files;
(7) PAN (Personal area Networking profile)
Personal LAN specification, Bluetooth network sharing.
(8) PBAP (Phone book Access profile)
Telephone Directory Access specification;
(9) MAP (Message Access profile)
Information access specification;
(HDP) (Health Device profile)
Health equipment specifications, and some health equipment to communicate;
(one) BLP (Blood pressure profile)
blood pressure regulation;
() FMP (Find Me profile)
Find me standard;
HRP (heart rate profile)
Heart rate specification;
(+) Hogp (HID-over-GATT profile)
The HID specification based on GATT.
() TIP (Time profile)
TIME specification.
Second, the hardware part of the knowledge
1. Basic processor
(1) Ap:applicationprocessor application processor
CPU with ARM architecture is typically responsible for performing OS and some specific settings and loading the boot presets. For example, a tablet that does not have a phone function simply runs the Android or WIN8 system. Just want the AP to be able.
(2) Bp:basebandprocessor baseband processor
The architecture is simple relative to the AP. Handles functions such as phones and messages.
(3) Cp:coprocessor Processor co-processor
Baseband Chip plus coprocessor (CP, typically a multimedia accelerator), is used to speed up auxiliary processors by handling some of the workload of the main CPU.
(4) Rf:radio frequency RF part
The transmitting part of the wireless signal, such as telephone, Bluetooth, WiFi signal transmitting, receiving.
2. Simple Tablet PC block diagram
AP Chip + Peripheral +android System
3. Bluetooth hardware interface block Diagram (for example, UART interface chip)
(1) UART part: Serial port. Mainly responsible for transmitting data;
(2) PCM part: Voice interface, used for voice transmission data during call;
(3) Power part: Power control.
Third, the software part of the knowledge
1.Android Bluetooth Architecture bluetooth® Code Architecture
1.1 Serial Port Driver
Linux kernel bluetooth driver, Linux kernel's Bluetooth protocol layer
Adapter for 1.2BlueZ
(1) Kernel code: consists of driver and BlueZ core protocol
Drivers: In fact, the kernel source code Kernel/driver/bluetooth (including Linuxkernel for various interfaces) includes: Usbdriver, USB driver and Otherdriver.
Bluetooth protocol: Real now kernel source code kernel/net/bluetooth.
Includes: HCI, L2cap, HID, Rfcomm, SCO, SDP, BNEP and other protocols.
(2) User Configuration program and tool set:
Contains the application interface and the BlueZ toolset.
BlueZ provides a library of functions and an application interface. Easy to program ape to develop Bluetooth applications. BlueZ Utils is the main toolset for initializing and controlling Bluetooth devices.
1.3 Bluetooth-related application interfaces
Android.buletooth each class in the package (the contents of Bluetooth in the frame layer)
Class name |
Role |
Bluetoothadapter |
Local Bluetooth device adaptation class, all of the Bluetooth operation through the class is complete |
Bluetoothclass |
Used to describe the type, characteristics, and other information of the remote device. |
Bluetoothdevice |
Bluetooth device class. Represents the remote device in the process of Bluetooth communication |
Bluetoothserversocket |
Bluetooth device server, similar to ServerSocket |
Bluetoothsocket |
Bluetooth device client, similar to socket |
Bluetoothclass.device |
Bluetooth about device information |
BluetoothClass.Device.Major |
Bluetooth Device Management |
Bluetoothclass.service |
Bluetooth related services |
Attached: Google's official Bluetooth code frame diagram
2.Hciattach: Establish a data connection channel between the serial port and the Bluetooth protocol layer
3. Transfer Data Flow
(1) The UART port obtains the data of the Bluetooth module;
(2) The UART port passed Ldisc to Hci_uart;
(3) The Hci_uart passed on to the H4;
(4) H4 passed to HCI layer;
(5) The HCI layer is passed to the L2CAP layer;
(6) The L2CAP layer is then passed on to Rfcomm.
4.BluetoothAdapter: Bluetooth Adapter
Until we establish a bluetoothsocket connection. You have to keep working on it.
Action constants in the Bluetoothadapter
Action_discovery_finished |
Completed Bluetooth search |
action_discovery_started |
Already started searching for Bluetooth devices |
Action_local_name_changed |
Change the name of Bluetooth |
Action_request_discoverable |
Requests can be searched |
Action_request_enable |
Request to start Bluetooth |
Action_scan_mode_changed |
Scan mode has changed |
Action_state_changed |
The status has changed |
Action_connection_state_changed |
|
5. Constants Introduction
(1) Switch status value
Bluetooth off: int State_off, with a value of 10, the Bluetooth module is turned off;
bluetooth on: int state_turning_on, the value is 11, the Bluetooth module is open;
bluetooth on: int state_on, the value is 12, the Bluetooth module is in the open state;
bluetooth on: int State_turning_off, the value is 13, the Bluetooth module is shutting down;
Bluetooth switch Status Order: State_off---state_turning_on--state_on-->state_turning_off--State_off;
(2) Scan status value
No functional status: int Scan_mode_none, the value is 20, query scanning and page scanning are invalid, in this state, the Bluetooth module can not scan other devices, nor visible;
Scan status: Int scan_mode_connectable, value is 21, query scan is invalid, page scan is valid, the Bluetooth module in this state can scan other devices, from the visibility only to the paired Bluetooth device visible, only paired device ability to actively connect the device;
Visible state: Int scan_mode_connectable_discoverable, value 23, Query scan and page scan are all valid;
Query scanning function: Other devices can scan to this device, refers to the visibility of the visible;
Page scanning function: Can actively scan other devices;
(3) Radio received by Bluetooth operation
Start Search broadcast: String action_discovery_started, the Bluetooth adapter starts to search the remote device, the value is "Android.bluetooth.action.DISCOVERY_START", after the Bluetooth adapter starts to search, Will have 12 seconds of query scanning (12s visible), query scanning after the page scan (active search), the need for Bluetooth permission;
If you search for a Bluetooth device, you will receive a Bluetoothdevice.action_found broadcast that allows you to get the Bluetoothdevice object stored in it from the intent, Intent.getparcelableextra ( Bluetoothdevice.extra_device);
Device lookup is very resource-intensive, in the process of finding, can not connect to the remote Bluetooth device, the existing connection also to limit the bandwidth, if you want to run other than the lookup operation, it is better to call Canceldiscovery ();
Search Complete Broadcast: String action_discovery_finished, Bluetooth s adapter complete the search issued by the broadcast,?
The value is "Android.bluetooth.adapter.action.DISCOVERY_FINISHED", need Bluetooth permission;
Bluetooth name change broadcast: String action_local_name_changed, the local Bluetooth adapter changed its name to a value of "android.bluetooth.adapter.action.LOCAL_NAME_ CHANGED ", notice the change is the device name, not the remote device. This broadcast includes a Extra_local_name additional domain that requires Bluetooth access;
Scan mode change broadcast: String action_scan_mode_changed, the Bluetooth module scanning mode has changed, the value is "Android.bluetooth.adapter.action.SCAN_MODE_CHANGED", The intent object includes Extra_scan_mode and Extra_previous_scan_mode, and two additional domains are both new and old scanning modes, which can be used to make different operation according to the different scanning modes, which require Bluetooth permission;
Switch mode change broadcast: String action_state_changed, the Bluetooth module is turned on or off, the value is "Android.bluetooth.adapter.action.STATE_CHANGED", The broadcast intent includes the Extra_state and extra_previous_state two additional domains, which require Bluetooth permission;
(4) Broadcast of Bluetooth operation request
Turn on Bluetooth: String action_request_enable, turn on Bluetooth, value is "Android.bluetooth.adapter.action.REQUEST_ENABLE",
Intentintent = new Intent (bluetoothadapter.action_request_enable);
Startactivityforresult (Intent,requestcode);
Able to process the results in the Onactivityresult () method in the activity, assuming that the Bluetooth module opens successfully, the result is returned RESULT_OK; If the Bluetooth module fails to open, the result code result_canceled is returned;
Open and close the Bluetooth module, can be action_state_changed broadcast to listen;
Bluetooth visible: String action_request_discoverable, make Bluetooth visible, the value is "Android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", The default visible time is 120s, can join the additional domain in the broadcast, set the arbitrary visible time, the additional domain is extra_discoverable_duration, need the Bluetooth permission;
Able to process the results in the Onactivityresult () method in activity, assuming that the Bluetooth module setting is visible, the result is returned RESULT_OK; Assuming that the Bluetooth module settings are not visible, the result code result_canceled is returned;
Intentdiscoverableintent = new Intent (bluetoothadapter.action_request_discoverable);
Discoverableintent.putextra (bluetoothadapter.extra_discoverable_duration,0);
Startactivityforresult (Discoverableintent,resultcode)
(5) Additional domains
Additional domain is placed in intent, using Intent.putextra (additional domain, added value) method to join the additional domain;
Scan mode Additional domain: The value of these two additional fields is scan mode, can be scan_mode_none, scan_mode_connectable,scan_mode_connectable_discoverable;
String Extra_scan_mode: The value is "Android.bluetooth.adapter.extra.SCAN_MODE";
String Extra_previous_scan_mode: The value is "Android.bluetooth.adapter.extra.PREVIOUS_SCAN_MODE";
Switch state Additional domain: The value of these two additional fields is the switch state, can be State_off, state_on, State_turning_off, state_turning_on;
String Extra_state: The value is "Android.bluetooth.adapter.extra.STATE";
String Extra_previous_state: The value is "Android.bluetooth.adapter.extra.PREVIOUS_STATE";
Bluetooth name Additional domain: String extra_local_name, which stores the added value of the action_local_name_changed additional domain, with the value "android.bluetooth.adapter.extra.LOCAL_ NAME ";
Visible time Additional field: String Extra_discoverable_duration, which holds the visible duration of the action_request_discoverable broadcast, with a value of " Android.bluetooth.adapter.extra.DISCOVERABLE_DURATION ";
(6) Error code
int error, this value is used to mark errors, convenient for their own use, no practical significance.
Establishment of the 6.A2DP connection
A2dp_connect upper-Level Code Flow table
Android Bluetooth Module Learning notes