BluetoothA2dp.java revision 5a0cf7a27f3953a1266af48543ccd9024f4cd89f
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...");
131221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn                                    doBind();
1320f42037eb7b5118015c2caca635538324ccf0ccffredc                                }
1330f42037eb7b5118015c2caca635538324ccf0ccffredc                            } catch (Exception re) {
1340f42037eb7b5118015c2caca635538324ccf0ccffredc                                Log.e(TAG,"",re);
1350f42037eb7b5118015c2caca635538324ccf0ccffredc                            }
1360f42037eb7b5118015c2caca635538324ccf0ccffredc                        }
1370f42037eb7b5118015c2caca635538324ccf0ccffredc                    }
1380f42037eb7b5118015c2caca635538324ccf0ccffredc                }
1390f42037eb7b5118015c2caca635538324ccf0ccffredc        };
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Create a BluetoothA2dp proxy object for interacting with the local
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Bluetooth A2DP service.
14362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1453e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    /*package*/ BluetoothA2dp(Context context, ServiceListener l) {
1463e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        mContext = context;
14762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        mServiceListener = l;
14862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        mAdapter = BluetoothAdapter.getDefaultAdapter();
1490f42037eb7b5118015c2caca635538324ccf0ccffredc        IBluetoothManager mgr = mAdapter.getBluetoothManager();
1500f42037eb7b5118015c2caca635538324ccf0ccffredc        if (mgr != null) {
1510f42037eb7b5118015c2caca635538324ccf0ccffredc            try {
1520f42037eb7b5118015c2caca635538324ccf0ccffredc                mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
1530f42037eb7b5118015c2caca635538324ccf0ccffredc            } catch (RemoteException e) {
1540f42037eb7b5118015c2caca635538324ccf0ccffredc                Log.e(TAG,"",e);
1550f42037eb7b5118015c2caca635538324ccf0ccffredc            }
1560f42037eb7b5118015c2caca635538324ccf0ccffredc        }
1570f42037eb7b5118015c2caca635538324ccf0ccffredc
158221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        doBind();
159221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn    }
160221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn
161221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn    boolean doBind() {
162221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        Intent intent = new Intent(IBluetoothA2dp.class.getName());
163221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
164221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        intent.setComponent(comp);
165221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        if (comp == null || !mContext.bindService(intent, mConnection, 0)) {
166221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn            Log.e(TAG, "Could not bind to Bluetooth A2DP Service with " + intent);
167221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn            return false;
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
169221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        return true;
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1729bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh    /*package*/ void close() {
1739bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh        mServiceListener = null;
1740f42037eb7b5118015c2caca635538324ccf0ccffredc        IBluetoothManager mgr = mAdapter.getBluetoothManager();
1750f42037eb7b5118015c2caca635538324ccf0ccffredc        if (mgr != null) {
1760f42037eb7b5118015c2caca635538324ccf0ccffredc            try {
1770f42037eb7b5118015c2caca635538324ccf0ccffredc                mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
1780f42037eb7b5118015c2caca635538324ccf0ccffredc            } catch (Exception e) {
1790f42037eb7b5118015c2caca635538324ccf0ccffredc                Log.e(TAG,"",e);
1800f42037eb7b5118015c2caca635538324ccf0ccffredc            }
1810f42037eb7b5118015c2caca635538324ccf0ccffredc        }
1820f42037eb7b5118015c2caca635538324ccf0ccffredc
1830f42037eb7b5118015c2caca635538324ccf0ccffredc        synchronized (mConnection) {
1840f42037eb7b5118015c2caca635538324ccf0ccffredc            if (mService != null) {
1850f42037eb7b5118015c2caca635538324ccf0ccffredc                try {
1860f42037eb7b5118015c2caca635538324ccf0ccffredc                    mService = null;
1870f42037eb7b5118015c2caca635538324ccf0ccffredc                    mContext.unbindService(mConnection);
1880f42037eb7b5118015c2caca635538324ccf0ccffredc                } catch (Exception re) {
1890f42037eb7b5118015c2caca635538324ccf0ccffredc                    Log.e(TAG,"",re);
1900f42037eb7b5118015c2caca635538324ccf0ccffredc                }
1910f42037eb7b5118015c2caca635538324ccf0ccffredc            }
1920f42037eb7b5118015c2caca635538324ccf0ccffredc        }
1939bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh    }
1949bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh
1950f42037eb7b5118015c2caca635538324ccf0ccffredc    public void finalize() {
1960f42037eb7b5118015c2caca635538324ccf0ccffredc        close();
1970f42037eb7b5118015c2caca635538324ccf0ccffredc    }
19862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
199f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Initiate connection to a profile of the remote bluetooth device.
200f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
201f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> Currently, the system supports only 1 connection to the
202f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * A2DP profile. The API will automatically disconnect connected
203f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * devices before connecting.
204f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
205f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> This API returns false in scenarios like the profile on the
206f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * device is already connected or Bluetooth is not turned on.
207f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * When this API returns true, it is guaranteed that
208f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * connection state intent for the profile will be broadcasted with
209f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * the state. Users can get the connection state of the profile
210f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * from this intent.
211f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
212f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
213f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
214f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
215f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Remote Bluetooth Device
216f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return false on immediate error,
217f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *               true otherwise
21862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
22062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean connect(BluetoothDevice device) {
22162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("connect(" + device + ")");
22262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
22362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
22462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
22562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.connect(device);
22662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
22762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
22862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return false;
22962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
23162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
23262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
23562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
236f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Initiate disconnection from a profile
237f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
238f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> This API will return false in scenarios like the profile on the
239f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Bluetooth device is not in connected state etc. When this API returns,
240f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * true, it is guaranteed that the connection state change
241f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * intent will be broadcasted with the state. Users can get the
242f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * disconnection state of the profile from this intent.
243f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
244f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> If the disconnection is initiated by a remote device, the state
245f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * will transition from {@link #STATE_CONNECTED} to
246f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the
247f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * host (local) device the state will transition from
248f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to
249f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * state {@link #STATE_DISCONNECTED}. The transition to
250f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_DISCONNECTING} can be used to distinguish between the
251f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * two scenarios.
252f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
253f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
254f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
255f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
256f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Remote Bluetooth Device
257f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return false on immediate error,
258f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *               true otherwise
25962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
26162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean disconnect(BluetoothDevice device) {
26262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("disconnect(" + device + ")");
26362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled() &&
26462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            isValidDevice(device)) {
26562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
26662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.disconnect(device);
26762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
26862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
26962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return false;
27062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
27262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
27362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
27662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
27762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
278f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan     */
27903cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh    public List<BluetoothDevice> getConnectedDevices() {
280563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getConnectedDevices()");
28162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()) {
28262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
28303cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return mService.getConnectedDevices();
28462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
28562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
28603cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return new ArrayList<BluetoothDevice>();
28762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
288f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan        }
28962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
29003cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh        return new ArrayList<BluetoothDevice>();
291f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan    }
292f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan
29362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
29462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
295f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan     */
29603cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh    public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
297563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getDevicesMatchingStates()");
29862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()) {
29962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
30003cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return mService.getDevicesMatchingConnectionStates(states);
30162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
30262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
30303cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return new ArrayList<BluetoothDevice>();
30462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
305f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan        }
30662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
30703cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh        return new ArrayList<BluetoothDevice>();
308f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan    }
309f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan
31062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
31162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
31362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public int getConnectionState(BluetoothDevice device) {
314563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getState(" + device + ")");
31562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()
31662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            && isValidDevice(device)) {
31762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
31862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.getConnectionState(device);
31962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
32062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
32162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return BluetoothProfile.STATE_DISCONNECTED;
32262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
32362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        }
32462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
32562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return BluetoothProfile.STATE_DISCONNECTED;
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
32862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
329f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Set priority of the profile
330f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
331f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> The device should already be paired.
3320f42037eb7b5118015c2caca635538324ccf0ccffredc     *  Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager
333f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_OFF},
334f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
335f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
336f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
337f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
338f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Paired bluetooth device
339f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param priority
340f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return true if priority is set, false on error
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
34362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean setPriority(BluetoothDevice device, int priority) {
34462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("setPriority(" + device + ", " + priority + ")");
34562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()
34662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            && isValidDevice(device)) {
34762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            if (priority != BluetoothProfile.PRIORITY_OFF &&
3486f6c54519a9b15379bb79e195655b6bd4fc5967aGanesh Ganapathi Batta                priority != BluetoothProfile.PRIORITY_ON){
34962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh              return false;
35062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
35162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
35262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.setPriority(device, priority);
35362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
35462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
35562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return false;
35662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
35862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
35962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
36262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
363f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Get the priority of the profile.
364f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
365f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> The priority can be any of:
366f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF},
367f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}
368f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
369f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
370f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
371f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Bluetooth device
372f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return priority of the device
373b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh     * @hide
374b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh     */
37562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public int getPriority(BluetoothDevice device) {
376563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getPriority(" + device + ")");
37762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()
37862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            && isValidDevice(device)) {
37962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
38062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.getPriority(device);
38162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
38262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
38362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return BluetoothProfile.PRIORITY_OFF;
38462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
385b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh        }
38662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
38762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return BluetoothProfile.PRIORITY_OFF;
388b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh    }
389b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh
39062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
3915a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * Checks if Avrcp device supports the absolute volume feature.
3925a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     *
3935a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @return true if device supports absolute volume
3945a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @hide
3955a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     */
3965a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    public boolean isAvrcpAbsoluteVolumeSupported() {
3975a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (DBG) Log.d(TAG, "isAvrcpAbsoluteVolumeSupported");
3985a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (mService != null && isEnabled()) {
3995a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            try {
4005a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                return mService.isAvrcpAbsoluteVolumeSupported();
4015a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            } catch (RemoteException e) {
4025a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                Log.e(TAG, "Error talking to BT service in isAvrcpAbsoluteVolumeSupported()", e);
4035a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                return false;
4045a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            }
4055a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        }
4065a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (mService == null) Log.w(TAG, "Proxy not attached to service");
4075a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        return false;
4085a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    }
4095a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du
4105a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    /**
4115a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * Tells remote device to adjust volume. Only if absolute volume is supported.
4125a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     *
4135a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @param direction 1 to increase volume, or -1 to decrease volume
4145a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @hide
4155a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     */
4165a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    public void adjustAvrcpAbsoluteVolume(int direction) {
4175a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (DBG) Log.d(TAG, "adjustAvrcpAbsoluteVolume");
4185a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (mService != null && isEnabled()) {
4195a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            try {
4205a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                mService.adjustAvrcpAbsoluteVolume(direction);
4215a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                return;
4225a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            } catch (RemoteException e) {
4235a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                Log.e(TAG, "Error talking to BT service in adjustAvrcpAbsoluteVolume()", e);
4245a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                return;
4255a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            }
4265a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        }
4275a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (mService == null) Log.w(TAG, "Proxy not attached to service");
4285a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    }
4295a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du
4305a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    /**
4315a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * Tells remote device to set an absolute volume. Only if absolute volume is supported
4325a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     *
4335a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @param volume Absolute volume to be set on AVRCP side
4345a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @hide
4355a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     */
4365a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    public void setAvrcpAbsoluteVolume(int volume) {
4375a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (DBG) Log.d(TAG, "setAvrcpAbsoluteVolume");
4385a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (mService != null && isEnabled()) {
4395a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            try {
4405a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                mService.setAvrcpAbsoluteVolume(volume);
4415a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                return;
4425a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            } catch (RemoteException e) {
4435a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                Log.e(TAG, "Error talking to BT service in setAvrcpAbsoluteVolume()", e);
4445a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                return;
4455a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            }
4465a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        }
4475a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (mService == null) Log.w(TAG, "Proxy not attached to service");
4485a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    }
4495a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du
4505a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    /**
45162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Check if A2DP profile is streaming music.
45262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
453c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
45462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
45562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @param device BluetoothDevice device
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
45762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean isA2dpPlaying(BluetoothDevice device) {
45862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService != null && isEnabled()
45962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            && isValidDevice(device)) {
46062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            try {
46162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.isA2dpPlaying(device);
46262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            } catch (RemoteException e) {
46362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
46462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return false;
46562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
46762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (mService == null) Log.w(TAG, "Proxy not attached to service");
46862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        return false;
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
47241d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * This function checks if the remote device is an AVCRP
47341d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * target and thus whether we should send volume keys
47441d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * changes or not.
47541d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * @hide
47641d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     */
47741d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh    public boolean shouldSendVolumeKeys(BluetoothDevice device) {
47841d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh        if (isEnabled() && isValidDevice(device)) {
47941d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            ParcelUuid[] uuids = device.getUuids();
48041d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            if (uuids == null) return false;
48141d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh
48241d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            for (ParcelUuid uuid: uuids) {
48341d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh                if (BluetoothUuid.isAvrcpTarget(uuid)) {
48441d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh                    return true;
48541d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh                }
48641d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            }
48741d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh        }
48841d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh        return false;
48941d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh    }
49041d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh
491a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    /**
49262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Helper for converting a state to a string.
49362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * For debug use only - strings are not internationalized.
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static String stateToString(int state) {
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch (state) {
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_DISCONNECTED:
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "disconnected";
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_CONNECTING:
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "connecting";
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_CONNECTED:
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "connected";
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_DISCONNECTING:
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "disconnecting";
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_PLAYING:
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "playing";
50962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        case STATE_NOT_PLAYING:
51062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh          return "not playing";
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "<unknown state " + state + ">";
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
515f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project
5163e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    private ServiceConnection mConnection = new ServiceConnection() {
5173e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        public void onServiceConnected(ComponentName className, IBinder service) {
5183e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (DBG) Log.d(TAG, "Proxy object connected");
5193e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            mService = IBluetoothA2dp.Stub.asInterface(service);
5203e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie
5213e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (mServiceListener != null) {
5223e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                mServiceListener.onServiceConnected(BluetoothProfile.A2DP, BluetoothA2dp.this);
5233e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
5243e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        }
5253e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        public void onServiceDisconnected(ComponentName className) {
5263e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (DBG) Log.d(TAG, "Proxy object disconnected");
5273e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            mService = null;
5283e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (mServiceListener != null) {
5293e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP);
5303e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
5313e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        }
5323e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    };
5333e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie
53462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private boolean isEnabled() {
53562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true;
53662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       return false;
53762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    }
53862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
53962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private boolean isValidDevice(BluetoothDevice device) {
54062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (device == null) return false;
54162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
54262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
54362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       return false;
54462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    }
54562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
546f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project    private static void log(String msg) {
54762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh      Log.d(TAG, msg);
548f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project    }
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
550