| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| Jeff Sharkey | 73458a8 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 23 | import android.os.Binder; |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 24 | import android.os.Handler; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.os.IBinder; |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 26 | import android.os.Looper; |
| 27 | import android.os.Message; |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 33 | |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | /** |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | * Public API for controlling the Bluetooth Headset Service. This includes both |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 36 | * Bluetooth Headset and Handsfree (v1.5) profiles. |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | * |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 38 | * <p>BluetoothHeadset is a proxy object for controlling the Bluetooth Headset |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | * Service via IPC. |
| 40 | * |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 41 | * <p> Use {@link BluetoothAdapter#getProfileProxy} to get |
| 42 | * the BluetoothHeadset proxy object. Use |
| 43 | * {@link BluetoothAdapter#closeProfileProxy} to close the service connection. |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | * |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 45 | * <p> Android only supports one connected Bluetooth Headset at a time. |
| 46 | * Each method is protected with its appropriate permission. |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | */ |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 48 | public final class BluetoothHeadset implements BluetoothProfile { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | private static final String TAG = "BluetoothHeadset"; |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 50 | private static final boolean DBG = true; |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 51 | private static final boolean VDBG = false; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 53 | /** |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 54 | * Intent used to broadcast the change in connection state of the Headset |
| 55 | * profile. |
| 56 | * |
| 57 | * <p>This intent will have 3 extras: |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 58 | * <ul> |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 59 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 60 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li> |
| 61 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 62 | * </ul> |
| Jaikumar Ganesh | aa0427e | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 63 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 64 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 65 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 66 | * |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 67 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 68 | * receive. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 69 | */ |
| 70 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 71 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 72 | "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED"; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Intent used to broadcast the change in the Audio Connection state of the |
| 76 | * A2DP profile. |
| 77 | * |
| 78 | * <p>This intent will have 3 extras: |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 79 | * <ul> |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 80 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 81 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li> |
| 82 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 83 | * </ul> |
| Jaikumar Ganesh | aa0427e | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 84 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 85 | * {@link #STATE_AUDIO_CONNECTED}, {@link #STATE_AUDIO_DISCONNECTED}, |
| 86 | * |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 87 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission |
| 88 | * to receive. |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 89 | */ |
| 90 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 91 | public static final String ACTION_AUDIO_STATE_CHANGED = |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 92 | "android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED"; |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 93 | |
| Jaikumar Ganesh | f48cda5 | 2010-04-02 14:44:43 -0700 | [diff] [blame] | 94 | |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 95 | /** |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 96 | * Intent used to broadcast that the headset has posted a |
| 97 | * vendor-specific event. |
| 98 | * |
| 99 | * <p>This intent will have 4 extras and 1 category. |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 100 | * <ul> |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 101 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote Bluetooth Device |
| 102 | * </li> |
| 103 | * <li> {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD} - The vendor |
| 104 | * specific command </li> |
| 105 | * <li> {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} - The AT |
| 106 | * command type which can be one of {@link #AT_CMD_TYPE_READ}, |
| 107 | * {@link #AT_CMD_TYPE_TEST}, or {@link #AT_CMD_TYPE_SET}, |
| 108 | * {@link #AT_CMD_TYPE_BASIC},{@link #AT_CMD_TYPE_ACTION}. </li> |
| 109 | * <li> {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS} - Command |
| 110 | * arguments. </li> |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 111 | * </ul> |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 112 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 113 | * <p> The category is the Company ID of the vendor defining the |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 114 | * vendor-specific command. {@link BluetoothAssignedNumbers} |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 115 | * |
| 116 | * For example, for Plantronics specific events |
| 117 | * Category will be {@link #VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY}.55 |
| 118 | * |
| 119 | * <p> For example, an AT+XEVENT=foo,3 will get translated into |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 120 | * <ul> |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 121 | * <li> EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD = +XEVENT </li> |
| 122 | * <li> EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE = AT_CMD_TYPE_SET </li> |
| 123 | * <li> EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS = foo, 3 </li> |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 124 | * </ul> |
| 125 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission |
| 126 | * to receive. |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 127 | */ |
| 128 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 129 | public static final String ACTION_VENDOR_SPECIFIC_HEADSET_EVENT = |
| 130 | "android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT"; |
| 131 | |
| 132 | /** |
| 133 | * A String extra field in {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} |
| 134 | * intents that contains the name of the vendor-specific command. |
| 135 | */ |
| 136 | public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD = |
| 137 | "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD"; |
| 138 | |
| 139 | /** |
| 140 | * An int extra field in {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 141 | * intents that contains the AT command type of the vendor-specific command. |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 142 | */ |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 143 | public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE = |
| 144 | "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE"; |
| 145 | |
| 146 | /** |
| 147 | * AT command type READ used with |
| 148 | * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} |
| 149 | * For example, AT+VGM?. There are no arguments for this command type. |
| 150 | */ |
| 151 | public static final int AT_CMD_TYPE_READ = 0; |
| 152 | |
| 153 | /** |
| 154 | * AT command type TEST used with |
| 155 | * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} |
| 156 | * For example, AT+VGM=?. There are no arguments for this command type. |
| 157 | */ |
| 158 | public static final int AT_CMD_TYPE_TEST = 1; |
| 159 | |
| 160 | /** |
| 161 | * AT command type SET used with |
| 162 | * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} |
| 163 | * For example, AT+VGM=<args>. |
| 164 | */ |
| 165 | public static final int AT_CMD_TYPE_SET = 2; |
| 166 | |
| 167 | /** |
| 168 | * AT command type BASIC used with |
| 169 | * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} |
| 170 | * For example, ATD. Single character commands and everything following the |
| 171 | * character are arguments. |
| 172 | */ |
| 173 | public static final int AT_CMD_TYPE_BASIC = 3; |
| 174 | |
| 175 | /** |
| 176 | * AT command type ACTION used with |
| 177 | * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} |
| 178 | * For example, AT+CHUP. There are no arguments for action commands. |
| 179 | */ |
| 180 | public static final int AT_CMD_TYPE_ACTION = 4; |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * A Parcelable String array extra field in |
| 184 | * {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} intents that contains |
| 185 | * the arguments to the vendor-specific command. |
| 186 | */ |
| 187 | public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS = |
| 188 | "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_ARGS"; |
| 189 | |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 190 | /** |
| 191 | * The intent category to be used with {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} |
| 192 | * for the companyId |
| 193 | */ |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 194 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY = |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 195 | "android.bluetooth.headset.intent.category.companyid"; |
| 196 | |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 197 | /** |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 198 | * A vendor-specific command for unsolicited result code. |
| 199 | */ |
| 200 | public static final String VENDOR_RESULT_CODE_COMMAND_ANDROID = "+ANDROID"; |
| 201 | |
| 202 | /** |
| Jack He | 7f9c70b | 2017-06-20 17:07:40 -0700 | [diff] [blame] | 203 | * A vendor-specific AT command |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 204 | * |
| Jack He | 7f9c70b | 2017-06-20 17:07:40 -0700 | [diff] [blame] | 205 | * @hide |
| 206 | */ |
| Jack He | 0dfd69b | 2017-06-20 17:09:47 -0700 | [diff] [blame] | 207 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_XAPL = "+XAPL"; |
| 208 | |
| 209 | /** |
| 210 | * A vendor-specific AT command |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 211 | * |
| Jack He | 0dfd69b | 2017-06-20 17:09:47 -0700 | [diff] [blame] | 212 | * @hide |
| 213 | */ |
| 214 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV = "+IPHONEACCEV"; |
| 215 | |
| 216 | /** |
| 217 | * Battery level indicator associated with |
| 218 | * {@link #VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV} |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 219 | * |
| Jack He | 0dfd69b | 2017-06-20 17:09:47 -0700 | [diff] [blame] | 220 | * @hide |
| 221 | */ |
| 222 | public static final int VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL = 1; |
| 223 | |
| 224 | /** |
| 225 | * A vendor-specific AT command |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 226 | * |
| Jack He | 0dfd69b | 2017-06-20 17:09:47 -0700 | [diff] [blame] | 227 | * @hide |
| 228 | */ |
| Jack He | 7f9c70b | 2017-06-20 17:07:40 -0700 | [diff] [blame] | 229 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_XEVENT = "+XEVENT"; |
| 230 | |
| 231 | /** |
| 232 | * Battery level indicator associated with {@link #VENDOR_SPECIFIC_HEADSET_EVENT_XEVENT} |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 233 | * |
| Jack He | 7f9c70b | 2017-06-20 17:07:40 -0700 | [diff] [blame] | 234 | * @hide |
| 235 | */ |
| 236 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_XEVENT_BATTERY_LEVEL = "BATTERY"; |
| 237 | |
| 238 | /** |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 239 | * Headset state when SCO audio is not connected. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 240 | * This state can be one of |
| 241 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 242 | * {@link #ACTION_AUDIO_STATE_CHANGED} intent. |
| 243 | */ |
| Jaikumar Ganesh | 8ea890d | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 244 | public static final int STATE_AUDIO_DISCONNECTED = 10; |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 245 | |
| 246 | /** |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 247 | * Headset state when SCO audio is connecting. |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 248 | * This state can be one of |
| 249 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 250 | * {@link #ACTION_AUDIO_STATE_CHANGED} intent. |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 251 | */ |
| Jaikumar Ganesh | 8ea890d | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 252 | public static final int STATE_AUDIO_CONNECTING = 11; |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 253 | |
| 254 | /** |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 255 | * Headset state when SCO audio is connected. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 256 | * This state can be one of |
| 257 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 258 | * {@link #ACTION_AUDIO_STATE_CHANGED} intent. |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 259 | */ |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * Intent used to broadcast the headset's indicator status |
| 263 | * |
| 264 | * <p>This intent will have 3 extras: |
| 265 | * <ul> |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 266 | * <li> {@link #EXTRA_HF_INDICATORS_IND_ID} - The Assigned number of headset Indicator which |
| 267 | * is supported by the headset ( as indicated by AT+BIND command in the SLC |
| 268 | * sequence) or whose value is changed (indicated by AT+BIEV command) </li> |
| 269 | * <li> {@link #EXTRA_HF_INDICATORS_IND_VALUE} - Updated value of headset indicator. </li> |
| 270 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - Remote device. </li> |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 271 | * </ul> |
| Jack He | 48f6a8a | 2017-06-22 12:56:54 -0700 | [diff] [blame] | 272 | * <p>{@link #EXTRA_HF_INDICATORS_IND_ID} is defined by Bluetooth SIG and each of the indicators |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 273 | * are given an assigned number. Below shows the assigned number of Indicator added so far |
| Jack He | 48f6a8a | 2017-06-22 12:56:54 -0700 | [diff] [blame] | 274 | * - Enhanced Safety - 1, Valid Values: 0 - Disabled, 1 - Enabled |
| 275 | * - Battery Level - 2, Valid Values: 0~100 - Remaining level of Battery |
| 276 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to receive. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 277 | * |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 278 | * @hide |
| 279 | */ |
| 280 | public static final String ACTION_HF_INDICATORS_VALUE_CHANGED = |
| 281 | "android.bluetooth.headset.action.HF_INDICATORS_VALUE_CHANGED"; |
| 282 | |
| 283 | /** |
| Jack He | 48f6a8a | 2017-06-22 12:56:54 -0700 | [diff] [blame] | 284 | * A int extra field in {@link #ACTION_HF_INDICATORS_VALUE_CHANGED} |
| 285 | * intents that contains the assigned number of the headset indicator as defined by |
| 286 | * Bluetooth SIG that is being sent. Value range is 0-65535 as defined in HFP 1.7 |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 287 | * |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 288 | * @hide |
| 289 | */ |
| 290 | public static final String EXTRA_HF_INDICATORS_IND_ID = |
| 291 | "android.bluetooth.headset.extra.HF_INDICATORS_IND_ID"; |
| 292 | |
| 293 | /** |
| Jack He | 48f6a8a | 2017-06-22 12:56:54 -0700 | [diff] [blame] | 294 | * A int extra field in {@link #ACTION_HF_INDICATORS_VALUE_CHANGED} |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 295 | * intents that contains the value of the Headset indicator that is being sent. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 296 | * |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 297 | * @hide |
| 298 | */ |
| 299 | public static final String EXTRA_HF_INDICATORS_IND_VALUE = |
| 300 | "android.bluetooth.headset.extra.HF_INDICATORS_IND_VALUE"; |
| 301 | |
| Jaikumar Ganesh | 8ea890d | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 302 | public static final int STATE_AUDIO_CONNECTED = 12; |
| 303 | |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 304 | private static final int MESSAGE_HEADSET_SERVICE_CONNECTED = 100; |
| 305 | private static final int MESSAGE_HEADSET_SERVICE_DISCONNECTED = 101; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 306 | |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 307 | private Context mContext; |
| 308 | private ServiceListener mServiceListener; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | private IBluetoothHeadset mService; |
| Matthew Xie | 5e9fb03 | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 310 | private BluetoothAdapter mAdapter; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 312 | final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
| 313 | new IBluetoothStateChangeCallback.Stub() { |
| 314 | public void onBluetoothStateChange(boolean up) { |
| 315 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 316 | if (!up) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 317 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 318 | doUnbind(); |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 319 | } else { |
| 320 | synchronized (mConnection) { |
| 321 | try { |
| 322 | if (mService == null) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 323 | if (VDBG) Log.d(TAG, "Binding service..."); |
| Dianne Hackborn | 3875ec6 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 324 | doBind(); |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 325 | } |
| 326 | } catch (Exception re) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 327 | Log.e(TAG, "", re); |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 332 | }; |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 333 | |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | /** |
| 335 | * Create a BluetoothHeadset proxy object. |
| 336 | */ |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 337 | /*package*/ BluetoothHeadset(Context context, ServiceListener l) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | mContext = context; |
| 339 | mServiceListener = l; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 340 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 341 | |
| 342 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 343 | if (mgr != null) { |
| 344 | try { |
| 345 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 346 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 347 | Log.e(TAG, "", e); |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| Dianne Hackborn | 3875ec6 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 351 | doBind(); |
| 352 | } |
| 353 | |
| 354 | boolean doBind() { |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 355 | try { |
| 356 | return mAdapter.getBluetoothManager().bindBluetoothProfileService( |
| 357 | BluetoothProfile.HEADSET, mConnection); |
| 358 | } catch (RemoteException e) { |
| 359 | Log.e(TAG, "Unable to bind HeadsetService", e); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | } |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 361 | return false; |
| 362 | } |
| 363 | |
| 364 | void doUnbind() { |
| 365 | synchronized (mConnection) { |
| 366 | if (mService != null) { |
| 367 | try { |
| 368 | mAdapter.getBluetoothManager().unbindBluetoothProfileService( |
| 369 | BluetoothProfile.HEADSET, mConnection); |
| 370 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 371 | Log.e(TAG, "Unable to unbind HeadsetService", e); |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | /** |
| 378 | * Close the connection to the backing service. |
| 379 | * Other public functions of BluetoothHeadset will return default error |
| 380 | * results once close() has been called. Multiple invocations of close() |
| 381 | * are ok. |
| 382 | */ |
| Matthew Xie | 7891249 | 2012-03-22 17:18:37 -0700 | [diff] [blame] | 383 | /*package*/ void close() { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 384 | if (VDBG) log("close()"); |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 385 | |
| 386 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 387 | if (mgr != null) { |
| 388 | try { |
| 389 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 390 | } catch (Exception e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 391 | Log.e(TAG, "", e); |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| Benjamin Franz | 733656c | 2014-12-16 15:33:03 +0000 | [diff] [blame] | 394 | mServiceListener = null; |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 395 | doUnbind(); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /** |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 399 | * Initiate connection to a profile of the remote bluetooth device. |
| 400 | * |
| 401 | * <p> Currently, the system supports only 1 connection to the |
| 402 | * headset/handsfree profile. The API will automatically disconnect connected |
| 403 | * devices before connecting. |
| 404 | * |
| 405 | * <p> This API returns false in scenarios like the profile on the |
| 406 | * device is already connected or Bluetooth is not turned on. |
| 407 | * When this API returns true, it is guaranteed that |
| 408 | * connection state intent for the profile will be broadcasted with |
| 409 | * the state. Users can get the connection state of the profile |
| 410 | * from this intent. |
| 411 | * |
| 412 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 413 | * permission. |
| 414 | * |
| 415 | * @param device Remote Bluetooth Device |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 416 | * @return false on immediate error, true otherwise |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 417 | * @hide |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | */ |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 419 | public boolean connect(BluetoothDevice device) { |
| 420 | if (DBG) log("connect(" + device + ")"); |
| 421 | if (mService != null && isEnabled() && |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 422 | isValidDevice(device)) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | try { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 424 | return mService.connect(device); |
| 425 | } catch (RemoteException e) { |
| 426 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 427 | return false; |
| 428 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | } |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 430 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | return false; |
| 432 | } |
| 433 | |
| 434 | /** |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 435 | * Initiate disconnection from a profile |
| 436 | * |
| 437 | * <p> This API will return false in scenarios like the profile on the |
| 438 | * Bluetooth device is not in connected state etc. When this API returns, |
| 439 | * true, it is guaranteed that the connection state change |
| 440 | * intent will be broadcasted with the state. Users can get the |
| 441 | * disconnection state of the profile from this intent. |
| 442 | * |
| 443 | * <p> If the disconnection is initiated by a remote device, the state |
| 444 | * will transition from {@link #STATE_CONNECTED} to |
| 445 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 446 | * host (local) device the state will transition from |
| 447 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 448 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 449 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 450 | * two scenarios. |
| 451 | * |
| 452 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 453 | * permission. |
| 454 | * |
| 455 | * @param device Remote Bluetooth Device |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 456 | * @return false on immediate error, true otherwise |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 457 | * @hide |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | */ |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 459 | public boolean disconnect(BluetoothDevice device) { |
| 460 | if (DBG) log("disconnect(" + device + ")"); |
| 461 | if (mService != null && isEnabled() && |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 462 | isValidDevice(device)) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | try { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 464 | return mService.disconnect(device); |
| 465 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 466 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 467 | return false; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 468 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | } |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 470 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | return false; |
| 472 | } |
| 473 | |
| 474 | /** |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 475 | * {@inheritDoc} |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | */ |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 477 | public List<BluetoothDevice> getConnectedDevices() { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 478 | if (VDBG) log("getConnectedDevices()"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 479 | if (mService != null && isEnabled()) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | try { |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 481 | return mService.getConnectedDevices(); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 482 | } catch (RemoteException e) { |
| 483 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 484 | return new ArrayList<BluetoothDevice>(); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 485 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | } |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 487 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 488 | return new ArrayList<BluetoothDevice>(); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /** |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 492 | * {@inheritDoc} |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | */ |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 494 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 495 | if (VDBG) log("getDevicesMatchingStates()"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 496 | if (mService != null && isEnabled()) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | try { |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 498 | return mService.getDevicesMatchingConnectionStates(states); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 499 | } catch (RemoteException e) { |
| 500 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 501 | return new ArrayList<BluetoothDevice>(); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 502 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | } |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 504 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 505 | return new ArrayList<BluetoothDevice>(); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /** |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 509 | * {@inheritDoc} |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | */ |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 511 | public int getConnectionState(BluetoothDevice device) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 512 | if (VDBG) log("getConnectionState(" + device + ")"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 513 | if (mService != null && isEnabled() && |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 514 | isValidDevice(device)) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | try { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 516 | return mService.getConnectionState(device); |
| 517 | } catch (RemoteException e) { |
| 518 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 519 | return BluetoothProfile.STATE_DISCONNECTED; |
| 520 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | } |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 522 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 523 | return BluetoothProfile.STATE_DISCONNECTED; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /** |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 527 | * Set priority of the profile |
| 528 | * |
| 529 | * <p> The device should already be paired. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 530 | * Priority can be one of {@link #PRIORITY_ON} or |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 531 | * {@link #PRIORITY_OFF}, |
| 532 | * |
| 533 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 534 | * permission. |
| 535 | * |
| 536 | * @param device Paired bluetooth device |
| 537 | * @param priority |
| 538 | * @return true if priority is set, false on error |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 539 | * @hide |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | */ |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 541 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 542 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 543 | if (mService != null && isEnabled() && |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 544 | isValidDevice(device)) { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 545 | if (priority != BluetoothProfile.PRIORITY_OFF && |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 546 | priority != BluetoothProfile.PRIORITY_ON) { |
| 547 | return false; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 548 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | try { |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 550 | return mService.setPriority(device, priority); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 551 | } catch (RemoteException e) { |
| 552 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 553 | return false; |
| 554 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | } |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 556 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | return false; |
| 558 | } |
| 559 | |
| 560 | /** |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 561 | * Get the priority of the profile. |
| 562 | * |
| 563 | * <p> The priority can be any of: |
| 564 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 565 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 566 | * |
| 567 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 568 | * |
| 569 | * @param device Bluetooth device |
| 570 | * @return priority of the device |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 571 | * @hide |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | */ |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 573 | public int getPriority(BluetoothDevice device) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 574 | if (VDBG) log("getPriority(" + device + ")"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 575 | if (mService != null && isEnabled() && |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 576 | isValidDevice(device)) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | try { |
| Nick Pelly | 2d66488 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 578 | return mService.getPriority(device); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 579 | } catch (RemoteException e) { |
| 580 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 581 | return PRIORITY_OFF; |
| 582 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | } |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 584 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 585 | return PRIORITY_OFF; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Start Bluetooth voice recognition. This methods sends the voice |
| 590 | * recognition AT command to the headset and establishes the |
| 591 | * audio connection. |
| 592 | * |
| 593 | * <p> Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. |
| Jaikumar Ganesh | 8ea890d | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 594 | * If this function returns true, this intent will be broadcasted with |
| 595 | * {@link #EXTRA_STATE} set to {@link #STATE_AUDIO_CONNECTING}. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 596 | * |
| Jaikumar Ganesh | 8ea890d | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 597 | * <p> {@link #EXTRA_STATE} will transition from |
| 598 | * {@link #STATE_AUDIO_CONNECTING} to {@link #STATE_AUDIO_CONNECTED} when |
| 599 | * audio connection is established and to {@link #STATE_AUDIO_DISCONNECTED} |
| 600 | * in case of failure to establish the audio connection. |
| 601 | * |
| 602 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 603 | * |
| 604 | * @param device Bluetooth headset |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 605 | * @return false if there is no headset connected of if the connected headset doesn't support |
| 606 | * voice recognition or on error, true otherwise |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 607 | */ |
| 608 | public boolean startVoiceRecognition(BluetoothDevice device) { |
| 609 | if (DBG) log("startVoiceRecognition()"); |
| 610 | if (mService != null && isEnabled() && |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 611 | isValidDevice(device)) { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 612 | try { |
| 613 | return mService.startVoiceRecognition(device); |
| 614 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 615 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Stop Bluetooth Voice Recognition mode, and shut down the |
| 624 | * Bluetooth audio path. |
| 625 | * |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 626 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 627 | * |
| 628 | * @param device Bluetooth headset |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 629 | * @return false if there is no headset connected or on error, true otherwise |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 630 | */ |
| 631 | public boolean stopVoiceRecognition(BluetoothDevice device) { |
| 632 | if (DBG) log("stopVoiceRecognition()"); |
| 633 | if (mService != null && isEnabled() && |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 634 | isValidDevice(device)) { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 635 | try { |
| 636 | return mService.stopVoiceRecognition(device); |
| 637 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 638 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 642 | return false; |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Check if Bluetooth SCO audio is connected. |
| 647 | * |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 648 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 649 | * |
| 650 | * @param device Bluetooth headset |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 651 | * @return true if SCO is connected, false otherwise or on error |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 652 | */ |
| 653 | public boolean isAudioConnected(BluetoothDevice device) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 654 | if (VDBG) log("isAudioConnected()"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 655 | if (mService != null && isEnabled() && |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 656 | isValidDevice(device)) { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 657 | try { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 658 | return mService.isAudioConnected(device); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 659 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 660 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 664 | return false; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /** |
| Nick Pelly | 312cd3a | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 668 | * Get battery usage hint for Bluetooth Headset service. |
| 669 | * This is a monotonically increasing integer. Wraps to 0 at |
| 670 | * Integer.MAX_INT, and at boot. |
| 671 | * Current implementation returns the number of AT commands handled since |
| 672 | * boot. This is a good indicator for spammy headset/handsfree units that |
| 673 | * can keep the device awake by polling for cellular status updates. As a |
| 674 | * rule of thumb, each AT command prevents the CPU from sleeping for 500 ms |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 675 | * |
| 676 | * @param device the bluetooth headset. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 677 | * @return monotonically increasing battery usage hint, or a negative error code on error |
| Nick Pelly | 312cd3a | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 678 | * @hide |
| 679 | */ |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 680 | public int getBatteryUsageHint(BluetoothDevice device) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 681 | if (VDBG) log("getBatteryUsageHint()"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 682 | if (mService != null && isEnabled() && |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 683 | isValidDevice(device)) { |
| Nick Pelly | 312cd3a | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 684 | try { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 685 | return mService.getBatteryUsageHint(device); |
| 686 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 687 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 688 | } |
| Nick Pelly | 312cd3a | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 689 | } |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 690 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| Nick Pelly | 312cd3a | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 691 | return -1; |
| 692 | } |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 693 | |
| Eric Laurent | 2e66fa2 | 2010-03-17 14:59:27 -0700 | [diff] [blame] | 694 | /** |
| 695 | * Indicates if current platform supports voice dialing over bluetooth SCO. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 696 | * |
| Eric Laurent | 2e66fa2 | 2010-03-17 14:59:27 -0700 | [diff] [blame] | 697 | * @return true if voice dialing over bluetooth is supported, false otherwise. |
| 698 | * @hide |
| 699 | */ |
| 700 | public static boolean isBluetoothVoiceDialingEnabled(Context context) { |
| 701 | return context.getResources().getBoolean( |
| 702 | com.android.internal.R.bool.config_bluetooth_sco_off_call); |
| 703 | } |
| 704 | |
| Jaikumar Ganesh | b84bbd9 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 705 | /** |
| Jaikumar Ganesh | b84bbd9 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 706 | * Accept the incoming connection. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 707 | * Note: This is an internal function and shouldn't be exposed |
| 708 | * |
| Jaikumar Ganesh | b84bbd9 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 709 | * @hide |
| 710 | */ |
| 711 | public boolean acceptIncomingConnect(BluetoothDevice device) { |
| 712 | if (DBG) log("acceptIncomingConnect"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 713 | if (mService != null && isEnabled()) { |
| Jaikumar Ganesh | b84bbd9 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 714 | try { |
| 715 | return mService.acceptIncomingConnect(device); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 716 | } catch (RemoteException e) { |
| 717 | Log.e(TAG, e.toString()); |
| 718 | } |
| Jaikumar Ganesh | b84bbd9 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 719 | } else { |
| 720 | Log.w(TAG, "Proxy not attached to service"); |
| 721 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 722 | } |
| 723 | return false; |
| 724 | } |
| 725 | |
| 726 | /** |
| Matthew Xie | 75cf0b9 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 727 | * Reject the incoming connection. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 728 | * |
| Matthew Xie | 75cf0b9 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 729 | * @hide |
| 730 | */ |
| 731 | public boolean rejectIncomingConnect(BluetoothDevice device) { |
| 732 | if (DBG) log("rejectIncomingConnect"); |
| 733 | if (mService != null) { |
| 734 | try { |
| 735 | return mService.rejectIncomingConnect(device); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 736 | } catch (RemoteException e) { |
| 737 | Log.e(TAG, e.toString()); |
| 738 | } |
| Matthew Xie | 75cf0b9 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 739 | } else { |
| 740 | Log.w(TAG, "Proxy not attached to service"); |
| 741 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 742 | } |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | /** |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 747 | * Get the current audio state of the Headset. |
| 748 | * Note: This is an internal function and shouldn't be exposed |
| 749 | * |
| 750 | * @hide |
| 751 | */ |
| 752 | public int getAudioState(BluetoothDevice device) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 753 | if (VDBG) log("getAudioState"); |
| Jaikumar Ganesh | 2fbe8f4 | 2011-04-06 11:09:30 -0700 | [diff] [blame] | 754 | if (mService != null && !isDisabled()) { |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 755 | try { |
| 756 | return mService.getAudioState(device); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 757 | } catch (RemoteException e) { |
| 758 | Log.e(TAG, e.toString()); |
| 759 | } |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 760 | } else { |
| 761 | Log.w(TAG, "Proxy not attached to service"); |
| 762 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 763 | } |
| 764 | return BluetoothHeadset.STATE_AUDIO_DISCONNECTED; |
| 765 | } |
| 766 | |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 767 | /** |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 768 | * Sets whether audio routing is allowed. When set to {@code false}, the AG will not route any |
| 769 | * audio to the HF unless explicitly told to. |
| 770 | * This method should be used in cases where the SCO channel is shared between multiple profiles |
| 771 | * and must be delegated by a source knowledgeable |
| 772 | * Note: This is an internal function and shouldn't be exposed |
| 773 | * |
| 774 | * @param allowed {@code true} if the profile can reroute audio, {@code false} otherwise. |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 775 | * @hide |
| 776 | */ |
| 777 | public void setAudioRouteAllowed(boolean allowed) { |
| 778 | if (VDBG) log("setAudioRouteAllowed"); |
| 779 | if (mService != null && isEnabled()) { |
| 780 | try { |
| 781 | mService.setAudioRouteAllowed(allowed); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 782 | } catch (RemoteException e) { |
| 783 | Log.e(TAG, e.toString()); |
| 784 | } |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 785 | } else { |
| 786 | Log.w(TAG, "Proxy not attached to service"); |
| 787 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Returns whether audio routing is allowed. see {@link #setAudioRouteAllowed(boolean)}. |
| 793 | * Note: This is an internal function and shouldn't be exposed |
| 794 | * |
| 795 | * @hide |
| 796 | */ |
| 797 | public boolean getAudioRouteAllowed() { |
| 798 | if (VDBG) log("getAudioRouteAllowed"); |
| 799 | if (mService != null && isEnabled()) { |
| 800 | try { |
| 801 | return mService.getAudioRouteAllowed(); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 802 | } catch (RemoteException e) { |
| 803 | Log.e(TAG, e.toString()); |
| 804 | } |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 805 | } else { |
| 806 | Log.w(TAG, "Proxy not attached to service"); |
| 807 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 808 | } |
| 809 | return false; |
| 810 | } |
| 811 | |
| 812 | /** |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 813 | * Force SCO audio to be opened regardless any other restrictions |
| 814 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 815 | * @param forced Whether or not SCO audio connection should be forced: True to force SCO audio |
| 816 | * False to use SCO audio in normal manner |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 817 | * @hide |
| 818 | */ |
| 819 | public void setForceScoAudio(boolean forced) { |
| 820 | if (VDBG) log("setForceScoAudio " + String.valueOf(forced)); |
| 821 | if (mService != null && isEnabled()) { |
| 822 | try { |
| 823 | mService.setForceScoAudio(forced); |
| 824 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 825 | Log.e(TAG, e.toString()); |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 826 | } |
| 827 | } else { |
| 828 | Log.w(TAG, "Proxy not attached to service"); |
| 829 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | /** |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 834 | * Check if Bluetooth SCO audio is connected. |
| 835 | * |
| 836 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 837 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 838 | * @return true if SCO is connected, false otherwise or on error |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 839 | * @hide |
| 840 | */ |
| 841 | public boolean isAudioOn() { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 842 | if (VDBG) log("isAudioOn()"); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 843 | if (mService != null && isEnabled()) { |
| 844 | try { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 845 | return mService.isAudioOn(); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 846 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 847 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 851 | return false; |
| 852 | |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Initiates a connection of headset audio. |
| 857 | * It setup SCO channel with remote connected headset device. |
| 858 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 859 | * @return true if successful false if there was some error such as there is no connected |
| 860 | * headset |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 861 | * @hide |
| 862 | */ |
| 863 | public boolean connectAudio() { |
| 864 | if (mService != null && isEnabled()) { |
| 865 | try { |
| 866 | return mService.connectAudio(); |
| 867 | } catch (RemoteException e) { |
| 868 | Log.e(TAG, e.toString()); |
| 869 | } |
| 870 | } else { |
| 871 | Log.w(TAG, "Proxy not attached to service"); |
| 872 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 873 | } |
| 874 | return false; |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * Initiates a disconnection of headset audio. |
| 879 | * It tears down the SCO channel from remote headset device. |
| 880 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 881 | * @return true if successful false if there was some error such as there is no connected SCO |
| 882 | * channel |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 883 | * @hide |
| 884 | */ |
| 885 | public boolean disconnectAudio() { |
| 886 | if (mService != null && isEnabled()) { |
| 887 | try { |
| 888 | return mService.disconnectAudio(); |
| 889 | } catch (RemoteException e) { |
| 890 | Log.e(TAG, e.toString()); |
| 891 | } |
| 892 | } else { |
| 893 | Log.w(TAG, "Proxy not attached to service"); |
| 894 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 895 | } |
| 896 | return false; |
| 897 | } |
| 898 | |
| 899 | /** |
| Jaikumar Ganesh | eea6d26 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 900 | * Initiates a SCO channel connection with the headset (if connected). |
| 901 | * Also initiates a virtual voice call for Handsfree devices as many devices |
| 902 | * do not accept SCO audio without a call. |
| 903 | * This API allows the handsfree device to be used for routing non-cellular |
| 904 | * call audio. |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 905 | * |
| 906 | * @param device Remote Bluetooth Device |
| 907 | * @return true if successful, false if there was some error. |
| 908 | * @hide |
| 909 | */ |
| Jaikumar Ganesh | eea6d26 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 910 | public boolean startScoUsingVirtualVoiceCall(BluetoothDevice device) { |
| 911 | if (DBG) log("startScoUsingVirtualVoiceCall()"); |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 912 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 913 | try { |
| Jaikumar Ganesh | eea6d26 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 914 | return mService.startScoUsingVirtualVoiceCall(device); |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 915 | } catch (RemoteException e) { |
| 916 | Log.e(TAG, e.toString()); |
| 917 | } |
| 918 | } else { |
| 919 | Log.w(TAG, "Proxy not attached to service"); |
| 920 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 921 | } |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | /** |
| Jaikumar Ganesh | eea6d26 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 926 | * Terminates an ongoing SCO connection and the associated virtual |
| 927 | * call. |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 928 | * |
| 929 | * @param device Remote Bluetooth Device |
| 930 | * @return true if successful, false if there was some error. |
| 931 | * @hide |
| 932 | */ |
| Jaikumar Ganesh | eea6d26 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 933 | public boolean stopScoUsingVirtualVoiceCall(BluetoothDevice device) { |
| 934 | if (DBG) log("stopScoUsingVirtualVoiceCall()"); |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 935 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 936 | try { |
| Jaikumar Ganesh | eea6d26 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 937 | return mService.stopScoUsingVirtualVoiceCall(device); |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 938 | } catch (RemoteException e) { |
| 939 | Log.e(TAG, e.toString()); |
| 940 | } |
| 941 | } else { |
| 942 | Log.w(TAG, "Proxy not attached to service"); |
| 943 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 944 | } |
| 945 | return false; |
| 946 | } |
| 947 | |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 948 | /** |
| 949 | * Notify Headset of phone state change. |
| 950 | * This is a backdoor for phone app to call BluetoothHeadset since |
| 951 | * there is currently not a good way to get precise call state change outside |
| 952 | * of phone app. |
| 953 | * |
| 954 | * @hide |
| 955 | */ |
| 956 | public void phoneStateChanged(int numActive, int numHeld, int callState, String number, |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 957 | int type) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 958 | if (mService != null && isEnabled()) { |
| 959 | try { |
| 960 | mService.phoneStateChanged(numActive, numHeld, callState, number, type); |
| 961 | } catch (RemoteException e) { |
| 962 | Log.e(TAG, e.toString()); |
| 963 | } |
| 964 | } else { |
| 965 | Log.w(TAG, "Proxy not attached to service"); |
| 966 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | /** |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 971 | * Send Headset of CLCC response |
| 972 | * |
| 973 | * @hide |
| 974 | */ |
| 975 | public void clccResponse(int index, int direction, int status, int mode, boolean mpty, |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 976 | String number, int type) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 977 | if (mService != null && isEnabled()) { |
| 978 | try { |
| 979 | mService.clccResponse(index, direction, status, mode, mpty, number, type); |
| 980 | } catch (RemoteException e) { |
| 981 | Log.e(TAG, e.toString()); |
| 982 | } |
| 983 | } else { |
| 984 | Log.w(TAG, "Proxy not attached to service"); |
| 985 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 986 | } |
| 987 | } |
| 988 | |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 989 | /** |
| 990 | * Sends a vendor-specific unsolicited result code to the headset. |
| 991 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 992 | * <p>The actual string to be sent is <code>command + ": " + arg</code>. For example, if {@code |
| 993 | * command} is {@link #VENDOR_RESULT_CODE_COMMAND_ANDROID} and {@code arg} is {@code "0"}, the |
| 994 | * string <code>"+ANDROID: 0"</code> will be sent. |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 995 | * |
| Ying Wang | 29d9389 | 2013-08-26 17:48:22 -0700 | [diff] [blame] | 996 | * <p>Currently only {@link #VENDOR_RESULT_CODE_COMMAND_ANDROID} is allowed as {@code command}. |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 997 | * |
| 998 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 999 | * |
| 1000 | * @param device Bluetooth headset. |
| 1001 | * @param command A vendor-specific command. |
| 1002 | * @param arg The argument that will be attached to the command. |
| 1003 | * @return {@code false} if there is no headset connected, or if the command is not an allowed |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1004 | * vendor-specific unsolicited result code, or on error. {@code true} otherwise. |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1005 | * @throws IllegalArgumentException if {@code command} is {@code null}. |
| 1006 | */ |
| 1007 | public boolean sendVendorSpecificResultCode(BluetoothDevice device, String command, |
| 1008 | String arg) { |
| 1009 | if (DBG) { |
| 1010 | log("sendVendorSpecificResultCode()"); |
| 1011 | } |
| 1012 | if (command == null) { |
| 1013 | throw new IllegalArgumentException("command is null"); |
| 1014 | } |
| 1015 | if (mService != null && isEnabled() && |
| 1016 | isValidDevice(device)) { |
| 1017 | try { |
| 1018 | return mService.sendVendorSpecificResultCode(device, command, arg); |
| 1019 | } catch (RemoteException e) { |
| 1020 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 1021 | } |
| 1022 | } |
| 1023 | if (mService == null) { |
| 1024 | Log.w(TAG, "Proxy not attached to service"); |
| 1025 | } |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| Mudumba Ananth | 80bf628 | 2014-04-27 13:11:00 -0700 | [diff] [blame] | 1029 | /** |
| 1030 | * enable WBS codec setting. |
| 1031 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1032 | * @return true if successful false if there was some error such as there is no connected |
| 1033 | * headset |
| Mudumba Ananth | 80bf628 | 2014-04-27 13:11:00 -0700 | [diff] [blame] | 1034 | * @hide |
| 1035 | */ |
| 1036 | public boolean enableWBS() { |
| 1037 | if (mService != null && isEnabled()) { |
| 1038 | try { |
| 1039 | return mService.enableWBS(); |
| 1040 | } catch (RemoteException e) { |
| 1041 | Log.e(TAG, e.toString()); |
| 1042 | } |
| 1043 | } else { |
| 1044 | Log.w(TAG, "Proxy not attached to service"); |
| 1045 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1046 | } |
| 1047 | return false; |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * disable WBS codec settting. It set NBS codec. |
| 1052 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1053 | * @return true if successful false if there was some error such as there is no connected |
| 1054 | * headset |
| Mudumba Ananth | 80bf628 | 2014-04-27 13:11:00 -0700 | [diff] [blame] | 1055 | * @hide |
| 1056 | */ |
| 1057 | public boolean disableWBS() { |
| 1058 | if (mService != null && isEnabled()) { |
| 1059 | try { |
| 1060 | return mService.disableWBS(); |
| 1061 | } catch (RemoteException e) { |
| 1062 | Log.e(TAG, e.toString()); |
| 1063 | } |
| 1064 | } else { |
| 1065 | Log.w(TAG, "Proxy not attached to service"); |
| 1066 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1067 | } |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 1071 | /** |
| Jack He | d8d204d | 2016-11-17 16:19:43 -0800 | [diff] [blame] | 1072 | * check if in-band ringing is supported for this platform. |
| 1073 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1074 | * @return true if in-band ringing is supported false if in-band ringing is not supported |
| Jack He | d8d204d | 2016-11-17 16:19:43 -0800 | [diff] [blame] | 1075 | * @hide |
| 1076 | */ |
| 1077 | public static boolean isInbandRingingSupported(Context context) { |
| 1078 | return context.getResources().getBoolean( |
| 1079 | com.android.internal.R.bool.config_bluetooth_hfp_inband_ringing_support); |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 1083 | * Send Headset the BIND response from AG to report change in the status of the |
| 1084 | * HF indicators to the headset |
| 1085 | * |
| 1086 | * @param ind_id Assigned Number of the indicator (defined by SIG) |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1087 | * @param ind_status possible values- false-Indicator is disabled, no value changes shall be |
| 1088 | * sent for this indicator true-Indicator is enabled, value changes may be sent for this |
| 1089 | * indicator |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 1090 | * @hide |
| 1091 | */ |
| 1092 | public void bindResponse(int ind_id, boolean ind_status) { |
| 1093 | if (mService != null && isEnabled()) { |
| 1094 | try { |
| 1095 | mService.bindResponse(ind_id, ind_status); |
| 1096 | } catch (RemoteException e) { |
| 1097 | Log.e(TAG, e.toString()); |
| 1098 | } |
| 1099 | } else { |
| 1100 | Log.w(TAG, "Proxy not attached to service"); |
| 1101 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1105 | private final IBluetoothProfileServiceConnection mConnection |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1106 | = new IBluetoothProfileServiceConnection.Stub() { |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1107 | @Override |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 1109 | if (DBG) Log.d(TAG, "Proxy object connected"); |
| Jeff Sharkey | 73458a8 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 1110 | mService = IBluetoothHeadset.Stub.asInterface(Binder.allowBlocking(service)); |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1111 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1112 | MESSAGE_HEADSET_SERVICE_CONNECTED)); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | } |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1114 | |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1115 | @Override |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | public void onServiceDisconnected(ComponentName className) { |
| 1117 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 1118 | mService = null; |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1119 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1120 | MESSAGE_HEADSET_SERVICE_DISCONNECTED)); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | } |
| 1122 | }; |
| The Android Open Source Project | 0047a0f | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 1123 | |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1124 | private boolean isEnabled() { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1125 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 1126 | return false; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
| Jaikumar Ganesh | 2fbe8f4 | 2011-04-06 11:09:30 -0700 | [diff] [blame] | 1129 | private boolean isDisabled() { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1130 | if (mAdapter.getState() == BluetoothAdapter.STATE_OFF) return true; |
| 1131 | return false; |
| Jaikumar Ganesh | 2fbe8f4 | 2011-04-06 11:09:30 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1134 | private boolean isValidDevice(BluetoothDevice device) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1135 | if (device == null) return false; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1136 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 1137 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 1138 | return false; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
| The Android Open Source Project | 0047a0f | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 1141 | private static void log(String msg) { |
| 1142 | Log.d(TAG, msg); |
| 1143 | } |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1144 | |
| 1145 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { |
| 1146 | @Override |
| 1147 | public void handleMessage(Message msg) { |
| 1148 | switch (msg.what) { |
| 1149 | case MESSAGE_HEADSET_SERVICE_CONNECTED: { |
| 1150 | if (mServiceListener != null) { |
| 1151 | mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, |
| 1152 | BluetoothHeadset.this); |
| 1153 | } |
| 1154 | break; |
| 1155 | } |
| 1156 | case MESSAGE_HEADSET_SERVICE_DISCONNECTED: { |
| 1157 | if (mServiceListener != null) { |
| 1158 | mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET); |
| 1159 | } |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1160 | break; |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | }; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | } |