19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.bluetooth;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pellyimport android.annotation.SdkConstant;
20005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pellyimport android.annotation.SdkConstant.SdkConstantType;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.ComponentName;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
230a17db1cc5942ea000ca87bb72853de57a15ec64Jeff Sharkeyimport android.os.Binder;
24e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franzimport android.os.Handler;
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.IBinder;
26e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franzimport android.os.Looper;
27e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franzimport android.os.Message;
2803cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganeshimport android.os.RemoteException;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3103cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganeshimport java.util.ArrayList;
3203cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganeshimport java.util.List;
3362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Public API for controlling the Bluetooth Headset Service. This includes both
3662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Bluetooth Headset and Handsfree (v1.5) profiles.
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
3862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p>BluetoothHeadset is a proxy object for controlling the Bluetooth Headset
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Service via IPC.
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
4162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p> Use {@link BluetoothAdapter#getProfileProxy} to get
4262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * the BluetoothHeadset proxy object. Use
4362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@link BluetoothAdapter#closeProfileProxy} to close the service connection.
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
4562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p> Android only supports one connected Bluetooth Headset at a time.
4662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Each method is protected with its appropriate permission.
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
4862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganeshpublic final class BluetoothHeadset implements BluetoothProfile {
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final String TAG = "BluetoothHeadset";
500f42037eb7b5118015c2caca635538324ccf0ccffredc    private static final boolean DBG = true;
51563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie    private static final boolean VDBG = false;
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
5462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Intent used to broadcast the change in connection state of the Headset
5562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * profile.
5662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
5762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * <p>This intent will have 3 extras:
58c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <ul>
59c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
60c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li>
61c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
62c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * </ul>
630706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
6462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
6562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
6662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
67c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
68c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * receive.
6962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     */
70005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
7162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public static final String ACTION_CONNECTION_STATE_CHANGED =
7262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED";
7362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
74005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    /**
7562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Intent used to broadcast the change in the Audio Connection state of the
7662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * A2DP profile.
7762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
7862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * <p>This intent will have 3 extras:
79c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <ul>
80c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
81c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li>
82c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
83c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * </ul>
840706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
8562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #STATE_AUDIO_CONNECTED}, {@link #STATE_AUDIO_DISCONNECTED},
8662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
87c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission
88c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * to receive.
89005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     */
90005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
91005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    public static final String ACTION_AUDIO_STATE_CHANGED =
9262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        "android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED";
9362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
94c24dbdb57b30f9e8fa6b0c5b48372017b5ae46b0Jaikumar Ganesh
95005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly    /**
96e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * Intent used to broadcast that the headset has posted a
97e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * vendor-specific event.
98e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     *
99e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * <p>This intent will have 4 extras and 1 category.
100c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <ul>
101c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *  <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote Bluetooth Device
102c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *       </li>
103c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *  <li> {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD} - The vendor
104c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *       specific command </li>
105c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *  <li> {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} - The AT
106c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *       command type which can be one of  {@link #AT_CMD_TYPE_READ},
107c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *       {@link #AT_CMD_TYPE_TEST}, or {@link #AT_CMD_TYPE_SET},
1080706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     *       {@link #AT_CMD_TYPE_BASIC},{@link #AT_CMD_TYPE_ACTION}. </li>
109c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *  <li> {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS} - Command
110c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *       arguments. </li>
111c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * </ul>
112e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     *
1130706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     *<p> The category is the Company ID of the vendor defining the
114e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * vendor-specific command. {@link BluetoothAssignedNumbers}
115e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     *
116e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * For example, for Plantronics specific events
117e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * Category will be {@link #VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY}.55
118e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     *
119e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * <p> For example, an AT+XEVENT=foo,3 will get translated into
120c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <ul>
121c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD = +XEVENT </li>
122c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE = AT_CMD_TYPE_SET </li>
1230706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     *   <li> EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS = foo, 3 </li>
124c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * </ul>
125c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission
126c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * to receive.
127a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek     */
128a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
129a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek    public static final String ACTION_VENDOR_SPECIFIC_HEADSET_EVENT =
130a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek            "android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT";
131a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek
132a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek    /**
133a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek     * A String extra field in {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT}
134a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek     * intents that contains the name of the vendor-specific command.
135a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek     */
136a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek    public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD =
137a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek            "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD";
138a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek
139a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek    /**
140a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek     * An int extra field in {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT}
141e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * intents that contains the AT command type of the vendor-specific command.
142e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     */
143e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE =
144e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh            "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE";
145e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh
146e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    /**
147e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * AT command type READ used with
148e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE}
149e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * For example, AT+VGM?. There are no arguments for this command type.
150e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     */
151e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    public static final int AT_CMD_TYPE_READ = 0;
152e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh
153e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    /**
154e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * AT command type TEST used with
155e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE}
156e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * For example, AT+VGM=?. There are no arguments for this command type.
157e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     */
158e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    public static final int AT_CMD_TYPE_TEST = 1;
159e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh
160e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    /**
161e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * AT command type SET used with
162e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE}
163e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * For example, AT+VGM=<args>.
164e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     */
165e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    public static final int AT_CMD_TYPE_SET = 2;
166e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh
167e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    /**
168e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * AT command type BASIC used with
169e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE}
170e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * For example, ATD. Single character commands and everything following the
171e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * character are arguments.
172e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     */
173e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    public static final int AT_CMD_TYPE_BASIC = 3;
174e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh
175e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    /**
176e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * AT command type ACTION used with
177e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE}
178e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * For example, AT+CHUP. There are no arguments for action commands.
179a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek     */
180e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    public static final int AT_CMD_TYPE_ACTION = 4;
181a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek
182a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek    /**
183a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek     * A Parcelable String array extra field in
184a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek     * {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} intents that contains
185a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek     * the arguments to the vendor-specific command.
186a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek     */
187a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek    public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS =
188a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek            "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_ARGS";
189a4733941839abd672b1e37edd7f61c8932d4aa48Herb Jellinek
190e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    /**
191e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * The intent category to be used with {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT}
192e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     * for the companyId
193e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh     */
194e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh    public static final String VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY  =
195e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh            "android.bluetooth.headset.intent.category.companyid";
196e775b3daab9766bce9ec2d01fe7d652c4a782b6bJaikumar Ganesh
19730d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh    /**
198922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     * A vendor-specific command for unsolicited result code.
199922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     */
200922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee    public static final String VENDOR_RESULT_CODE_COMMAND_ANDROID = "+ANDROID";
201922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee
202922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee    /**
203c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * Headset state when SCO audio is not connected.
20462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * This state can be one of
20562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of
20662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #ACTION_AUDIO_STATE_CHANGED} intent.
207005b228cdfb369d9b3b325884c0337ba5968bf8cNick Pelly     */
208b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh    public static final int STATE_AUDIO_DISCONNECTED = 10;
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
211c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * Headset state when SCO audio is connecting.
21262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * This state can be one of
21362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of
21462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #ACTION_AUDIO_STATE_CHANGED} intent.
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
216b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh    public static final int STATE_AUDIO_CONNECTING = 11;
21762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
21830d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh    /**
219c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * Headset state when SCO audio is connected.
22030d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh     * This state can be one of
22130d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh     * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of
22230d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh     * {@link #ACTION_AUDIO_STATE_CHANGED} intent.
22330d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh     */
2240d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth
2250d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth    /**
2260d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * Intent used to broadcast the headset's indicator status
2270d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     *
2280d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * <p>This intent will have 3 extras:
2290d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * <ul>
2300d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     *   <li> {@link #EXTRA_IND_ID} - The Assigned number of headset Indicator which is supported by
2310d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth                                        the headset ( as indicated by AT+BIND
2320d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth                                        command in the SLC sequence).or whose value
2330d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth                                        is changed (indicated by AT+BIEV command)</li>
2340d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     *   <li> {@link #EXTRA_IND_VALUE}- The updated value of headset indicator. </li>
2350d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     *   <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
2360d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * </ul>
2370d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * <p>{@link #EXTRA_IND_ID} is defined by Bluetooth SIG and each of the indicators are
2380d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * given an assigned number. Below shows the assigned number of Indicator added so far
2390d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * - Enhanced Safety - 1
2400d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
2410d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * receive.
2420d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * @hide
2430d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     */
2440d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth    public static final String ACTION_HF_INDICATORS_VALUE_CHANGED =
2450d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth            "android.bluetooth.headset.action.HF_INDICATORS_VALUE_CHANGED";
2460d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth
2470d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth    /**
2480d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * A String extra field in {@link #ACTION_HF_INDICATORS_VALUE_CHANGED}
2490d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * intents that contains the UUID of the headset  indicator (as defined by Bluetooth SIG)
2500d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * that is being sent.
2510d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * @hide
2520d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     */
2530d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth    public static final String EXTRA_HF_INDICATORS_IND_ID =
2540d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth            "android.bluetooth.headset.extra.HF_INDICATORS_IND_ID";
2550d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth
2560d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth    /**
2570d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * A int  extra field in {@link #ACTION_HF_INDICATORS_VALUE_CHANGED}
2580d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * intents that contains the value of the Headset indicator that is being sent.
2590d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * @hide
2600d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     */
2610d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth    public static final String EXTRA_HF_INDICATORS_IND_VALUE =
2620d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth            "android.bluetooth.headset.extra.HF_INDICATORS_IND_VALUE";
2630d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth
264b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh    public static final int STATE_AUDIO_CONNECTED = 12;
265b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh
266e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz    private static final int MESSAGE_HEADSET_SERVICE_CONNECTED = 100;
267e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz    private static final int MESSAGE_HEADSET_SERVICE_DISCONNECTED = 101;
26862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
26962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private Context mContext;
27062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private ServiceListener mServiceListener;
27162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private IBluetoothHeadset mService;
272bf246ef0abb6ea354fe412b139dec1adb4e5791dMatthew Xie    private BluetoothAdapter mAdapter;
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2740f42037eb7b5118015c2caca635538324ccf0ccffredc    final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
2750f42037eb7b5118015c2caca635538324ccf0ccffredc            new IBluetoothStateChangeCallback.Stub() {
2760f42037eb7b5118015c2caca635538324ccf0ccffredc                public void onBluetoothStateChange(boolean up) {
2770f42037eb7b5118015c2caca635538324ccf0ccffredc                    if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
2780f42037eb7b5118015c2caca635538324ccf0ccffredc                    if (!up) {
279563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie                        if (VDBG) Log.d(TAG,"Unbinding service...");
280e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                        doUnbind();
2810f42037eb7b5118015c2caca635538324ccf0ccffredc                    } else {
2820f42037eb7b5118015c2caca635538324ccf0ccffredc                        synchronized (mConnection) {
2830f42037eb7b5118015c2caca635538324ccf0ccffredc                            try {
2840f42037eb7b5118015c2caca635538324ccf0ccffredc                                if (mService == null) {
285563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie                                    if (VDBG) Log.d(TAG,"Binding service...");
286221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn                                    doBind();
2870f42037eb7b5118015c2caca635538324ccf0ccffredc                                }
2880f42037eb7b5118015c2caca635538324ccf0ccffredc                            } catch (Exception re) {
2890f42037eb7b5118015c2caca635538324ccf0ccffredc                                Log.e(TAG,"",re);
2900f42037eb7b5118015c2caca635538324ccf0ccffredc                            }
2910f42037eb7b5118015c2caca635538324ccf0ccffredc                        }
2920f42037eb7b5118015c2caca635538324ccf0ccffredc                    }
2930f42037eb7b5118015c2caca635538324ccf0ccffredc                }
2940f42037eb7b5118015c2caca635538324ccf0ccffredc        };
2950f42037eb7b5118015c2caca635538324ccf0ccffredc
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Create a BluetoothHeadset proxy object.
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
29962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /*package*/ BluetoothHeadset(Context context, ServiceListener l) {
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mContext = context;
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mServiceListener = l;
30262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        mAdapter = BluetoothAdapter.getDefaultAdapter();
3030f42037eb7b5118015c2caca635538324ccf0ccffredc
3040f42037eb7b5118015c2caca635538324ccf0ccffredc        IBluetoothManager mgr = mAdapter.getBluetoothManager();
3050f42037eb7b5118015c2caca635538324ccf0ccffredc        if (mgr != null) {
3060f42037eb7b5118015c2caca635538324ccf0ccffredc            try {
3070f42037eb7b5118015c2caca635538324ccf0ccffredc                mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
3080f42037eb7b5118015c2caca635538324ccf0ccffredc            } catch (RemoteException e) {
3090f42037eb7b5118015c2caca635538324ccf0ccffredc                Log.e(TAG,"",e);
3100f42037eb7b5118015c2caca635538324ccf0ccffredc            }
3110f42037eb7b5118015c2caca635538324ccf0ccffredc        }
3120f42037eb7b5118015c2caca635538324ccf0ccffredc
313221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        doBind();
314221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn    }
315221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn
316221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn    boolean doBind() {
317e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        try {
318e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz            return mAdapter.getBluetoothManager().bindBluetoothProfileService(
319e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    BluetoothProfile.HEADSET, mConnection);
320e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        } catch (RemoteException e) {
321e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz            Log.e(TAG, "Unable to bind HeadsetService", e);
322e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        }
323e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        return false;
324e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz    }
325e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz
326e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz    void doUnbind() {
327e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        synchronized (mConnection) {
328e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz            if (mService != null) {
329e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                try {
330e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    mAdapter.getBluetoothManager().unbindBluetoothProfileService(
331e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                            BluetoothProfile.HEADSET, mConnection);
332e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                } catch (RemoteException e) {
333e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    Log.e(TAG,"Unable to unbind HeadsetService", e);
334e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                }
335e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz            }
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Close the connection to the backing service.
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Other public functions of BluetoothHeadset will return default error
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * results once close() has been called. Multiple invocations of close()
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * are ok.
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
34513450df2b9264ef2220418f308037c19cec739a9Matthew Xie    /*package*/ void close() {
346563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("close()");
3470f42037eb7b5118015c2caca635538324ccf0ccffredc
3480f42037eb7b5118015c2caca635538324ccf0ccffredc        IBluetoothManager mgr = mAdapter.getBluetoothManager();
3490f42037eb7b5118015c2caca635538324ccf0ccffredc        if (mgr != null) {
3500f42037eb7b5118015c2caca635538324ccf0ccffredc            try {
3510f42037eb7b5118015c2caca635538324ccf0ccffredc                mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
3520f42037eb7b5118015c2caca635538324ccf0ccffredc            } catch (Exception e) {
3530f42037eb7b5118015c2caca635538324ccf0ccffredc                Log.e(TAG,"",e);
3540f42037eb7b5118015c2caca635538324ccf0ccffredc            }
3550f42037eb7b5118015c2caca635538324ccf0ccffredc        }
356c88b6bdcfada95447269b41d388fd7028dba574bBenjamin Franz        mServiceListener = null;
357e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        doUnbind();
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
361f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Initiate connection to a profile of the remote bluetooth device.
362f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
363f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> Currently, the system supports only 1 connection to the
364f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * headset/handsfree profile. The API will automatically disconnect connected
365f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * devices before connecting.
366f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
367f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> This API returns false in scenarios like the profile on the
368f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * device is already connected or Bluetooth is not turned on.
369f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * When this API returns true, it is guaranteed that
370f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * connection state intent for the profile will be broadcasted with
371f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * the state. Users can get the connection state of the profile
372f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * from this intent.
373f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
374f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
375f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
376f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
377f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Remote Bluetooth Device
378f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return false on immediate error,
379f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *               true otherwise
38062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
38262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean connect(BluetoothDevice device) {
38362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("connect(" + device + ")");
38462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
38562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
38762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.connect(device);
38862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
38962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, Log.getStackTraceString(new Throwable()));
39062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return false;
39162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
39362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
39462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
398f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Initiate disconnection from a profile
399f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
400f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> This API will return false in scenarios like the profile on the
401f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Bluetooth device is not in connected state etc. When this API returns,
402f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * true, it is guaranteed that the connection state change
403f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * intent will be broadcasted with the state. Users can get the
404f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * disconnection state of the profile from this intent.
405f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
406f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> If the disconnection is initiated by a remote device, the state
407f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * will transition from {@link #STATE_CONNECTED} to
408f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the
409f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * host (local) device the state will transition from
410f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to
411f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * state {@link #STATE_DISCONNECTED}. The transition to
412f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_DISCONNECTING} can be used to distinguish between the
413f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * two scenarios.
414f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
415f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
416f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
417f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
418f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Remote Bluetooth Device
419f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return false on immediate error,
420f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *               true otherwise
42162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
42362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean disconnect(BluetoothDevice device) {
42462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("disconnect(" + device + ")");
42562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
42662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
42862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.disconnect(device);
42962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
43062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh              Log.e(TAG, Log.getStackTraceString(new Throwable()));
43162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh              return false;
43262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
43462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
43562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
43962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
44103cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh    public List<BluetoothDevice> getConnectedDevices() {
442563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getConnectedDevices()");
44362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()) {
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
44503cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return mService.getConnectedDevices();
44662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
44762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, Log.getStackTraceString(new Throwable()));
44803cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return new ArrayList<BluetoothDevice>();
44962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
45162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
45203cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh        return new ArrayList<BluetoothDevice>();
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
45662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
45803cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh    public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
459563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getDevicesMatchingStates()");
46062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()) {
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
46203cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return mService.getDevicesMatchingConnectionStates(states);
46362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
46462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, Log.getStackTraceString(new Throwable()));
46503cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return new ArrayList<BluetoothDevice>();
46662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
46862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
46903cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh        return new ArrayList<BluetoothDevice>();
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
47362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
47562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public int getConnectionState(BluetoothDevice device) {
476563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getConnectionState(" + device + ")");
47762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
47862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
48062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.getConnectionState(device);
48162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
48262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, Log.getStackTraceString(new Throwable()));
48362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return BluetoothProfile.STATE_DISCONNECTED;
48462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
48662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
48762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return BluetoothProfile.STATE_DISCONNECTED;
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
491f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Set priority of the profile
492f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
493f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> The device should already be paired.
494f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *  Priority can be one of {@link #PRIORITY_ON} or
495f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_OFF},
496f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
497f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
498f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
499f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
500f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Paired bluetooth device
501f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param priority
502f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return true if priority is set, false on error
50362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
50562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean setPriority(BluetoothDevice device, int priority) {
50662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("setPriority(" + device + ", " + priority + ")");
50762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
50862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
50962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            if (priority != BluetoothProfile.PRIORITY_OFF &&
5106f6c54519a9b15379bb79e195655b6bd4fc5967aGanesh Ganapathi Batta                priority != BluetoothProfile.PRIORITY_ON) {
51162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh              return false;
51262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
51462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.setPriority(device, priority);
51562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
51662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, Log.getStackTraceString(new Throwable()));
51762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return false;
51862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
52062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
525f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Get the priority of the profile.
526f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
527f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> The priority can be any of:
528f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF},
529f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}
530f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
531f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
532f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
533f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Bluetooth device
534f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return priority of the device
53562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
53762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public int getPriority(BluetoothDevice device) {
538563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getPriority(" + device + ")");
53962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
54062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
54262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.getPriority(device);
54362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
54462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, Log.getStackTraceString(new Throwable()));
54562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return PRIORITY_OFF;
54662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
54862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
54962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return PRIORITY_OFF;
55062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    }
55162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
55262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
55362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Start Bluetooth voice recognition. This methods sends the voice
55462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * recognition AT command to the headset and establishes the
55562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * audio connection.
55662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
55762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * <p> Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}.
558b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh     * If this function returns true, this intent will be broadcasted with
559b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh     * {@link #EXTRA_STATE} set to {@link #STATE_AUDIO_CONNECTING}.
560b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh     *
561b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh     * <p> {@link #EXTRA_STATE} will transition from
562b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh     * {@link #STATE_AUDIO_CONNECTING} to {@link #STATE_AUDIO_CONNECTED} when
563b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh     * audio connection is established and to {@link #STATE_AUDIO_DISCONNECTED}
564b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh     * in case of failure to establish the audio connection.
56562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
566b0a1d01b4c044a0779cfe006e204bac468459802Jaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
56762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
56862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @param device Bluetooth headset
56962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @return false if there is no headset connected of if the
57062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *               connected headset doesn't support voice recognition
57162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *               or on error, true otherwise
57262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     */
57362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean startVoiceRecognition(BluetoothDevice device) {
57462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("startVoiceRecognition()");
57562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
57662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
57762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
57862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.startVoiceRecognition(device);
57962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
58062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG,  Log.getStackTraceString(new Throwable()));
58162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
58262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        }
58362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
58862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Stop Bluetooth Voice Recognition mode, and shut down the
58962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Bluetooth audio path.
59062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
591c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
59262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
59362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @param device Bluetooth headset
59462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @return false if there is no headset connected
59562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *               or on error, true otherwise
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
59762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean stopVoiceRecognition(BluetoothDevice device) {
59862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("stopVoiceRecognition()");
59962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
60062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
60262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.stopVoiceRecognition(device);
60362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
60462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG,  Log.getStackTraceString(new Throwable()));
60562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
60762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
61262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Check if Bluetooth SCO audio is connected.
61362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
614c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
61562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
61662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @param device Bluetooth headset
61762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @return true if SCO is connected,
61862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *         false otherwise or on error
6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
62062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean isAudioConnected(BluetoothDevice device) {
621563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("isAudioConnected()");
62262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
62362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
62562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh              return mService.isAudioConnected(device);
62662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
62762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh              Log.e(TAG,  Log.getStackTraceString(new Throwable()));
62862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
6296c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly        }
63062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
63162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
6326c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly    }
6336c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly
6346c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly    /**
6356c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     * Get battery usage hint for Bluetooth Headset service.
6366c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     * This is a monotonically increasing integer. Wraps to 0 at
6376c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     * Integer.MAX_INT, and at boot.
6386c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     * Current implementation returns the number of AT commands handled since
6396c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     * boot. This is a good indicator for spammy headset/handsfree units that
6406c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     * can keep the device awake by polling for cellular status updates. As a
6416c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     * rule of thumb, each AT command prevents the CPU from sleeping for 500 ms
64262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
64362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @param device the bluetooth headset.
6446c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     * @return monotonically increasing battery usage hint, or a negative error
6456c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     *         code on error
6466c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     * @hide
6476c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly     */
64862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public int getBatteryUsageHint(BluetoothDevice device) {
649563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getBatteryUsageHint()");
65062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
65162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
6526c901db72dbaf57d8fdf26adae6721de14ecae22Nick Pelly            try {
65362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.getBatteryUsageHint(device);
65462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
65562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG,  Log.getStackTraceString(new Throwable()));
65662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
65862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
66162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
662d726b35ebd8660022dcea706ee6d3ca51886b04eEric Laurent    /**
663d726b35ebd8660022dcea706ee6d3ca51886b04eEric Laurent     * Indicates if current platform supports voice dialing over bluetooth SCO.
66462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
665d726b35ebd8660022dcea706ee6d3ca51886b04eEric Laurent     * @return true if voice dialing over bluetooth is supported, false otherwise.
666d726b35ebd8660022dcea706ee6d3ca51886b04eEric Laurent     * @hide
667d726b35ebd8660022dcea706ee6d3ca51886b04eEric Laurent     */
668d726b35ebd8660022dcea706ee6d3ca51886b04eEric Laurent    public static boolean isBluetoothVoiceDialingEnabled(Context context) {
669d726b35ebd8660022dcea706ee6d3ca51886b04eEric Laurent        return context.getResources().getBoolean(
670d726b35ebd8660022dcea706ee6d3ca51886b04eEric Laurent                com.android.internal.R.bool.config_bluetooth_sco_off_call);
671d726b35ebd8660022dcea706ee6d3ca51886b04eEric Laurent    }
672d726b35ebd8660022dcea706ee6d3ca51886b04eEric Laurent
6739b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    /**
6749b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh     * Accept the incoming connection.
67562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Note: This is an internal function and shouldn't be exposed
67662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
6779b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh     * @hide
6789b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh     */
6799b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    public boolean acceptIncomingConnect(BluetoothDevice device) {
6809b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        if (DBG) log("acceptIncomingConnect");
68162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()) {
6829b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            try {
6839b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh                return mService.acceptIncomingConnect(device);
6849b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            } catch (RemoteException e) {Log.e(TAG, e.toString());}
6859b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        } else {
6869b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            Log.w(TAG, "Proxy not attached to service");
6879b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
6889b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
6899b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        return false;
6909b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
6919b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
6929b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    /**
693a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie     * Reject the incoming connection.
694a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie     * @hide
695a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie     */
696a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    public boolean rejectIncomingConnect(BluetoothDevice device) {
697a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        if (DBG) log("rejectIncomingConnect");
698a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        if (mService != null) {
699a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            try {
700a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie                return mService.rejectIncomingConnect(device);
701a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            } catch (RemoteException e) {Log.e(TAG, e.toString());}
702a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        } else {
703a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            Log.w(TAG, "Proxy not attached to service");
704a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
705a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        }
706a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie        return false;
707a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    }
708a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie
709a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    /**
7103e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * Get the current audio state of the Headset.
7119b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh     * Note: This is an internal function and shouldn't be exposed
71262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
7139b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh     * @hide
7149b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh     */
7153e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    public int getAudioState(BluetoothDevice device) {
716563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getAudioState");
7173e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        if (mService != null && !isDisabled()) {
7189b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            try {
7193e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                return mService.getAudioState(device);
7209b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            } catch (RemoteException e) {Log.e(TAG, e.toString());}
7219b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        } else {
7229b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            Log.w(TAG, "Proxy not attached to service");
7239b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
7249b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
7253e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        return BluetoothHeadset.STATE_AUDIO_DISCONNECTED;
7269b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
7279b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh
7289b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    /**
7290db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     * Sets whether audio routing is allowed. When set to {@code false}, the AG will not route any
7300db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     * audio to the HF unless explicitly told to.
7310db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     * This method should be used in cases where the SCO channel is shared between multiple profiles
7320db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     * and must be delegated by a source knowledgeable
7330db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     * Note: This is an internal function and shouldn't be exposed
7340db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     *
7350db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     * @param allowed {@code true} if the profile can reroute audio, {@code false} otherwise.
7360db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     *
7370db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     * @hide
7380db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     */
7390db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee    public void setAudioRouteAllowed(boolean allowed) {
7400db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee        if (VDBG) log("setAudioRouteAllowed");
7410db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee        if (mService != null && isEnabled()) {
7420db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee            try {
7430db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee                mService.setAudioRouteAllowed(allowed);
7440db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee            } catch (RemoteException e) {Log.e(TAG, e.toString());}
7450db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee        } else {
7460db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee            Log.w(TAG, "Proxy not attached to service");
7470db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
7480db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee        }
7490db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee    }
7500db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee
7510db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee    /**
7520db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     * Returns whether audio routing is allowed. see {@link #setAudioRouteAllowed(boolean)}.
7530db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     * Note: This is an internal function and shouldn't be exposed
7540db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     *
7550db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     * @hide
7560db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee     */
7570db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee    public boolean getAudioRouteAllowed() {
7580db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee        if (VDBG) log("getAudioRouteAllowed");
7590db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee        if (mService != null && isEnabled()) {
7600db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee            try {
7610db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee                return mService.getAudioRouteAllowed();
7620db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee            } catch (RemoteException e) {Log.e(TAG, e.toString());}
7630db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee        } else {
7640db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee            Log.w(TAG, "Proxy not attached to service");
7650db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
7660db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee        }
7670db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee        return false;
7680db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee    }
7690db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee
7700db53d90a028b138a68ebc5f7e30fe776e789b5dBryce Lee    /**
7713e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * Check if Bluetooth SCO audio is connected.
7723e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     *
7733e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
77462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
7753e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * @return true if SCO is connected,
7763e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     *         false otherwise or on error
7779b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh     * @hide
7789b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh     */
7793e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    public boolean isAudioOn() {
780563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("isAudioOn()");
7813e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        if (mService != null && isEnabled()) {
7829b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh            try {
7833e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie              return mService.isAudioOn();
7843e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            } catch (RemoteException e) {
7853e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie              Log.e(TAG,  Log.getStackTraceString(new Throwable()));
7863e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
7879b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        }
7883e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        if (mService == null) Log.w(TAG, "Proxy not attached to service");
7899b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh        return false;
7903e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie
7919b637e5985f9a86f39d70335c0390ade3716592aJaikumar Ganesh    }
79262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
79362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
7943e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * Initiates a connection of headset audio.
7953e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * It setup SCO channel with remote connected headset device.
79662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
7973e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * @return true if successful
7983e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     *         false if there was some error such as
7993e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     *               there is no connected headset
80062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
80162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     */
8023e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    public boolean connectAudio() {
8033e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        if (mService != null && isEnabled()) {
80462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
8053e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                return mService.connectAudio();
8063e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            } catch (RemoteException e) {
8073e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                Log.e(TAG, e.toString());
8083e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
80962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        } else {
81062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            Log.w(TAG, "Proxy not attached to service");
81162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
81262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        }
81362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
81462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    }
81562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
81630d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh    /**
8173e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * Initiates a disconnection of headset audio.
8183e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * It tears down the SCO channel from remote headset device.
81930d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh     *
8203e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * @return true if successful
8213e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     *         false if there was some error such as
8223e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     *               there is no connected SCO channel
82330d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh     * @hide
82430d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh     */
8253e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    public boolean disconnectAudio() {
8263e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        if (mService != null && isEnabled()) {
82730d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh            try {
8283e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                return mService.disconnectAudio();
8293e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            } catch (RemoteException e) {
8303e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                Log.e(TAG, e.toString());
8313e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
83230d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh        } else {
83330d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh            Log.w(TAG, "Proxy not attached to service");
83430d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
83530d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh        }
8363e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        return false;
83730d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh    }
83830d181690e48b26cdfae3b144d23f1e16c75da37Jaikumar Ganesh
839f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh    /**
840dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh     * Initiates a SCO channel connection with the headset (if connected).
841dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh     * Also initiates a virtual voice call for Handsfree devices as many devices
842dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh     * do not accept SCO audio without a call.
843dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh     * This API allows the handsfree device to be used for routing non-cellular
844dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh     * call audio.
845f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh     *
846f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh     * @param device Remote Bluetooth Device
847f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh     * @return true if successful, false if there was some error.
848f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh     * @hide
849f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh     */
850dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh    public boolean startScoUsingVirtualVoiceCall(BluetoothDevice device) {
851dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh        if (DBG) log("startScoUsingVirtualVoiceCall()");
852f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh        if (mService != null && isEnabled() && isValidDevice(device)) {
853f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh            try {
854dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh                return mService.startScoUsingVirtualVoiceCall(device);
855f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh            } catch (RemoteException e) {
856f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh                Log.e(TAG, e.toString());
857f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh            }
858f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh        } else {
859f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh            Log.w(TAG, "Proxy not attached to service");
860f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
861f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh        }
862f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh        return false;
863f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh    }
864f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh
865f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh    /**
866dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh     * Terminates an ongoing SCO connection and the associated virtual
867dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh     * call.
868f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh     *
869f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh     * @param device Remote Bluetooth Device
870f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh     * @return true if successful, false if there was some error.
871f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh     * @hide
872f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh     */
873dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh    public boolean stopScoUsingVirtualVoiceCall(BluetoothDevice device) {
874dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh        if (DBG) log("stopScoUsingVirtualVoiceCall()");
875f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh        if (mService != null && isEnabled() && isValidDevice(device)) {
876f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh            try {
877dde68c64fd8e97a592633ec4c09283ec928e5697Jaikumar Ganesh                return mService.stopScoUsingVirtualVoiceCall(device);
878f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh            } catch (RemoteException e) {
879f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh                Log.e(TAG, e.toString());
880f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh            }
881f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh        } else {
882f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh            Log.w(TAG, "Proxy not attached to service");
883f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
884f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh        }
885f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh        return false;
886f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh    }
887f2e6b13620f3ebbb94166834abaaabcc08a403b7Jaikumar Ganesh
8883e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    /**
8893e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * Notify Headset of phone state change.
8903e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * This is a backdoor for phone app to call BluetoothHeadset since
8913e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * there is currently not a good way to get precise call state change outside
8923e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * of phone app.
8933e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     *
8943e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * @hide
8953e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     */
8963e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    public void phoneStateChanged(int numActive, int numHeld, int callState, String number,
8973e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                                  int type) {
8983e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        if (mService != null && isEnabled()) {
8993e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            try {
9003e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                mService.phoneStateChanged(numActive, numHeld, callState, number, type);
9013e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            } catch (RemoteException e) {
9023e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                Log.e(TAG, e.toString());
9033e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
9043e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        } else {
9053e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            Log.w(TAG, "Proxy not attached to service");
9063e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
9073e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        }
9083e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    }
9093e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie
9103e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    /**
9113e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * Send Headset of CLCC response
9123e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     *
9133e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     * @hide
9143e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie     */
9153e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    public void clccResponse(int index, int direction, int status, int mode, boolean mpty,
9163e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                             String number, int type) {
9173e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        if (mService != null && isEnabled()) {
9183e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            try {
9193e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                mService.clccResponse(index, direction, status, mode, mpty, number, type);
9203e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            } catch (RemoteException e) {
9213e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                Log.e(TAG, e.toString());
9223e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
9233e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        } else {
9243e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            Log.w(TAG, "Proxy not attached to service");
9253e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
9263e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        }
9273e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    }
9283e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie
929922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee    /**
930922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     * Sends a vendor-specific unsolicited result code to the headset.
931922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     *
932922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     * <p>The actual string to be sent is <code>command + ": " + arg</code>.
933b240578f0333f5b35472cb11d6d7b5bb1da431ccYing Wang     * For example, if {@code command} is {@link #VENDOR_RESULT_CODE_COMMAND_ANDROID} and {@code arg}
934922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     * is {@code "0"}, the string <code>"+ANDROID: 0"</code> will be sent.
935922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     *
936b240578f0333f5b35472cb11d6d7b5bb1da431ccYing Wang     * <p>Currently only {@link #VENDOR_RESULT_CODE_COMMAND_ANDROID} is allowed as {@code command}.
937922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     *
938922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
939922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     *
940922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     * @param device Bluetooth headset.
941922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     * @param command A vendor-specific command.
942922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     * @param arg The argument that will be attached to the command.
943922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     * @return {@code false} if there is no headset connected, or if the command is not an allowed
944922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     *         vendor-specific unsolicited result code, or on error. {@code true} otherwise.
945922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     * @throws IllegalArgumentException if {@code command} is {@code null}.
946922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee     */
947922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee    public boolean sendVendorSpecificResultCode(BluetoothDevice device, String command,
948922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee            String arg) {
949922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee        if (DBG) {
950922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee            log("sendVendorSpecificResultCode()");
951922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee        }
952922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee        if (command == null) {
953922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee            throw new IllegalArgumentException("command is null");
954922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee        }
955922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee        if (mService != null && isEnabled() &&
956922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee                isValidDevice(device)) {
957922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee            try {
958922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee                return mService.sendVendorSpecificResultCode(device, command, arg);
959922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee            } catch (RemoteException e) {
960922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee                Log.e(TAG, Log.getStackTraceString(new Throwable()));
961922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee            }
962922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee        }
963922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee        if (mService == null) {
964922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee            Log.w(TAG, "Proxy not attached to service");
965922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee        }
966922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee        return false;
967922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee    }
968922d41b6ef202eac65991d27c43d74c828256f0dEdward Jee
969177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth    /**
970177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     * enable WBS codec setting.
971177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     *
972177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     * @return true if successful
973177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     *         false if there was some error such as
974177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     *               there is no connected headset
975177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     * @hide
976177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     */
977177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth    public boolean enableWBS() {
978177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth        if (mService != null && isEnabled()) {
979177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth            try {
980177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth                return mService.enableWBS();
981177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth            } catch (RemoteException e) {
982177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth                Log.e(TAG, e.toString());
983177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth            }
984177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth        } else {
985177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth            Log.w(TAG, "Proxy not attached to service");
986177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
987177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth        }
988177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth        return false;
989177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth    }
990177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth
991177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth    /**
992177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     * disable WBS codec settting. It set NBS codec.
993177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     *
994177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     * @return true if successful
995177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     *         false if there was some error such as
996177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     *               there is no connected headset
997177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     * @hide
998177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth     */
999177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth    public boolean disableWBS() {
1000177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth        if (mService != null && isEnabled()) {
1001177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth            try {
1002177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth                return mService.disableWBS();
1003177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth            } catch (RemoteException e) {
1004177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth                Log.e(TAG, e.toString());
1005177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth            }
1006177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth        } else {
1007177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth            Log.w(TAG, "Proxy not attached to service");
1008177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1009177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth        }
1010177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth        return false;
1011177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth    }
1012177d078d90f9659cfc7f091d1fdeb5712b357123Mudumba Ananth
10130d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth    /**
101476cd53a953db78efe40b435457f51dd83150216aJack He     * check if in-band ringing is supported for this platform.
101576cd53a953db78efe40b435457f51dd83150216aJack He     *
101676cd53a953db78efe40b435457f51dd83150216aJack He     * @return true if in-band ringing is supported
101776cd53a953db78efe40b435457f51dd83150216aJack He     *         false if in-band ringing is not supported
101876cd53a953db78efe40b435457f51dd83150216aJack He     * @hide
101976cd53a953db78efe40b435457f51dd83150216aJack He     */
102076cd53a953db78efe40b435457f51dd83150216aJack He    public static boolean isInbandRingingSupported(Context context) {
102176cd53a953db78efe40b435457f51dd83150216aJack He        return context.getResources().getBoolean(
102276cd53a953db78efe40b435457f51dd83150216aJack He                com.android.internal.R.bool.config_bluetooth_hfp_inband_ringing_support);
102376cd53a953db78efe40b435457f51dd83150216aJack He    }
102476cd53a953db78efe40b435457f51dd83150216aJack He
102576cd53a953db78efe40b435457f51dd83150216aJack He    /**
10260d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * Send Headset the BIND response from AG to report change in the status of the
10270d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * HF indicators to the headset
10280d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     *
10290d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * @param ind_id Assigned Number of the indicator (defined by SIG)
10300d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * @param ind_status
10310d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * possible values- false-Indicator is disabled, no value changes shall be sent for this indicator
10320d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     *                  true-Indicator is enabled, value changes may be sent for this indicator
10330d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     * @hide
10340d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth     */
10350d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth    public void bindResponse(int ind_id, boolean ind_status) {
10360d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth        if (mService != null && isEnabled()) {
10370d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth            try {
10380d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth                mService.bindResponse(ind_id, ind_status);
10390d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth            } catch (RemoteException e) {
10400d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth                Log.e(TAG, e.toString());
10410d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth            }
10420d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth        } else {
10430d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth            Log.w(TAG, "Proxy not attached to service");
10440d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth            if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
10450d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth        }
10460d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth    }
10470d98ebf997b24e96d0328b4195a22832f9ae2664Mudumba Ananth
1048e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz    private final IBluetoothProfileServiceConnection mConnection
1049e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz            = new IBluetoothProfileServiceConnection.Stub()  {
1050e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        @Override
10519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onServiceConnected(ComponentName className, IBinder service) {
10529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (DBG) Log.d(TAG, "Proxy object connected");
10530a17db1cc5942ea000ca87bb72853de57a15ec64Jeff Sharkey            mService = IBluetoothHeadset.Stub.asInterface(Binder.allowBlocking(service));
1054e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz            mHandler.sendMessage(mHandler.obtainMessage(
1055e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    MESSAGE_HEADSET_SERVICE_CONNECTED));
10569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1057e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        @Override
10589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void onServiceDisconnected(ComponentName className) {
10599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (DBG) Log.d(TAG, "Proxy object disconnected");
10609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mService = null;
1061e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz            mHandler.sendMessage(mHandler.obtainMessage(
1062e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    MESSAGE_HEADSET_SERVICE_DISCONNECTED));
10639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
1065f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project
106662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private boolean isEnabled() {
106762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true;
106862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       return false;
106962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    }
107062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
10716f7a9736602ba1f1dc5a16542aa947861a520ec5Jaikumar Ganesh    private boolean isDisabled() {
10726f7a9736602ba1f1dc5a16542aa947861a520ec5Jaikumar Ganesh       if (mAdapter.getState() == BluetoothAdapter.STATE_OFF) return true;
10736f7a9736602ba1f1dc5a16542aa947861a520ec5Jaikumar Ganesh       return false;
10746f7a9736602ba1f1dc5a16542aa947861a520ec5Jaikumar Ganesh    }
10756f7a9736602ba1f1dc5a16542aa947861a520ec5Jaikumar Ganesh
107662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private boolean isValidDevice(BluetoothDevice device) {
107762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (device == null) return false;
107862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
107962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
108062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       return false;
108162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    }
108262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
1083f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project    private static void log(String msg) {
1084f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project        Log.d(TAG, msg);
1085f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project    }
1086e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz
1087e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz    private final Handler mHandler = new Handler(Looper.getMainLooper()) {
1088e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        @Override
1089e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        public void handleMessage(Message msg) {
1090e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz            switch (msg.what) {
1091e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                case MESSAGE_HEADSET_SERVICE_CONNECTED: {
1092e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    if (mServiceListener != null) {
1093e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                        mServiceListener.onServiceConnected(BluetoothProfile.HEADSET,
1094e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                                BluetoothHeadset.this);
1095e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    }
1096e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    break;
1097e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                }
1098e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                case MESSAGE_HEADSET_SERVICE_DISCONNECTED: {
1099e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    if (mServiceListener != null) {
1100e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                        mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET);
1101e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    }
1102e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                    break;
1103e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz                }
1104e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz            }
1105e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz        }
1106e8b98925d08f720c4d22b626d0650de536840a9aBenjamin Franz    };
11079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1108