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 bands: UHF radio waves using ISM bands from 2.402GGHz to 2.480GHz (including protection bands)
3. Bluetooth uses frequency hopping technology to divide the transmitted data into packets and transmit the packets via 79 designated Bluetooth channels. The bandwidth of each channel is 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. From a device, it is generally connected to only one master device. The roles 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 |
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, and signal fading due to wall attenuation and signal reflection can 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 an open environment.
8. Latest Version: Bluetooth 4.2, posted 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, the user can name his Bluetooth device (the Bluetooth device name), which is displayed in the scan results and paired devices list of other devices.
10. Bluetooth frame
11. The Bluetooth core system comprises a host and a controller, wherein the controller includes a master controller and a 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, which 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, as well as 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 confusing, it defines the transmission of data, including synchronization, file transfer, or pushing other data. The Transport layer protocol, which can be understood as content-independent, can be used by any application to transfer its own defined data objects.
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 specifications, music playback, suspension, etc.;
(3) HFP (Hands-free profile)
Hands-free specification for Bluetooth calls;
(4) HSP (Headset profile)
Headset specification for Bluetooth headsets;
(5) HID (Human Interface Device profile)
Human interface Equipment specification, divided into HID host and HID device;
(6) OPP (Object Push profile)
Object push specification for transferring files;
(7) PAN (Personal area Networking profile)
Personal LAN specification, Bluetooth network sharing;
(8) PBAP (Phone book Access profile)
Phone book 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 running the OS and some specific settings and loading the boot presets. For example, a tablet with no phone function, only the Android or WIN8 system, as long as the AP is available.
(2) Bp:basebandprocessor baseband processor
The architecture is simple with respect to the AP, handling functions such as telephony and information.
(3) Cp:coprocessor Processor co-processor
The baseband chip plus coprocessor (CP, typically a multimedia accelerator), is used to speed up the auxiliary processor 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 data transmission;
(2) PCM part: Voice interface for voice data transmission 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: implemented in the kernel source code Kernel/driver/bluetooth (including Linuxkernel for various interfaces) include: Usbdriver, USB driver and Otherdriver.
Bluetooth protocol: Implemented in the kernel source code kernel/net/bluetooth. Including: HCI, L2cap, HID, Rfcomm, SCO, SDP, BNEP and other protocols.
(2) User Configuration program and tool set:
Includes the application interface and the BlueZ toolset. BlueZ provides a library of functions as well as an application interface to enable programmers 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 Bluetooth operations are done through this class |
Bluetoothclass |
Used to describe the type, characteristics and other information of the remote device |
Bluetoothdevice |
Bluetooth device class, which 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. Data transfer process
(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
It is not until we establish a bluetoothsocket connection that we continue to operate 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 the paired device can 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 be scanned 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 the 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 receive a bluetoothdevice.action_found broadcast, and you can get the Bluetoothdevice object stored in it from 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 perform 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,? value is "Android.bluetooth.adapter.action.DISCOVERY_FINISHED ", require 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 contains 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 contains Extra_scan_mode and Extra_previous_scan_mode, two additional domains are new and old scanning mode, here can be based on the different scanning mode to make different operation, need 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 contains the Extra_state and extra_previous_state two additional domains, requiring 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);
The results can be processed in the Onactivityresult () method in activity, and if the Bluetooth module is turned on successfully, will the result be 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, you can add additional fields in the broadcast, set any visible time, additional domain is extra_discoverable_duration, need Bluetooth permission;
The results can be processed in the Onactivityresult () method in activity, and if the Bluetooth module settings are visible, the results are returned RESULT_OK; If the Bluetooth module settings fail to be 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 fields are placed in intent, and additional fields are added using Intent.putextra (additional domain, added value) method;
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