1 <html devsite> 2 <head> 3 <title>Bluetooth</title> 4 <meta name="project_path" value="/_project.yaml" /> 5 <meta name="book_path" value="/_book.yaml" /> 6 </head> 7 <body> 8 <!-- 9 Copyright 2017 The Android Open Source Project 10 11 Licensed under the Apache License, Version 2.0 (the "License"); 12 you may not use this file except in compliance with the License. 13 You may obtain a copy of the License at 14 15 http://www.apache.org/licenses/LICENSE-2.0 16 17 Unless required by applicable law or agreed to in writing, software 18 distributed under the License is distributed on an "AS IS" BASIS, 19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 See the License for the specific language governing permissions and 21 limitations under the License. 22 --> 23 24 25 26 <img style="float: right; margin: 0px 15px 15px 15px;" src="images/ape_fwk_hal_bluetooth.png" alt="Android Bluetooth HAL icon"/> 27 28 <p>Android provides a default Bluetooth stack that is divided into two layers: The Bluetooth Embedded System (BTE), 29 which implements the core Bluetooth functionality, and the Bluetooth Application Layer (BTA), which 30 communicates with Android framework applications.</p> 31 32 <p>To fully leverage the <a href="http://developer.android.com/about/versions/android-5.0.html#BluetoothBroadcasting">Bluetooth Low Energy APIs</a> 33 added in Android 5.0, you should implement the <a href="Android-6.0-Bluetooth-HCI-Reqs.pdf">Android 6.0 Bluetooth HCI Requirements</a>. 34 That document initially was provided as the <a href="Android-5.0-Bluetooth-HCI-Reqs.pdf">Android 5.0 Bluetooth HCI Requirements</a>.</p> 35 36 <h2 id="architecture">Architecture</h2> 37 <p>A Bluetooth system service communicates with the Bluetooth stack through JNI and with applications through Binder IPC. The system service provides developers with access to various Bluetooth profiles. The following diagram shows the general structure of the Bluetooth stack: 38 </p> 39 40 <img src="images/ape_fwk_bluetooth.png" alt="Android Bluetooth architecture" id="figure1" /> 41 <p class="img-caption"> 42 <strong>Figure 1.</strong> Bluetooth architecture 43 </p> 44 45 <dl> 46 <dt>Application framework</dt> 47 <dd>At the application framework level is application code, which utilizes the <a 48 href="http://developer.android.com/reference/android/bluetooth/package-summary.html">android.bluetooth</a> 49 APIs to interact with the Bluetooth hardware. Internally, this code calls the Bluetooth process through 50 the Binder IPC mechanism.</dd> 51 52 <dt>Bluetooth system service</dt> 53 <dd>The Bluetooth system service, located in <code>packages/apps/Bluetooth</code>, is packaged as an Android 54 app and implements the Bluetooth service and profiles at the Android framework layer. This app 55 calls into the HAL layer via JNI.</dd> 56 57 <dt>JNI</dt> 58 <dd>The JNI code associated with <a 59 href="http://developer.android.com/reference/android/bluetooth/package-summary.html">android.bluetooth</a> is located in 60 <code>packages/apps/Bluetooth/jni</code>. The JNI code calls into the HAL layer and receives 61 callbacks from the HAL when certain Bluetooth operations occur, such as when devices are 62 discovered.</dd> 63 64 <dt>HAL</dt> 65 <dd>The hardware abstraction layer defines the standard interface that the <a 66 href="http://developer.android.com/reference/android/bluetooth/package-summary.html">android.bluetooth</a> APIs 67 and Bluetooth process call into and that you must implement to have your Bluetooth hardware 68 function correctly. The header file for the Bluetooth HAL 69 is <code>hardware/libhardware/include/hardware/bluetooth.h</code>. Additionally, please review all of the 70 <code>hardware/libhardware/include/hardware/bt_*.h</code> files. 71 </dd> 72 73 <dt>Bluetooth stack</dt> 74 <dd>The default Bluetooth stack is provided for you and is located in 75 <code>system/bt</code>. The stack implements the generic Bluetooth HAL and 76 customizes it with extensions and configuration changes. 77 </dd> 78 79 <dt>Vendor extensions</dt> 80 <dd>To add custom extensions and an HCI layer for tracing, you can create a libbt-vendor module 81 and specify these components. 82 </dd> 83 84 </dl> 85 86 <h2 id="implementing">Implementing the HAL</h2> 87 <p>The Bluetooth HAL is located in <code>/hardware/libhardware/include/hardware/bluetooth.h</code>. 88 Thus, the <code>bluetooth.h</code> file contains the basic interface for the Bluetooth stack, and you must implement its functions.</p> 89 90 <p>Profile-specific files are located in the same directory. For details, see the <a 91 href="/reference/hal/dir_6b11132f1a015b03f2670f21bef1d871.html">HAL File Reference</a>.</p> 92 93 <p>The following is a <strong>partial</strong> list of the profile-related 94 files. For the <strong>complete set</strong>, see the <code>/hardware/libhardware/include/hardware/</code> directory:</p> 95 96 <ul> 97 <li><code>bt_av.h</code>: Includes the interface definition for the A2DP profile.</li> 98 <li><code>bt_gatt.h</code>, <code>bt_gatt_client.h</code>, and <code>bt_gatt_server.h</code>: These include the interface definition for the GATT profile.</li> 99 <li><code>bt_hf.h</code>: Includes the interface definition for the HFP profile.</li> 100 <li><code>bt_hh.h</code>: Includes the interface definition for the HID host profile.</li> 101 <li><code>bt_hl.h</code>: Includes the interface definition for the HDP profile.</li> 102 <li><code>bt_mce.h</code>: Includes the interface definition for the MAP profile.</li> 103 <li><code>bt_pan.h</code>: Includes the interface definition for the PAN profile.</li> 104 <li><code>bt_rc.h</code>: Includes the interface definition for the AVRCP profile.</li> 105 <li><code>bt_sock.h</code>: Includes the interface definition for RFCOMM sockets.</li> 106 </ul> 107 108 <p>Keep in mind that your Bluetooth implementation is not constrained to the features 109 and profiles exposed in the HAL. You can find the default implementation located 110 in the Bluetooth stack in the <code>system/bt</code> directory, 111 which implements the default HAL and also extra features and customizations.</p> 112 113 114 <h2 id="customizing">Customizing the Native Bluetooth Stack</h2> 115 <p>If you are using the default Bluetooth stack, but want to make a few customizations, you can 116 do the following:</p> 117 <ul> 118 <li>Custom Bluetooth profiles - If you want to add Bluetooth profiles that do not have 119 HAL interfaces provided by Android, you must supply an SDK add-on download to make the profile available to app developers, make the APIs available in the Bluetooth system process app (<code>packages/apps/Bluetooth</code>), and add them to the default stack (<code>system/bt</code>).</li> 120 <li>Custom vendor extensions and configuration changes - You can add things such as extra AT commands or device-specific configuration changes 121 by creating a <code>libbt-vendor</code> module. See the <code>/hardware/broadcom/libbt</code> directory 122 for an example.</li> 123 <li>Host Controller Interface (HCI) - You can provide your own HCI by creating a <code>libbt-hci</code> module, which 124 is mainly used for debug tracing. See the <code>external/bluetooth/hci</code> directory for an example.</li> 125 </ul> 126 127 </body> 128 </html> 129