Android realization of PBAP synchronization phone book for Bluetooth phone

Android implementation of PBAP synchronization phone book

Insert picture description here

Phonebook synchronization in Bluetooth phones basically includes two processes: synchronizing contacts + synchronizing call records , then we will briefly talk about how the phonebook of the Bluetooth phone (PCE) in the Android system is synchronized.

The Android source code version referred to in this article: Android 9 (P version) , students who are familiar with the Android source code architecture should be aware of the hierarchical relationship of the Bluetooth service in it, because the existing Android API interface BluetoothPbapClient has not yet integrated the corresponding Sync phonebook data interface, even if the Bluetooth API sync phonebook interface in the latest Android 11 version to be released is still not integrated, but the relevant data synchronization analysis in the Bluetooth service layer com.android.bluetooth has been implemented, so we need us Add the API yourself to open up the connection between the framework and the service. This part is easy to implement and will not be explained.

Phonebook synchronization is inseparable from the connection of the PBAP protocol. The article "PBAP Protocol Connection of Bluetooth Phone " has summarized the connection process in the Android system. Students who are not clear can check it first. The connection process is mainly to create a socket for communication between the Bluetooth service layer and the bluedroid protocol stack. The data synchronization in this article is definitely inseparable from this socket.

After opening up the connection between the framework layer and the Bluetooth service layer, the synchronization command is first sent to PbapClientService, and we use this as a starting point for analysis.

The current Android source code integrates the process of synchronizing contacts and call records together, and the number of synchronized objects is all synchronized (except for excluding the local number information when synchronizing contacts), and it is easy to understand all contact information , But phones with a lot of call records (phones with tens of thousands of call records...) do not need to synchronize all of them (very time-consuming), so you can re-adapt the logic of PBAP synchronization in the Bluetooth service layer according to your needs . For example, only synchronize the latest N call records and so on.

Let us first take a brief look at the sequence diagram of synchronizing the phone book in the Android system:
Insert picture description here

The main process of PBAP's PCE to synchronize data in the Android system is as described above, and the code is not posted here (I don’t like boring explanations of the code), and students who need to track the code flow follow the above sequence diagram to analyze step by step. .

It can be seen in the sequence diagram that the main synchronization process is completed in the sub-thread PbapClientConnectionHandler of PBAP connection, disconnection, and synchronization of data , and the command is issued through BluetoothPbapRequestPullPhoneBook and the reply data is analyzed.

Data analysis is mainly done through the related methods provided in the com.android.vcard package. Interested students are recommended to focus on the analysis of the VCardEntry class.

The data will eventually be stored in the database that comes with the Android system. Everyone must have a certain foundation for database operation, so I won't introduce it.
Contact database: /data/data/com.android.providers.contacts/databases/contacts2.db
Call log database: /data/data/com.android.providers.contacts/databases/calllog.db

A little suggestion : After
applications such as Bluetooth phones successfully issue synchronization commands through the framework's new interface, they still need to know the synchronization status? Increase error process processing for synchronization failure; successfully extract data from the system database for processing or display.

Therefore, the Bluetooth service layer needs to notify the application of the synchronization result after the synchronization is over. The simplest method in the Android system is to broadcast.

Interested friends welcome private messages and leave a message to discuss together, learn together, and make progress together!

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/108618835