The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.os.IBinder; |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 26 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | * Public API for controlling the Bluetooth Headset Service. This includes both |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 34 | * Bluetooth Headset and Handsfree (v1.5) profiles. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | * |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 36 | * <p>BluetoothHeadset is a proxy object for controlling the Bluetooth Headset |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | * Service via IPC. |
| 38 | * |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 39 | * <p> Use {@link BluetoothAdapter#getProfileProxy} to get |
| 40 | * the BluetoothHeadset proxy object. Use |
| 41 | * {@link BluetoothAdapter#closeProfileProxy} to close the service connection. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | * |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 43 | * <p> Android only supports one connected Bluetooth Headset at a time. |
| 44 | * Each method is protected with its appropriate permission. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 46 | public final class BluetoothHeadset implements BluetoothProfile { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | private static final String TAG = "BluetoothHeadset"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 48 | private static final boolean DBG = true; |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 49 | private static final boolean VDBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 51 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 52 | * Intent used to broadcast the change in connection state of the Headset |
| 53 | * profile. |
| 54 | * |
| 55 | * <p>This intent will have 3 extras: |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 56 | * <ul> |
| 57 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 58 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li> |
| 59 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
| 60 | * </ul> |
Jaikumar Ganesh | 0706fed | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 61 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 62 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 63 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 64 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 65 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 66 | * receive. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 67 | */ |
| 68 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 69 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| 70 | "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED"; |
| 71 | |
| 72 | /** |
| 73 | * Intent used to broadcast the change in the Audio Connection state of the |
| 74 | * A2DP profile. |
| 75 | * |
| 76 | * <p>This intent will have 3 extras: |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 77 | * <ul> |
| 78 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 79 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li> |
| 80 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
| 81 | * </ul> |
Jaikumar Ganesh | 0706fed | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 82 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 83 | * {@link #STATE_AUDIO_CONNECTED}, {@link #STATE_AUDIO_DISCONNECTED}, |
| 84 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 85 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission |
| 86 | * to receive. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 87 | */ |
| 88 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 89 | public static final String ACTION_AUDIO_STATE_CHANGED = |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 90 | "android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED"; |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 91 | |
Jaikumar Ganesh | c24dbdb | 2010-04-02 14:44:43 -0700 | [diff] [blame] | 92 | |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 93 | /** |
Jaikumar Ganesh | e775b3d | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 94 | * Intent used to broadcast that the headset has posted a |
| 95 | * vendor-specific event. |
| 96 | * |
| 97 | * <p>This intent will have 4 extras and 1 category. |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 98 | * <ul> |
| 99 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote Bluetooth Device |
| 100 | * </li> |
| 101 | * <li> {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD} - The vendor |
| 102 | * specific command </li> |
| 103 | * <li> {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} - The AT |
| 104 | * command type which can be one of {@link #AT_CMD_TYPE_READ}, |
| 105 | * {@link #AT_CMD_TYPE_TEST}, or {@link #AT_CMD_TYPE_SET}, |
Jaikumar Ganesh | 0706fed | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 106 | * {@link #AT_CMD_TYPE_BASIC},{@link #AT_CMD_TYPE_ACTION}. </li> |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 107 | * <li> {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS} - Command |
| 108 | * arguments. </li> |
| 109 | * </ul> |
Jaikumar Ganesh | e775b3d | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 110 | * |
Jaikumar Ganesh | 0706fed | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 111 | *<p> The category is the Company ID of the vendor defining the |
Jaikumar Ganesh | e775b3d | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 112 | * vendor-specific command. {@link BluetoothAssignedNumbers} |
Jaikumar Ganesh | e775b3d | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 113 | * |
| 114 | * For example, for Plantronics specific events |
| 115 | * Category will be {@link #VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY}.55 |
| 116 | * |
| 117 | * <p> For example, an AT+XEVENT=foo,3 will get translated into |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 118 | * <ul> |
| 119 | * <li> EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD = +XEVENT </li> |
| 120 | * <li> EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE = AT_CMD_TYPE_SET </li> |
Jaikumar Ganesh | 0706fed | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 121 | * <li> EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS = foo, 3 </li> |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 122 | * </ul> |
| 123 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission |
| 124 | * to receive. |
Herb Jellinek | a473394 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 125 | */ |
| 126 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 127 | public static final String ACTION_VENDOR_SPECIFIC_HEADSET_EVENT = |
| 128 | "android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT"; |
| 129 | |
| 130 | /** |
| 131 | * A String extra field in {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} |
| 132 | * intents that contains the name of the vendor-specific command. |
| 133 | */ |
| 134 | public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD = |
| 135 | "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD"; |
| 136 | |
| 137 | /** |
| 138 | * An int extra field in {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} |
Jaikumar Ganesh | e775b3d | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 139 | * intents that contains the AT command type of the vendor-specific command. |
Herb Jellinek | a473394 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 140 | */ |
Jaikumar Ganesh | e775b3d | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 141 | public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE = |
| 142 | "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE"; |
| 143 | |
| 144 | /** |
| 145 | * AT command type READ used with |
| 146 | * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} |
| 147 | * For example, AT+VGM?. There are no arguments for this command type. |
| 148 | */ |
| 149 | public static final int AT_CMD_TYPE_READ = 0; |
| 150 | |
| 151 | /** |
| 152 | * AT command type TEST used with |
| 153 | * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} |
| 154 | * For example, AT+VGM=?. There are no arguments for this command type. |
| 155 | */ |
| 156 | public static final int AT_CMD_TYPE_TEST = 1; |
| 157 | |
| 158 | /** |
| 159 | * AT command type SET used with |
| 160 | * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} |
| 161 | * For example, AT+VGM=<args>. |
| 162 | */ |
| 163 | public static final int AT_CMD_TYPE_SET = 2; |
| 164 | |
| 165 | /** |
| 166 | * AT command type BASIC used with |
| 167 | * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} |
| 168 | * For example, ATD. Single character commands and everything following the |
| 169 | * character are arguments. |
| 170 | */ |
| 171 | public static final int AT_CMD_TYPE_BASIC = 3; |
| 172 | |
| 173 | /** |
| 174 | * AT command type ACTION used with |
| 175 | * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} |
| 176 | * For example, AT+CHUP. There are no arguments for action commands. |
| 177 | */ |
| 178 | public static final int AT_CMD_TYPE_ACTION = 4; |
Herb Jellinek | a473394 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * A Parcelable String array extra field in |
| 182 | * {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} intents that contains |
| 183 | * the arguments to the vendor-specific command. |
| 184 | */ |
| 185 | public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS = |
| 186 | "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_ARGS"; |
| 187 | |
Jaikumar Ganesh | e775b3d | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 188 | /** |
| 189 | * The intent category to be used with {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} |
| 190 | * for the companyId |
| 191 | */ |
| 192 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY = |
| 193 | "android.bluetooth.headset.intent.category.companyid"; |
| 194 | |
Jaikumar Ganesh | 30d1816 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 195 | /** |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 196 | * Headset state when SCO audio is not connected. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 197 | * This state can be one of |
| 198 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 199 | * {@link #ACTION_AUDIO_STATE_CHANGED} intent. |
| 200 | */ |
Jaikumar Ganesh | b0a1d01 | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 201 | public static final int STATE_AUDIO_DISCONNECTED = 10; |
Herb Jellinek | a473394 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 202 | |
| 203 | /** |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 204 | * Headset state when SCO audio is connecting. |
Jaikumar Ganesh | 30d1816 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 205 | * This state can be one of |
| 206 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 207 | * {@link #ACTION_AUDIO_STATE_CHANGED} intent. |
Jaikumar Ganesh | 30d1816 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 208 | */ |
Jaikumar Ganesh | b0a1d01 | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 209 | public static final int STATE_AUDIO_CONNECTING = 11; |
Jaikumar Ganesh | 30d1816 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 210 | |
| 211 | /** |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 212 | * Headset state when SCO audio is connected. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 213 | * This state can be one of |
| 214 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 215 | * {@link #ACTION_AUDIO_STATE_CHANGED} intent. |
Nick Pelly | 005b228 | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 216 | */ |
Jaikumar Ganesh | b0a1d01 | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 217 | public static final int STATE_AUDIO_CONNECTED = 12; |
| 218 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 219 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 220 | private Context mContext; |
| 221 | private ServiceListener mServiceListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | private IBluetoothHeadset mService; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 223 | private BluetoothAdapter mAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 225 | final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
| 226 | new IBluetoothStateChangeCallback.Stub() { |
| 227 | public void onBluetoothStateChange(boolean up) { |
| 228 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 229 | if (!up) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 230 | if (VDBG) Log.d(TAG,"Unbinding service..."); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 231 | synchronized (mConnection) { |
| 232 | try { |
| 233 | mService = null; |
| 234 | mContext.unbindService(mConnection); |
| 235 | } catch (Exception re) { |
| 236 | Log.e(TAG,"",re); |
| 237 | } |
| 238 | } |
| 239 | } else { |
| 240 | synchronized (mConnection) { |
| 241 | try { |
| 242 | if (mService == null) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 243 | if (VDBG) Log.d(TAG,"Binding service..."); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 244 | if (!mContext.bindService(new Intent(IBluetoothHeadset.class.getName()), mConnection, 0)) { |
| 245 | Log.e(TAG, "Could not bind to Bluetooth Headset Service"); |
| 246 | } |
| 247 | } |
| 248 | } catch (Exception re) { |
| 249 | Log.e(TAG,"",re); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | }; |
| 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Create a BluetoothHeadset proxy object. |
| 258 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 259 | /*package*/ BluetoothHeadset(Context context, ServiceListener l) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | mContext = context; |
| 261 | mServiceListener = l; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 262 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 263 | |
| 264 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 265 | if (mgr != null) { |
| 266 | try { |
| 267 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 268 | } catch (RemoteException e) { |
| 269 | Log.e(TAG,"",e); |
| 270 | } |
| 271 | } |
| 272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | if (!context.bindService(new Intent(IBluetoothHeadset.class.getName()), mConnection, 0)) { |
| 274 | Log.e(TAG, "Could not bind to Bluetooth Headset Service"); |
| 275 | } |
| 276 | } |
| 277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | /** |
| 279 | * Close the connection to the backing service. |
| 280 | * Other public functions of BluetoothHeadset will return default error |
| 281 | * results once close() has been called. Multiple invocations of close() |
| 282 | * are ok. |
| 283 | */ |
Matthew Xie | 13450df | 2012-03-22 17:18:37 -0700 | [diff] [blame] | 284 | /*package*/ void close() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 285 | if (VDBG) log("close()"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 286 | |
| 287 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 288 | if (mgr != null) { |
| 289 | try { |
| 290 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 291 | } catch (Exception e) { |
| 292 | Log.e(TAG,"",e); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | synchronized (mConnection) { |
| 297 | if (mService != null) { |
| 298 | try { |
| 299 | mService = null; |
| 300 | mContext.unbindService(mConnection); |
| 301 | } catch (Exception re) { |
| 302 | Log.e(TAG,"",re); |
| 303 | } |
| 304 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 306 | mServiceListener = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 310 | * Initiate connection to a profile of the remote bluetooth device. |
| 311 | * |
| 312 | * <p> Currently, the system supports only 1 connection to the |
| 313 | * headset/handsfree profile. The API will automatically disconnect connected |
| 314 | * devices before connecting. |
| 315 | * |
| 316 | * <p> This API returns false in scenarios like the profile on the |
| 317 | * device is already connected or Bluetooth is not turned on. |
| 318 | * When this API returns true, it is guaranteed that |
| 319 | * connection state intent for the profile will be broadcasted with |
| 320 | * the state. Users can get the connection state of the profile |
| 321 | * from this intent. |
| 322 | * |
| 323 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 324 | * permission. |
| 325 | * |
| 326 | * @param device Remote Bluetooth Device |
| 327 | * @return false on immediate error, |
| 328 | * true otherwise |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 329 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 331 | public boolean connect(BluetoothDevice device) { |
| 332 | if (DBG) log("connect(" + device + ")"); |
| 333 | if (mService != null && isEnabled() && |
| 334 | isValidDevice(device)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | try { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 336 | return mService.connect(device); |
| 337 | } catch (RemoteException e) { |
| 338 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 339 | return false; |
| 340 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 342 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | return false; |
| 344 | } |
| 345 | |
| 346 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 347 | * Initiate disconnection from a profile |
| 348 | * |
| 349 | * <p> This API will return false in scenarios like the profile on the |
| 350 | * Bluetooth device is not in connected state etc. When this API returns, |
| 351 | * true, it is guaranteed that the connection state change |
| 352 | * intent will be broadcasted with the state. Users can get the |
| 353 | * disconnection state of the profile from this intent. |
| 354 | * |
| 355 | * <p> If the disconnection is initiated by a remote device, the state |
| 356 | * will transition from {@link #STATE_CONNECTED} to |
| 357 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 358 | * host (local) device the state will transition from |
| 359 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 360 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 361 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 362 | * two scenarios. |
| 363 | * |
| 364 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 365 | * permission. |
| 366 | * |
| 367 | * @param device Remote Bluetooth Device |
| 368 | * @return false on immediate error, |
| 369 | * true otherwise |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 370 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 372 | public boolean disconnect(BluetoothDevice device) { |
| 373 | if (DBG) log("disconnect(" + device + ")"); |
| 374 | if (mService != null && isEnabled() && |
| 375 | isValidDevice(device)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | try { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 377 | return mService.disconnect(device); |
| 378 | } catch (RemoteException e) { |
| 379 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 380 | return false; |
| 381 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 383 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | |
| 387 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 388 | * {@inheritDoc} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | */ |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 390 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 391 | if (VDBG) log("getConnectedDevices()"); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 392 | if (mService != null && isEnabled()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | try { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 394 | return mService.getConnectedDevices(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 395 | } catch (RemoteException e) { |
| 396 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 397 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 398 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 400 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 401 | return new ArrayList<BluetoothDevice>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 405 | * {@inheritDoc} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | */ |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 407 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 408 | if (VDBG) log("getDevicesMatchingStates()"); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 409 | if (mService != null && isEnabled()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | try { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 411 | return mService.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 412 | } catch (RemoteException e) { |
| 413 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 414 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 415 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 417 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 418 | return new ArrayList<BluetoothDevice>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 422 | * {@inheritDoc} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 424 | public int getConnectionState(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 425 | if (VDBG) log("getConnectionState(" + device + ")"); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 426 | if (mService != null && isEnabled() && |
| 427 | isValidDevice(device)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | try { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 429 | return mService.getConnectionState(device); |
| 430 | } catch (RemoteException e) { |
| 431 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 432 | return BluetoothProfile.STATE_DISCONNECTED; |
| 433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 435 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 436 | return BluetoothProfile.STATE_DISCONNECTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 440 | * Set priority of the profile |
| 441 | * |
| 442 | * <p> The device should already be paired. |
| 443 | * Priority can be one of {@link #PRIORITY_ON} or |
| 444 | * {@link #PRIORITY_OFF}, |
| 445 | * |
| 446 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 447 | * permission. |
| 448 | * |
| 449 | * @param device Paired bluetooth device |
| 450 | * @param priority |
| 451 | * @return true if priority is set, false on error |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 452 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 454 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 455 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 456 | if (mService != null && isEnabled() && |
| 457 | isValidDevice(device)) { |
| 458 | if (priority != BluetoothProfile.PRIORITY_OFF && |
Ganesh Ganapathi Batta | 6f6c5451 | 2012-07-31 16:08:17 -0700 | [diff] [blame] | 459 | priority != BluetoothProfile.PRIORITY_ON) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 460 | return false; |
| 461 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 463 | return mService.setPriority(device, priority); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 464 | } catch (RemoteException e) { |
| 465 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 466 | return false; |
| 467 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 469 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | |
| 473 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 474 | * Get the priority of the profile. |
| 475 | * |
| 476 | * <p> The priority can be any of: |
| 477 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 478 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 479 | * |
| 480 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 481 | * |
| 482 | * @param device Bluetooth device |
| 483 | * @return priority of the device |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 484 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | */ |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 486 | public int getPriority(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 487 | if (VDBG) log("getPriority(" + device + ")"); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 488 | if (mService != null && isEnabled() && |
| 489 | isValidDevice(device)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | try { |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 491 | return mService.getPriority(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 492 | } catch (RemoteException e) { |
| 493 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 494 | return PRIORITY_OFF; |
| 495 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 497 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 498 | return PRIORITY_OFF; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Start Bluetooth voice recognition. This methods sends the voice |
| 503 | * recognition AT command to the headset and establishes the |
| 504 | * audio connection. |
| 505 | * |
| 506 | * <p> Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. |
Jaikumar Ganesh | b0a1d01 | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 507 | * If this function returns true, this intent will be broadcasted with |
| 508 | * {@link #EXTRA_STATE} set to {@link #STATE_AUDIO_CONNECTING}. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 509 | * |
Jaikumar Ganesh | b0a1d01 | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 510 | * <p> {@link #EXTRA_STATE} will transition from |
| 511 | * {@link #STATE_AUDIO_CONNECTING} to {@link #STATE_AUDIO_CONNECTED} when |
| 512 | * audio connection is established and to {@link #STATE_AUDIO_DISCONNECTED} |
| 513 | * in case of failure to establish the audio connection. |
| 514 | * |
| 515 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 516 | * |
| 517 | * @param device Bluetooth headset |
| 518 | * @return false if there is no headset connected of if the |
| 519 | * connected headset doesn't support voice recognition |
| 520 | * or on error, true otherwise |
| 521 | */ |
| 522 | public boolean startVoiceRecognition(BluetoothDevice device) { |
| 523 | if (DBG) log("startVoiceRecognition()"); |
| 524 | if (mService != null && isEnabled() && |
| 525 | isValidDevice(device)) { |
| 526 | try { |
| 527 | return mService.startVoiceRecognition(device); |
| 528 | } catch (RemoteException e) { |
| 529 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 530 | } |
| 531 | } |
| 532 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Stop Bluetooth Voice Recognition mode, and shut down the |
| 538 | * Bluetooth audio path. |
| 539 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 540 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 541 | * |
| 542 | * @param device Bluetooth headset |
| 543 | * @return false if there is no headset connected |
| 544 | * or on error, true otherwise |
| 545 | */ |
| 546 | public boolean stopVoiceRecognition(BluetoothDevice device) { |
| 547 | if (DBG) log("stopVoiceRecognition()"); |
| 548 | if (mService != null && isEnabled() && |
| 549 | isValidDevice(device)) { |
| 550 | try { |
| 551 | return mService.stopVoiceRecognition(device); |
| 552 | } catch (RemoteException e) { |
| 553 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 554 | } |
| 555 | } |
| 556 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Check if Bluetooth SCO audio is connected. |
| 562 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 563 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 564 | * |
| 565 | * @param device Bluetooth headset |
| 566 | * @return true if SCO is connected, |
| 567 | * false otherwise or on error |
| 568 | */ |
| 569 | public boolean isAudioConnected(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 570 | if (VDBG) log("isAudioConnected()"); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 571 | if (mService != null && isEnabled() && |
| 572 | isValidDevice(device)) { |
| 573 | try { |
| 574 | return mService.isAudioConnected(device); |
| 575 | } catch (RemoteException e) { |
| 576 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 577 | } |
| 578 | } |
| 579 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 580 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /** |
Nick Pelly | 6c901db | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 584 | * Get battery usage hint for Bluetooth Headset service. |
| 585 | * This is a monotonically increasing integer. Wraps to 0 at |
| 586 | * Integer.MAX_INT, and at boot. |
| 587 | * Current implementation returns the number of AT commands handled since |
| 588 | * boot. This is a good indicator for spammy headset/handsfree units that |
| 589 | * can keep the device awake by polling for cellular status updates. As a |
| 590 | * rule of thumb, each AT command prevents the CPU from sleeping for 500 ms |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 591 | * |
| 592 | * @param device the bluetooth headset. |
Nick Pelly | 6c901db | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 593 | * @return monotonically increasing battery usage hint, or a negative error |
| 594 | * code on error |
| 595 | * @hide |
| 596 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 597 | public int getBatteryUsageHint(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 598 | if (VDBG) log("getBatteryUsageHint()"); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 599 | if (mService != null && isEnabled() && |
| 600 | isValidDevice(device)) { |
Nick Pelly | 6c901db | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 601 | try { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 602 | return mService.getBatteryUsageHint(device); |
| 603 | } catch (RemoteException e) { |
| 604 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 605 | } |
Nick Pelly | 6c901db | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 606 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 607 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Nick Pelly | 6c901db | 2009-06-19 10:08:09 -0700 | [diff] [blame] | 608 | return -1; |
| 609 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 610 | |
Eric Laurent | d726b35 | 2010-03-17 14:59:27 -0700 | [diff] [blame] | 611 | /** |
| 612 | * Indicates if current platform supports voice dialing over bluetooth SCO. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 613 | * |
Eric Laurent | d726b35 | 2010-03-17 14:59:27 -0700 | [diff] [blame] | 614 | * @return true if voice dialing over bluetooth is supported, false otherwise. |
| 615 | * @hide |
| 616 | */ |
| 617 | public static boolean isBluetoothVoiceDialingEnabled(Context context) { |
| 618 | return context.getResources().getBoolean( |
| 619 | com.android.internal.R.bool.config_bluetooth_sco_off_call); |
| 620 | } |
| 621 | |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 622 | /** |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 623 | * Accept the incoming connection. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 624 | * Note: This is an internal function and shouldn't be exposed |
| 625 | * |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 626 | * @hide |
| 627 | */ |
| 628 | public boolean acceptIncomingConnect(BluetoothDevice device) { |
| 629 | if (DBG) log("acceptIncomingConnect"); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 630 | if (mService != null && isEnabled()) { |
Jaikumar Ganesh | 9b637e5 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 631 | try { |
| 632 | return mService.acceptIncomingConnect(device); |
| 633 | } catch (RemoteException e) {Log.e(TAG, e.toString());} |
| 634 | } else { |
| 635 | Log.w(TAG, "Proxy not attached to service"); |
| 636 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 637 | } |
| 638 | return false; |
| 639 | } |
| 640 | |
| 641 | /** |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 642 | * Reject the incoming connection. |
| 643 | * @hide |
| 644 | */ |
| 645 | public boolean rejectIncomingConnect(BluetoothDevice device) { |
| 646 | if (DBG) log("rejectIncomingConnect"); |
| 647 | if (mService != null) { |
| 648 | try { |
| 649 | return mService.rejectIncomingConnect(device); |
| 650 | } catch (RemoteException e) {Log.e(TAG, e.toString());} |
| 651 | } else { |
| 652 | Log.w(TAG, "Proxy not attached to service"); |
| 653 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 654 | } |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | /** |
Jaikumar Ganesh | 30d1816 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 659 | * Get the current audio state of the Headset. |
| 660 | * Note: This is an internal function and shouldn't be exposed |
| 661 | * |
| 662 | * @hide |
| 663 | */ |
| 664 | public int getAudioState(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 665 | if (VDBG) log("getAudioState"); |
Jaikumar Ganesh | 6f7a973 | 2011-04-06 11:09:30 -0700 | [diff] [blame] | 666 | if (mService != null && !isDisabled()) { |
Jaikumar Ganesh | 30d1816 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 667 | try { |
| 668 | return mService.getAudioState(device); |
| 669 | } catch (RemoteException e) {Log.e(TAG, e.toString());} |
| 670 | } else { |
| 671 | Log.w(TAG, "Proxy not attached to service"); |
| 672 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 673 | } |
| 674 | return BluetoothHeadset.STATE_AUDIO_DISCONNECTED; |
| 675 | } |
| 676 | |
Jaikumar Ganesh | f2e6b13 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 677 | /** |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 678 | * Check if Bluetooth SCO audio is connected. |
| 679 | * |
| 680 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 681 | * |
| 682 | * @return true if SCO is connected, |
| 683 | * false otherwise or on error |
| 684 | * @hide |
| 685 | */ |
| 686 | public boolean isAudioOn() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 687 | if (VDBG) log("isAudioOn()"); |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 688 | if (mService != null && isEnabled()) { |
| 689 | try { |
| 690 | return mService.isAudioOn(); |
| 691 | } catch (RemoteException e) { |
| 692 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 693 | } |
| 694 | } |
| 695 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 696 | return false; |
| 697 | |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * Initiates a connection of headset audio. |
| 702 | * It setup SCO channel with remote connected headset device. |
| 703 | * |
| 704 | * @return true if successful |
| 705 | * false if there was some error such as |
| 706 | * there is no connected headset |
| 707 | * @hide |
| 708 | */ |
| 709 | public boolean connectAudio() { |
| 710 | if (mService != null && isEnabled()) { |
| 711 | try { |
| 712 | return mService.connectAudio(); |
| 713 | } catch (RemoteException e) { |
| 714 | Log.e(TAG, e.toString()); |
| 715 | } |
| 716 | } else { |
| 717 | Log.w(TAG, "Proxy not attached to service"); |
| 718 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 719 | } |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Initiates a disconnection of headset audio. |
| 725 | * It tears down the SCO channel from remote headset device. |
| 726 | * |
| 727 | * @return true if successful |
| 728 | * false if there was some error such as |
| 729 | * there is no connected SCO channel |
| 730 | * @hide |
| 731 | */ |
| 732 | public boolean disconnectAudio() { |
| 733 | if (mService != null && isEnabled()) { |
| 734 | try { |
| 735 | return mService.disconnectAudio(); |
| 736 | } catch (RemoteException e) { |
| 737 | Log.e(TAG, e.toString()); |
| 738 | } |
| 739 | } else { |
| 740 | Log.w(TAG, "Proxy not attached to service"); |
| 741 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 742 | } |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | /** |
Jaikumar Ganesh | dde68c6 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 747 | * Initiates a SCO channel connection with the headset (if connected). |
| 748 | * Also initiates a virtual voice call for Handsfree devices as many devices |
| 749 | * do not accept SCO audio without a call. |
| 750 | * This API allows the handsfree device to be used for routing non-cellular |
| 751 | * call audio. |
Jaikumar Ganesh | f2e6b13 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 752 | * |
| 753 | * @param device Remote Bluetooth Device |
| 754 | * @return true if successful, false if there was some error. |
| 755 | * @hide |
| 756 | */ |
Jaikumar Ganesh | dde68c6 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 757 | public boolean startScoUsingVirtualVoiceCall(BluetoothDevice device) { |
| 758 | if (DBG) log("startScoUsingVirtualVoiceCall()"); |
Jaikumar Ganesh | f2e6b13 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 759 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 760 | try { |
Jaikumar Ganesh | dde68c6 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 761 | return mService.startScoUsingVirtualVoiceCall(device); |
Jaikumar Ganesh | f2e6b13 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 762 | } catch (RemoteException e) { |
| 763 | Log.e(TAG, e.toString()); |
| 764 | } |
| 765 | } else { |
| 766 | Log.w(TAG, "Proxy not attached to service"); |
| 767 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 768 | } |
| 769 | return false; |
| 770 | } |
| 771 | |
| 772 | /** |
Jaikumar Ganesh | dde68c6 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 773 | * Terminates an ongoing SCO connection and the associated virtual |
| 774 | * call. |
Jaikumar Ganesh | f2e6b13 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 775 | * |
| 776 | * @param device Remote Bluetooth Device |
| 777 | * @return true if successful, false if there was some error. |
| 778 | * @hide |
| 779 | */ |
Jaikumar Ganesh | dde68c6 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 780 | public boolean stopScoUsingVirtualVoiceCall(BluetoothDevice device) { |
| 781 | if (DBG) log("stopScoUsingVirtualVoiceCall()"); |
Jaikumar Ganesh | f2e6b13 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 782 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 783 | try { |
Jaikumar Ganesh | dde68c6 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 784 | return mService.stopScoUsingVirtualVoiceCall(device); |
Jaikumar Ganesh | f2e6b13 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 785 | } catch (RemoteException e) { |
| 786 | Log.e(TAG, e.toString()); |
| 787 | } |
| 788 | } else { |
| 789 | Log.w(TAG, "Proxy not attached to service"); |
| 790 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 791 | } |
| 792 | return false; |
| 793 | } |
| 794 | |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 795 | /** |
| 796 | * Notify Headset of phone state change. |
| 797 | * This is a backdoor for phone app to call BluetoothHeadset since |
| 798 | * there is currently not a good way to get precise call state change outside |
| 799 | * of phone app. |
| 800 | * |
| 801 | * @hide |
| 802 | */ |
| 803 | public void phoneStateChanged(int numActive, int numHeld, int callState, String number, |
| 804 | int type) { |
| 805 | if (mService != null && isEnabled()) { |
| 806 | try { |
| 807 | mService.phoneStateChanged(numActive, numHeld, callState, number, type); |
| 808 | } catch (RemoteException e) { |
| 809 | Log.e(TAG, e.toString()); |
| 810 | } |
| 811 | } else { |
| 812 | Log.w(TAG, "Proxy not attached to service"); |
| 813 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /** |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 818 | * Send Headset of CLCC response |
| 819 | * |
| 820 | * @hide |
| 821 | */ |
| 822 | public void clccResponse(int index, int direction, int status, int mode, boolean mpty, |
| 823 | String number, int type) { |
| 824 | if (mService != null && isEnabled()) { |
| 825 | try { |
| 826 | mService.clccResponse(index, direction, status, mode, mpty, number, type); |
| 827 | } catch (RemoteException e) { |
| 828 | Log.e(TAG, e.toString()); |
| 829 | } |
| 830 | } else { |
| 831 | Log.w(TAG, "Proxy not attached to service"); |
| 832 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 833 | } |
| 834 | } |
| 835 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | private ServiceConnection mConnection = new ServiceConnection() { |
| 837 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 838 | if (DBG) Log.d(TAG, "Proxy object connected"); |
| 839 | mService = IBluetoothHeadset.Stub.asInterface(service); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 840 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | if (mServiceListener != null) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 842 | mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, BluetoothHeadset.this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | public void onServiceDisconnected(ComponentName className) { |
| 846 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 847 | mService = null; |
| 848 | if (mServiceListener != null) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 849 | mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | }; |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 853 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 854 | private boolean isEnabled() { |
| 855 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 856 | return false; |
| 857 | } |
| 858 | |
Jaikumar Ganesh | 6f7a973 | 2011-04-06 11:09:30 -0700 | [diff] [blame] | 859 | private boolean isDisabled() { |
| 860 | if (mAdapter.getState() == BluetoothAdapter.STATE_OFF) return true; |
| 861 | return false; |
| 862 | } |
| 863 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 864 | private boolean isValidDevice(BluetoothDevice device) { |
| 865 | if (device == null) return false; |
| 866 | |
| 867 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 868 | return false; |
| 869 | } |
| 870 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 871 | private static void log(String msg) { |
| 872 | Log.d(TAG, msg); |
| 873 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | } |