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