The Bluetooth protocol stack on Android 4.4 uses the BRCM and Google co-developed bluedroid, instead of the previous BlueZ.
One, Bluetooth source distribution (based on Android 4.4)
1. Packages/apps/settings/src/com/android/settings/bluetooth
bluetooth Settings code
2. Packages/apps/bluetooth
BT application layer code, and BT profile (eg: a2dp,gatt,hdp,hfp,hid,map,opp,pan,pbap ... ) Upper Code
Packages/apps/bluetooth/jni
3. Frameworks/base/core/java/android/bluetooth
Framework layer-related Java code and AIDL
4. External/bluetooth/bluedroid
BRCM and Google co-developed the official Bluetooth protocol stack
5. Linux/kernel/drivers/bluetooth
6. Linux/kernel/net/bluetooth
7. The following is the recent project Intel platform
Hardware/broadcom/libbt
Hardware/libhardware
VENDOR/INTEL/FW/PRIVATE/BT Manufacturer BT Firmware
Second, Bluetooth common categories and related profiles
A2DP: Advanced Audio distribution Profiles Bluetooth audio transmission model agreement
Bluetooth stereo, and Bluetooth headset listen to the song about those, there is also a avrcp--(Audio/video Remote control Profile) audio/video remotely controlled configuration file, is used to listen to the song when paused, up and down songs selected
GATT: Generic Attribute Profile Common property profiles
The GATT is based on the ATT protocol, which has been specifically optimized for BLE devices, using as little data as possible in the transfer process. Each property has a unique UUID, and the properties are transferred as characteristics and services
Https://developer.bluetooth.org/TechnologyOverview/Pages/GATT.aspx
HDP: Bluetooth Health Device profile bluetooth® for medical applications
HFP : Hands-free profile and phone-related, Bluetooth answer, hang up the phone
HID : Human Interface Device
The protocol, characteristics and usage procedures of Bluetooth in human-computer interface device are defined. Typical applications include Bluetooth mouse, Bluetooth keyboard, Bluetooth gamepad, and more. The protocol is adapted from USB HID Protocol
MAP : Message Access Profile
OPP : Object Push Profile
PAN : Personal area Network profile
Describes how two or more Bluetooth devices form an instant network, and the network-related serial port feature (SPP), Dial-Up Networking function (DUN)
PBAP: Phonebook access profile phone book accessing protocol
Third, Enable Bluetooth
1. Service Startup:
Frameworks/base/services/java/com/android/server/systemserver.java
When the system starts, the Bluetooth service Management Bluetoothmanagerservice service is registered in Systemserver:
Bt Services
Other processes invoke the service through the binder mechanism, which belongs to the Integrated services management class, including Adapterservice startup, Bluetooth adapter adapter management, and so on.
2. Bluetoothadapter
The Bluetooth enable for Android is provided by Bluetoothadapter. You only need to call Bluetoothadapter.enable () to start Bluetooth. I'll analyze the process below
Frameworks/base/core/java/android/bluetooth/bluetoothadapter.java
Mmanagerservice.enable ()
Mmanagerservice is actually a proxy of Bluetoothadapter,
Getdefaultadapter
Bluetoothadapter
3. Bluetoothmanagerservice
Frameworks/base/services/java/com/android/server/bluetoothmanagerservice.java
Bluetoothmanagerservice:enable ()
Sendenablemsg
Sendenablemsg to Handlemessage processing, you can see case message_enable: Call the Handleenable
Handleenable
You can see that the mbluetooth.enable () is called.
4. Adapterservice,adapterstate
Packages/app/bluetooth/src/com/android/bluetooth/btservice/adapterservice.java
Adapterservice:enable ()
The use of statemachine here, it will be in the Adapterstate processmessage processing (StateMachine is the state machine, in different states, received the same event, do different things), direct search Uer_turn_ On you can see the following:
user_turn_on
The next step is to call the Adapterservice.processstart ()
Processstart
Setprofileservicestate (supportedprofileservices,bluetoothadapter.state_on); is used to turn on Bluetooth profile, which can be seen in log:
BT Profile Log
Then you can see: Madapterstatemachine.sendmessage (Madapterstatemachine.obtainmessage (adapterstate.started));
Handed over to the pendingcommandstate under the ProcessMessage treatment
Case STARTED
by Madapterservice.enablenative (); You can see/*package*/native Boolean enablenative ();
This is where the JNI comes in.
5. JNI Invocation
Enablenative () is in Packages/apps/bluetooth/jni/com_android_bluetooth_btservice_adapterservice.cpp
Enablenative
static const bt_interface_t *sbluetoothinterface = NULL;
bt_interface_t definition
bt_interface_t defined in HARDWARE/LIBHARDWARE/INCLUDE/HARDWARE/BLUETOOTH.H
Sbluetoothinterface is initialized in Classinitnative (), this function probably does the following things:
1), Registered Java callback function (that is, the current layer has turned on Bluetooth, and then to notify the upper level, Bluetooth has been turned on, the Java layer can send Bluetooth open broadcast. )
2), initialize the HAL interface of the Bluetooth module.
3), Get Sbluetoothinterface
6. Bluedroid->bluetooth.c
External/bluetooth/bluedroid/btif/src/bluetooth.c
Sbluetoothinterface->enable (); will be transferred to the bottom.
Bluetooth:enable ()
Next Call: external/bluetooth/bluedroid/btif/src/btif_core.c
Btif_enable_bluetooth
External/bluetooth/bluedroid/main/bte_main.c
Bte_main_enable
Bte_hci_enable
Let's take a look at the bt_hc_if->set_power, and do some initialization bluedroid before.
by Bt_hc_if->set_power (bt_hc_chip_pwr_on), it can be seen that:
Static bt_hc_interface_t *bt_hc_if=null;
Below is the initialization of the BT_HC_IF
Bte_main_in_hw_init
External/bluetooth/bluedroid/hci/src/bt_hci_bdroid.c
Bt_hc_get_interface
Bluetoothhclibinterface
The above can be seen set_power
Set_power
Can be seen, Bt_vnd_if->op (Bt_vnd_op_power_ctrl, &pwr_state);
External/bluetooth/bluedroid/hci/src/bt_hw.c
bt_vendor_interface_t *bt_vnd_if=null;
BT_VND_IF initialization:
Init_vnd_if
The init_vnd_if () function can be seen in fact as a libbt-vendor.so interface. This is the library of vendor (chip maker)
Vendor is the chip supplier, after they do a piece of Bluetooth chip, need to provide some hardware-related actions, such as power up and down, set the baud rate and so on. However, these operations are generally not open to unlicensed. Bluedroid provides a unified interface bt_vendor_interface_t, the provider only need to implement the interface definition of Bluetooth-related operations can be handed to bluedroid to do the rest of the matter
The following is mainly Broadcom for example, we enter/hardware/inside:
$ find. -name android.mk |xargs grep LIBBT
./broadcom/libbt/android.mk:local_module: = Libbt-vendor
./broadcom/libbt/android.mk:local_module: = Libbt-vendor
./broadcom/libbt/android.mk:local_src_files: = $ (Ti_bt_vendor_path)/libbt-vendor-ti.c
./qcom/bt/android.mk:include $ (call All-named-subdir-makefiles,libbt-vendor)
./qcom/bt/libbt-vendor/android.mk:local_module: = Libbt-vendor
Or
$ GREP-NR Bt_vnd_op_power_ctrl
Broadcom/libbt/src/bt_vendor_brcm.c:147:case Bt_vnd_op_power_ctrl:
broadcom/libbt/src/bt_vendor_brcm.c:149:btvnddbg ("Op:bt_vnd_op_power_ctrl");
Qcom/bt/libbt-vendor/src/bt_vendor_qcom.c:105:case Bt_vnd_op_power_ctrl:
In BROADCOM/LIBBT/SRC/BT_VENDOR_BRCM.C
Op
Upio_set_bluetooth_power
static char *rfkill_state_path = NULL;
The Rfkill_state_path is initialized below.
Init_rfkill
The original was written in the Rfkill_state_path (/sys/class/rfkill/rfkill[x]/state) virtual device 1
Shell@android:/sys/class/rfkill/rfkill0 $ cat State
0//indicates Bluetooth is off
Shell@android:/sys/class/rfkill/rfkill0 $ cat State
1//Turn on Bluetooth to see
Rfkill is a standard wireless controlled virtual device under Linux, and Linux also provides Rfkill commands to view and control all registered wireless devices. They generate the corresponding virtual devices in/dev/(PC Linux) or/sys/class (typically Android).
Combining the log and bluetoothhclibinterface definitions below the Set_power, you can see the next proload->bthc_signal_event in the called Bluetoothhclibinterface ( Hc_event_preload)->bt_hc_worker_thread-"Userial_open (userial_port_1)->bt_vnd_if->op (BT_VND_OP_USERIAL _open, &fd_array);->userial_vendor_open ((TUSERIAL_CFG *) &userial_init_cfg);
Next up is hardware.c.
hw_config_start-"Hw_config_cback
Some of the logs are as follows:
Log
Below is the log of the Bluetooth-enabled logcat in minicom
Logcat
REF:
http://blog.csdn.net/baimy1985/article/details/8892410
http://my.oschina.net/u/1436933/blog/192060
http://blog.csdn.net/yutao52shi/article/details/12690353