Analysis of the initialization process of the Bluetooth service layer in the Android system

Analysis of the initialization process of the Bluetooth service layer in the Android system

Insert picture description here
The Bluetooth system relies on Android, from top to bottom, with a clear hierarchy. In this article, we will briefly analyze the relevant procedures of the Bluetooth service layer initialization.

First of all, let’s understand that the Bluetooth module in the Android system is roughly divided into the following parts:
Insert picture description here
Friends who have a general understanding of the startup sequence of the Android system must know that the Bluetooth Manager Service, the management object of the Bluetooth service, will be initialized during the startup process of the System Server and is in the system process. Run in the system, and then perform different operations at different stages of system startup.

Therefore, the trigger timing of Bluetooth system initialization is mainly as follows:

  1. The Bluetooth service management is in the start phase of PHASE_ACTIVITY_MANAGER_READY, according to the state before the Bluetooth is turned off in Settings, if it is not turned off, the Bluetooth is automatically turned on.
  2. Third-party applications actively turn on Bluetooth through the BluetoothAdapter BluetoothAdapter.

But regardless of the trigger method, the execution process is the same for the Bluetooth service. Next, we mainly analyze the initialization process of the lower service layer. First, refer to the following sequence diagram: The
Insert picture description here
above sequence diagram has the following points that need to be focused Description:

1. The frame marked in yellow indicates the call of the bind Bluetooth service in BluetoothManagerService. Since it is bound to IBluetooth.class.getName(), it can be known according to the manifest file of Bluetooth.apk: packages\apps\Bluetooth\AndroidManifest.xml The service to be bound is AdapterService
Insert picture description here
2. When the com.android.bluetooth service process is started, the AdapterApp will be instantiated first and the Bluetooth JNI so library will be loaded.

3. The block diagram marked in green represents the Bluetooth protocol supported in the loading system, which is determined according to the Boolean value of each protocol field in the configuration file config.xml. Since the Bluetooth protocol is divided into two parts: Server and Client, the Android system will also configure the corresponding protocol according to the current system usage scenario:

  • Mobile phone (Server), configuration path A : packages\apps\Bluetooth\res\values\config.xml

  • Bluetooth headset, car and other equipment (Client), configuration path B : packages\services\Car\car_product\overlay\packages\apps\Bluetooth\res\values\config.xml

The actual commercial Android system may also have a configuration file added by the manufacturer. At this time, whether the Bluetooth protocol is loaded is the result of the joint action of these files.

Here I use Qualcomm's Android source code as a simple example. Qualcomm itself has also configured the Bluetooth protocol field under the following path, the configuration path C :
device\qcom\common\product\overlay\packages\apps\Bluetooth\res\values\config .xml

Therefore, the Bluetooth protocol configuration in the Android system for mobile phones is the result of the combined action of A+C, and the Bluetooth protocol configuration in the Android system for the car-mounted device is the result of the combined action of A+B+C.

The specific configuration of the Bluetooth protocol under these three paths is shown in the following screenshots.
Path A :

Insert picture description here

Path B :

Insert picture description here

Path C :
Insert picture description here

The priority of these three paths is referenced by the following rules:

  • For mobile devices: A<C
  • For car Bluetooth devices: A<B<C

That is, for a certain protocol, whether the protocol is configured in the Android Bluetooth system is finally subject to the configuration value in the high-priority Bluetooth protocol configuration file. If there is no such protocol field in the high-priority configuration file, the low priority is adopted. The configuration value in the level configuration file shall prevail, and so on, until the field corresponding to the Bluetooth protocol is clearly defined.

Therefore, combining the above three configuration files in Qualcomm's car Android system can conclude that the supported Bluetooth protocols are as follows:

  • profile_supported_a2dp_sink(true)—A2dpSinkService
  • profile_supported_pan(true)—PanService
  • profile_supported_gatt(true)—GattService
  • profile_supported_hfpclient(true)—HeadsetClientService
  • profile_supported_avrcp_target(true)—AvrcpTargetService
  • profile_supported_avrcp_controller(true)—AvrcpControllerService
  • profile_supported_sap(true)—SapService
  • profile_supported_pbapclient(true)—PbapClientService
  • profile_supported_mapmce(true)—MapClientService
  • profile_supported_opp(true)—BluetoothOppService

See the log screenshot for the Bluetooth protocol supported by the Bluetooth service layer Config.java loading:
Insert picture description here

After the above three points are briefly explained, the Bluetooth service and protocol stack are initialized, and the IBinder object of the Bluetooth service layer is returned to BluetoothManagerService through onBind() to continue the process of enabling Bluetooth enable.

The initialization of the Bluetooth service layer in this article is simply analyzed here. Later, we will continue to analyze the process of Bluetooth related modules. If you are interested in Bluetooth technology, don’t forget to pay attention to the wave. The exciting content is continuously updated, and you are welcome to leave a message. discuss.

For more interconnection technologies, please pay attention to the WeChat public account: Connectivity
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44260005/article/details/105701583