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
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.annotation.SdkConstant;
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.annotation.SdkConstant.SdkConstantType;
213e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xieimport android.content.ComponentName;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
233e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xieimport android.content.Intent;
243e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xieimport android.content.ServiceConnection;
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.IBinder;
2641d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganeshimport android.os.ParcelUuid;
2762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganeshimport android.os.RemoteException;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3003cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganeshimport java.util.ArrayList;
3103cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganeshimport java.util.List;
3262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
3562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * This class provides the public APIs to control the Bluetooth A2DP
3662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * profile.
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
3862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh *<p>BluetoothA2dp is a proxy object for controlling the Bluetooth A2DP
3962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get
4062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * the BluetoothA2dp proxy object.
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
4262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p> Android only supports one connected Bluetooth A2dp device at a time.
4362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Each method is protected with its appropriate permission.
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
4562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganeshpublic final class BluetoothA2dp implements BluetoothProfile {
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final String TAG = "BluetoothA2dp";
473e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    private static final boolean DBG = true;
48563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie    private static final boolean VDBG = false;
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
5162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Intent used to broadcast the change in connection state of the A2DP
5262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * profile.
5362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
5462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * <p>This intent will have 3 extras:
55c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <ul>
56c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
57c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li>
58c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
59c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * </ul>
6062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
610706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
6262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
6362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
6462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
65c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
66c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * receive.
6762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     */
6862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
6962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public static final String ACTION_CONNECTION_STATE_CHANGED =
7062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED";
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
7362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Intent used to broadcast the change in the Playing state of the A2DP
7462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * profile.
7562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
7662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * <p>This intent will have 3 extras:
77c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <ul>
78c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
79c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li>
800706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     *   <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
81c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * </ul>
8262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
830706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
8462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #STATE_PLAYING}, {@link #STATE_NOT_PLAYING},
8562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
86c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
87c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * receive.
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
9062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public static final String ACTION_PLAYING_STATE_CHANGED =
9162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        "android.bluetooth.a2dp.profile.action.PLAYING_STATE_CHANGED";
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
9462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * A2DP sink device is streaming music. This state can be one of
9562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of
9662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #ACTION_PLAYING_STATE_CHANGED} intent.
9762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     */
9862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public static final int STATE_PLAYING   =  10;
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
10162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * A2DP sink device is NOT streaming music. This state can be one of
10262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of
10362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #ACTION_PLAYING_STATE_CHANGED} intent.
10462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     */
10562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public static final int STATE_NOT_PLAYING   =  11;
106bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
1073e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    private Context mContext;
10862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private ServiceListener mServiceListener;
10962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private IBluetoothA2dp mService;
11062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private BluetoothAdapter mAdapter;
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1120f42037eb7b5118015c2caca635538324ccf0ccffredc    final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
1130f42037eb7b5118015c2caca635538324ccf0ccffredc            new IBluetoothStateChangeCallback.Stub() {
1140f42037eb7b5118015c2caca635538324ccf0ccffredc                public void onBluetoothStateChange(boolean up) {
1150f42037eb7b5118015c2caca635538324ccf0ccffredc                    if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
1160f42037eb7b5118015c2caca635538324ccf0ccffredc                    if (!up) {
117563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie                        if (VDBG) Log.d(TAG,"Unbinding service...");
1180f42037eb7b5118015c2caca635538324ccf0ccffredc                        synchronized (mConnection) {
1190f42037eb7b5118015c2caca635538324ccf0ccffredc                            try {
1200f42037eb7b5118015c2caca635538324ccf0ccffredc                                mService = null;
1210f42037eb7b5118015c2caca635538324ccf0ccffredc                                mContext.unbindService(mConnection);
1220f42037eb7b5118015c2caca635538324ccf0ccffredc                            } catch (Exception re) {
1230f42037eb7b5118015c2caca635538324ccf0ccffredc                                Log.e(TAG,"",re);
1240f42037eb7b5118015c2caca635538324ccf0ccffredc                            }
1250f42037eb7b5118015c2caca635538324ccf0ccffredc                        }
1260f42037eb7b5118015c2caca635538324ccf0ccffredc                    } else {
1270f42037eb7b5118015c2caca635538324ccf0ccffredc                        synchronized (mConnection) {
1280f42037eb7b5118015c2caca635538324ccf0ccffredc                            try {
1290f42037eb7b5118015c2caca635538324ccf0ccffredc                                if (mService == null) {
130563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie                                    if (VDBG) Log.d(TAG,"Binding service...");
1310f42037eb7b5118015c2caca635538324ccf0ccffredc                                    if (!mContext.bindService(new Intent(IBluetoothA2dp.class.getName()), mConnection, 0)) {
1320f42037eb7b5118015c2caca635538324ccf0ccffredc                                        Log.e(TAG, "Could not bind to Bluetooth A2DP Service");
1330f42037eb7b5118015c2caca635538324ccf0ccffredc                                    }
1340f42037eb7b5118015c2caca635538324ccf0ccffredc                                }
1350f42037eb7b5118015c2caca635538324ccf0ccffredc                            } catch (Exception re) {
1360f42037eb7b5118015c2caca635538324ccf0ccffredc                                Log.e(TAG,"",re);
1370f42037eb7b5118015c2caca635538324ccf0ccffredc                            }
1380f42037eb7b5118015c2caca635538324ccf0ccffredc                        }
1390f42037eb7b5118015c2caca635538324ccf0ccffredc                    }
1400f42037eb7b5118015c2caca635538324ccf0ccffredc                }
1410f42037eb7b5118015c2caca635538324ccf0ccffredc        };
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Create a BluetoothA2dp proxy object for interacting with the local
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Bluetooth A2DP service.
14562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1473e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    /*package*/ BluetoothA2dp(Context context, ServiceListener l) {
1483e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        mContext = context;
14962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        mServiceListener = l;
15062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        mAdapter = BluetoothAdapter.getDefaultAdapter();
1510f42037eb7b5118015c2caca635538324ccf0ccffredc        IBluetoothManager mgr = mAdapter.getBluetoothManager();
1520f42037eb7b5118015c2caca635538324ccf0ccffredc        if (mgr != null) {
1530f42037eb7b5118015c2caca635538324ccf0ccffredc            try {
1540f42037eb7b5118015c2caca635538324ccf0ccffredc                mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
1550f42037eb7b5118015c2caca635538324ccf0ccffredc            } catch (RemoteException e) {
1560f42037eb7b5118015c2caca635538324ccf0ccffredc                Log.e(TAG,"",e);
1570f42037eb7b5118015c2caca635538324ccf0ccffredc            }
1580f42037eb7b5118015c2caca635538324ccf0ccffredc        }
1590f42037eb7b5118015c2caca635538324ccf0ccffredc
1603e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        if (!context.bindService(new Intent(IBluetoothA2dp.class.getName()), mConnection, 0)) {
1613e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            Log.e(TAG, "Could not bind to Bluetooth A2DP Service");
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1659bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh    /*package*/ void close() {
1669bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh        mServiceListener = null;
1670f42037eb7b5118015c2caca635538324ccf0ccffredc        IBluetoothManager mgr = mAdapter.getBluetoothManager();
1680f42037eb7b5118015c2caca635538324ccf0ccffredc        if (mgr != null) {
1690f42037eb7b5118015c2caca635538324ccf0ccffredc            try {
1700f42037eb7b5118015c2caca635538324ccf0ccffredc                mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
1710f42037eb7b5118015c2caca635538324ccf0ccffredc            } catch (Exception e) {
1720f42037eb7b5118015c2caca635538324ccf0ccffredc                Log.e(TAG,"",e);
1730f42037eb7b5118015c2caca635538324ccf0ccffredc            }
1740f42037eb7b5118015c2caca635538324ccf0ccffredc        }
1750f42037eb7b5118015c2caca635538324ccf0ccffredc
1760f42037eb7b5118015c2caca635538324ccf0ccffredc        synchronized (mConnection) {
1770f42037eb7b5118015c2caca635538324ccf0ccffredc            if (mService != null) {
1780f42037eb7b5118015c2caca635538324ccf0ccffredc                try {
1790f42037eb7b5118015c2caca635538324ccf0ccffredc                    mService = null;
1800f42037eb7b5118015c2caca635538324ccf0ccffredc                    mContext.unbindService(mConnection);
1810f42037eb7b5118015c2caca635538324ccf0ccffredc                } catch (Exception re) {
1820f42037eb7b5118015c2caca635538324ccf0ccffredc                    Log.e(TAG,"",re);
1830f42037eb7b5118015c2caca635538324ccf0ccffredc                }
1840f42037eb7b5118015c2caca635538324ccf0ccffredc            }
1850f42037eb7b5118015c2caca635538324ccf0ccffredc        }
1869bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh    }
1879bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh
1880f42037eb7b5118015c2caca635538324ccf0ccffredc    public void finalize() {
1890f42037eb7b5118015c2caca635538324ccf0ccffredc        close();
1900f42037eb7b5118015c2caca635538324ccf0ccffredc    }
19162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
192f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Initiate connection to a profile of the remote bluetooth device.
193f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
194f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> Currently, the system supports only 1 connection to the
195f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * A2DP profile. The API will automatically disconnect connected
196f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * devices before connecting.
197f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
198f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> This API returns false in scenarios like the profile on the
199f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * device is already connected or Bluetooth is not turned on.
200f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * When this API returns true, it is guaranteed that
201f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * connection state intent for the profile will be broadcasted with
202f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * the state. Users can get the connection state of the profile
203f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * from this intent.
204f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
205f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
206f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
207f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
208f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Remote Bluetooth Device
209f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return false on immediate error,
210f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *               true otherwise
21162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
21362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean connect(BluetoothDevice device) {
21462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("connect(" + device + ")");
21562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
21662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
21762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
21862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.connect(device);
21962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
22062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
22162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return false;
22262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
22462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
22562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
22862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
229f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Initiate disconnection from a profile
230f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
231f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> This API will return false in scenarios like the profile on the
232f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Bluetooth device is not in connected state etc. When this API returns,
233f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * true, it is guaranteed that the connection state change
234f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * intent will be broadcasted with the state. Users can get the
235f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * disconnection state of the profile from this intent.
236f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
237f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> If the disconnection is initiated by a remote device, the state
238f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * will transition from {@link #STATE_CONNECTED} to
239f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the
240f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * host (local) device the state will transition from
241f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to
242f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * state {@link #STATE_DISCONNECTED}. The transition to
243f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_DISCONNECTING} can be used to distinguish between the
244f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * two scenarios.
245f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
246f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
247f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
248f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
249f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Remote Bluetooth Device
250f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return false on immediate error,
251f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *               true otherwise
25262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
25462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean disconnect(BluetoothDevice device) {
25562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("disconnect(" + device + ")");
25662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
25762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
25862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
25962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.disconnect(device);
26062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
26162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
26262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return false;
26362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
26562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
26662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
26962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
27062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
271f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan     */
27203cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh    public List<BluetoothDevice> getConnectedDevices() {
273563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getConnectedDevices()");
27462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()) {
27562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
27603cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return mService.getConnectedDevices();
27762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
27862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
27903cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return new ArrayList<BluetoothDevice>();
28062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
281f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan        }
28262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
28303cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh        return new ArrayList<BluetoothDevice>();
284f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan    }
285f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan
28662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
28762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
288f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan     */
28903cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh    public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
290563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getDevicesMatchingStates()");
29162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()) {
29262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
29303cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return mService.getDevicesMatchingConnectionStates(states);
29462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
29562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
29603cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return new ArrayList<BluetoothDevice>();
29762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
298f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan        }
29962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
30003cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh        return new ArrayList<BluetoothDevice>();
301f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan    }
302f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan
30362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
30462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
30662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public int getConnectionState(BluetoothDevice device) {
307563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getState(" + device + ")");
30862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()
30962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            && isValidDevice(device)) {
31062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
31162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.getConnectionState(device);
31262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
31362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
31462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return BluetoothProfile.STATE_DISCONNECTED;
31562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
31662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        }
31762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
31862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return BluetoothProfile.STATE_DISCONNECTED;
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
32162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
322f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Set priority of the profile
323f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
324f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> The device should already be paired.
3250f42037eb7b5118015c2caca635538324ccf0ccffredc     *  Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager
326f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_OFF},
327f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
328f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
329f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
330f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
331f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Paired bluetooth device
332f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param priority
333f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return true if priority is set, false on error
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
33662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean setPriority(BluetoothDevice device, int priority) {
33762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("setPriority(" + device + ", " + priority + ")");
33862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()
33962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            && isValidDevice(device)) {
34062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            if (priority != BluetoothProfile.PRIORITY_OFF &&
3416f6c54519a9b15379bb79e195655b6bd4fc5967aGanesh Ganapathi Batta                priority != BluetoothProfile.PRIORITY_ON){
34262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh              return false;
34362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
34462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
34562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.setPriority(device, priority);
34662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
34762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
34862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return false;
34962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
35162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
35262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
35562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
356f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Get the priority of the profile.
357f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
358f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> The priority can be any of:
359f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF},
360f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}
361f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
362f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
363f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
364f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Bluetooth device
365f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return priority of the device
366b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh     * @hide
367b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh     */
36862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public int getPriority(BluetoothDevice device) {
369563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getPriority(" + device + ")");
37062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()
37162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            && isValidDevice(device)) {
37262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
37362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.getPriority(device);
37462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
37562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
37662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return BluetoothProfile.PRIORITY_OFF;
37762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
378b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh        }
37962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
38062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return BluetoothProfile.PRIORITY_OFF;
381b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh    }
382b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh
38362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
38462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Check if A2DP profile is streaming music.
38562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
386c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
38762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
38862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @param device BluetoothDevice device
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
39062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean isA2dpPlaying(BluetoothDevice device) {
39162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()
39262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            && isValidDevice(device)) {
39362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
39462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.isA2dpPlaying(device);
39562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
39662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
39762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return false;
39862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
40062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
40162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
40541d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * This function checks if the remote device is an AVCRP
40641d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * target and thus whether we should send volume keys
40741d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * changes or not.
40841d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * @hide
40941d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     */
41041d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh    public boolean shouldSendVolumeKeys(BluetoothDevice device) {
41141d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh        if (isEnabled() && isValidDevice(device)) {
41241d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            ParcelUuid[] uuids = device.getUuids();
41341d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            if (uuids == null) return false;
41441d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh
41541d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            for (ParcelUuid uuid: uuids) {
41641d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh                if (BluetoothUuid.isAvrcpTarget(uuid)) {
41741d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh                    return true;
41841d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh                }
41941d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            }
42041d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh        }
42141d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh        return false;
42241d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh    }
42341d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh
424a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    /**
42562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Helper for converting a state to a string.
42662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * For debug use only - strings are not internationalized.
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static String stateToString(int state) {
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch (state) {
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_DISCONNECTED:
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "disconnected";
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_CONNECTING:
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "connecting";
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_CONNECTED:
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "connected";
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_DISCONNECTING:
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "disconnecting";
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_PLAYING:
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "playing";
44262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        case STATE_NOT_PLAYING:
44362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh          return "not playing";
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "<unknown state " + state + ">";
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
448f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project
4493e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    private ServiceConnection mConnection = new ServiceConnection() {
4503e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        public void onServiceConnected(ComponentName className, IBinder service) {
4513e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (DBG) Log.d(TAG, "Proxy object connected");
4523e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            mService = IBluetoothA2dp.Stub.asInterface(service);
4533e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie
4543e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (mServiceListener != null) {
4553e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                mServiceListener.onServiceConnected(BluetoothProfile.A2DP, BluetoothA2dp.this);
4563e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
4573e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        }
4583e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        public void onServiceDisconnected(ComponentName className) {
4593e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (DBG) Log.d(TAG, "Proxy object disconnected");
4603e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            mService = null;
4613e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (mServiceListener != null) {
4623e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP);
4633e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
4643e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        }
4653e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    };
4663e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie
46762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private boolean isEnabled() {
46862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true;
46962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       return false;
47062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    }
47162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
47262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private boolean isValidDevice(BluetoothDevice device) {
47362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (device == null) return false;
47462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
47562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
47662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       return false;
47762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    }
47862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
479f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project    private static void log(String msg) {
48062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh      Log.d(TAG, msg);
481f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project    }
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
483