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