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