Bluetooth in Android 4.2 and 4.3 (3): Enable Bluetooth

Source: Internet
Author: User

The following is an analysis of Bluetooth br/EDR enable process based on Android 4.2 code. The descrithadapter class represents the local device Bluetooth adapter, while the descrithdevice class represents the remote Bluetooth device. In Android 4.3, we introduced a new class named explorthmanager, which is a high-level manager used to "to obtain an instance of an Elastic thadapter ".
And conducting CT overall Bluetooth management ".

The Bluetooth enable process is complex and has many layers. The best analysis method is to read the code against the Bluetooth log output by logcat. First, we will introduce the following enable process in general. The entry on the UI is settings. When the Bluetooth switch is switched, the Bluetooth enable process is started, and then enable Bluetooth hardware is disabled by the bluedroid. When the Bluetooth hardware enabled, the enabled message is uploaded from the bluedroid layer to the UI Layer. After receiving the message, settings can update the status of the Bluetooth switch. The specific process is as follows:

  1. Reset thenabler class of settings (corresponding to the Bluetooth switch seen on the UI), get the descrithadapter representing the local device, and then call descrithadapter: Enable ().
  2. Javasthadapter is basically a wrapper and does not do anything specific. It directly calls descrithmanagerservice: Enable ().
  3. Pluthmanagerservice uses the Binder Mechanism to connect adapterservice, which will eventually cause adapterservice: Enable () to be called. Bluetoothmanagerservice also registers the callback function with adapterservice to receive the adapter state change message.
  4. Adapterservice maintains a state machine adapterstate. All work is done by driving the state machine. When adapterstate receives the user_turn_on message from adapterservice, it calls adapterservice: processstart () to start profie services initialization and Bluetooth hardware enable process. The status of the Bluetooth adapter isExport thadapter. state_turning_on.
  5. Each profile has a service. After each profile service is started, the adapterservice is notified. When adapterservice: processprofileservicestatechanged () confirms that all profile services have been started, the adapterstate. Started message is sent to the state machine adapterstate.
  6. The state machine adapterstate: pendingcommandstate: processmessage () Immediately calls adapterservice: enablenative () After receiving the adapterstate. Started message ().
  7. Adapterservice: enablenative () is the Bluetooth JNI interface used to enable Bluetooth. Enablenative () calls enable () of the Bluetooth Hal ().
  8. Bluedroid uses btif_enable_bluetooth () to enable the Bluetooth Hal ().
  9. After bluedroid completes enable Bluetooth hardware, it calls hal_cback () of Bluetooth JNI to call adapter_state_change_callback () through hal_cback in btif_enable_rjth_evt.
  10. Adapterstate converts the Bluetooth adapter statusDescrithadapter. state_onAnd the adapterstate: yyadapterstatechanged () is used to notify adapterservice.
  11. Adapterservice: updateadapterstate () will notify bluetoothmanagerservice through the callback function, and the adapter status has changed.
  12. When thmanagerservice confirms that the status changes, it sends an intent of descrithadapter. action_state_change.
  13. After receiving the intent, the reset thenabler of settings updates the Bluetooth switch status on the UI.
Note: The description of the above process does not include all function calls in this process, but provides key functions and States.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.