BluetoothA2dp.java revision d7d16b9f372116da1658f589df213aed33c2ded6
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
192d49752ee050ab7f1cd848933f6c62a73707e2d9Tor Norbyeimport android.Manifest;
202d49752ee050ab7f1cd848933f6c62a73707e2d9Tor Norbyeimport android.annotation.RequiresPermission;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.annotation.SdkConstant;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.annotation.SdkConstant.SdkConstantType;
233e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xieimport android.content.ComponentName;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
253e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xieimport android.content.Intent;
263e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xieimport android.content.ServiceConnection;
274197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErikimport android.media.AudioManager;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.IBinder;
2941d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganeshimport android.os.ParcelUuid;
3062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganeshimport android.os.RemoteException;
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
33d7d16b9f372116da1658f589df213aed33c2ded6Calvin Onimport com.android.internal.annotations.GuardedBy;
34d7d16b9f372116da1658f589df213aed33c2ded6Calvin On
3503cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganeshimport java.util.ArrayList;
3603cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganeshimport java.util.List;
37d7d16b9f372116da1658f589df213aed33c2ded6Calvin Onimport java.util.concurrent.locks.ReentrantReadWriteLock;
3862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
4162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * This class provides the public APIs to control the Bluetooth A2DP
4262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * profile.
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
4462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh *<p>BluetoothA2dp is a proxy object for controlling the Bluetooth A2DP
4562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get
4662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * the BluetoothA2dp proxy object.
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
4862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p> Android only supports one connected Bluetooth A2dp device at a time.
4962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Each method is protected with its appropriate permission.
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
5162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganeshpublic final class BluetoothA2dp implements BluetoothProfile {
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final String TAG = "BluetoothA2dp";
533e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    private static final boolean DBG = true;
54563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie    private static final boolean VDBG = false;
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
5762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Intent used to broadcast the change in connection state of the A2DP
5862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * profile.
5962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
6062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * <p>This intent will have 3 extras:
61c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <ul>
62c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
63c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li>
64c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
65c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * </ul>
6662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
670706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
6862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
6962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
7062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
71c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
72c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * receive.
7362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     */
7462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
7562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public static final String ACTION_CONNECTION_STATE_CHANGED =
7662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED";
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
7962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Intent used to broadcast the change in the Playing state of the A2DP
8062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * profile.
8162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
8262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * <p>This intent will have 3 extras:
83c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <ul>
84c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
85c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     *   <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li>
860706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     *   <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
87c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * </ul>
8862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
890706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh     * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
9062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #STATE_PLAYING}, {@link #STATE_NOT_PLAYING},
9162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
92c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
93c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * receive.
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
9662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public static final String ACTION_PLAYING_STATE_CHANGED =
9762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        "android.bluetooth.a2dp.profile.action.PLAYING_STATE_CHANGED";
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
993b76a4b2b62e9539e2629dc569d7e9a934e65dfbHemant Gupta    /** @hide */
1003b76a4b2b62e9539e2629dc569d7e9a934e65dfbHemant Gupta    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
1013b76a4b2b62e9539e2629dc569d7e9a934e65dfbHemant Gupta    public static final String ACTION_AVRCP_CONNECTION_STATE_CHANGED =
1023b76a4b2b62e9539e2629dc569d7e9a934e65dfbHemant Gupta        "android.bluetooth.a2dp.profile.action.AVRCP_CONNECTION_STATE_CHANGED";
1033b76a4b2b62e9539e2629dc569d7e9a934e65dfbHemant Gupta
10462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
10562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * A2DP sink device is streaming music. This state can be one of
10662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of
10762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #ACTION_PLAYING_STATE_CHANGED} intent.
10862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     */
10962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public static final int STATE_PLAYING   =  10;
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
11262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * A2DP sink device is NOT streaming music. This state can be one of
11362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of
11462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@link #ACTION_PLAYING_STATE_CHANGED} intent.
11562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     */
11662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public static final int STATE_NOT_PLAYING   =  11;
117bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly
1183e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    private Context mContext;
11962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private ServiceListener mServiceListener;
120d7d16b9f372116da1658f589df213aed33c2ded6Calvin On    private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock();
121d7d16b9f372116da1658f589df213aed33c2ded6Calvin On    @GuardedBy("mServiceLock") private IBluetoothA2dp mService;
12262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private BluetoothAdapter mAdapter;
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1240f42037eb7b5118015c2caca635538324ccf0ccffredc    final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
1250f42037eb7b5118015c2caca635538324ccf0ccffredc            new IBluetoothStateChangeCallback.Stub() {
1260f42037eb7b5118015c2caca635538324ccf0ccffredc                public void onBluetoothStateChange(boolean up) {
1270f42037eb7b5118015c2caca635538324ccf0ccffredc                    if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
1280f42037eb7b5118015c2caca635538324ccf0ccffredc                    if (!up) {
129d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                        if (VDBG) Log.d(TAG, "Unbinding service...");
130d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                        try {
131d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                            mServiceLock.writeLock().lock();
132d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                            mService = null;
133d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                            mContext.unbindService(mConnection);
134d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                        } catch (Exception re) {
135d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                            Log.e(TAG, "", re);
136d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                        } finally {
137d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                            mServiceLock.writeLock().unlock();
1380f42037eb7b5118015c2caca635538324ccf0ccffredc                        }
1390f42037eb7b5118015c2caca635538324ccf0ccffredc                    } else {
140d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                        try {
141d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                            mServiceLock.readLock().lock();
142d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                            if (mService == null) {
143d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                                if (VDBG) Log.d(TAG,"Binding service...");
144d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                                doBind();
1450f42037eb7b5118015c2caca635538324ccf0ccffredc                            }
146d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                        } catch (Exception re) {
147d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                            Log.e(TAG,"",re);
148d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                        } finally {
149d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                            mServiceLock.readLock().unlock();
1500f42037eb7b5118015c2caca635538324ccf0ccffredc                        }
1510f42037eb7b5118015c2caca635538324ccf0ccffredc                    }
1520f42037eb7b5118015c2caca635538324ccf0ccffredc                }
1530f42037eb7b5118015c2caca635538324ccf0ccffredc        };
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Create a BluetoothA2dp proxy object for interacting with the local
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Bluetooth A2DP service.
15762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1593e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    /*package*/ BluetoothA2dp(Context context, ServiceListener l) {
1603e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        mContext = context;
16162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        mServiceListener = l;
16262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        mAdapter = BluetoothAdapter.getDefaultAdapter();
1630f42037eb7b5118015c2caca635538324ccf0ccffredc        IBluetoothManager mgr = mAdapter.getBluetoothManager();
1640f42037eb7b5118015c2caca635538324ccf0ccffredc        if (mgr != null) {
1650f42037eb7b5118015c2caca635538324ccf0ccffredc            try {
1660f42037eb7b5118015c2caca635538324ccf0ccffredc                mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
1670f42037eb7b5118015c2caca635538324ccf0ccffredc            } catch (RemoteException e) {
1680f42037eb7b5118015c2caca635538324ccf0ccffredc                Log.e(TAG,"",e);
1690f42037eb7b5118015c2caca635538324ccf0ccffredc            }
1700f42037eb7b5118015c2caca635538324ccf0ccffredc        }
1710f42037eb7b5118015c2caca635538324ccf0ccffredc
172221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        doBind();
173221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn    }
174221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn
175221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn    boolean doBind() {
176221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        Intent intent = new Intent(IBluetoothA2dp.class.getName());
177221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
178221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        intent.setComponent(comp);
179466ce96da8ca7ea8c97e716b02a7d55007179aa1Dianne Hackborn        if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
180466ce96da8ca7ea8c97e716b02a7d55007179aa1Dianne Hackborn                android.os.Process.myUserHandle())) {
181221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn            Log.e(TAG, "Could not bind to Bluetooth A2DP Service with " + intent);
182221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn            return false;
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
184221ea892dcc661bd07d6f36ff012edca2c48aed4Dianne Hackborn        return true;
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1879bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh    /*package*/ void close() {
1889bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh        mServiceListener = null;
1890f42037eb7b5118015c2caca635538324ccf0ccffredc        IBluetoothManager mgr = mAdapter.getBluetoothManager();
1900f42037eb7b5118015c2caca635538324ccf0ccffredc        if (mgr != null) {
1910f42037eb7b5118015c2caca635538324ccf0ccffredc            try {
1920f42037eb7b5118015c2caca635538324ccf0ccffredc                mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
1930f42037eb7b5118015c2caca635538324ccf0ccffredc            } catch (Exception e) {
1940f42037eb7b5118015c2caca635538324ccf0ccffredc                Log.e(TAG,"",e);
1950f42037eb7b5118015c2caca635538324ccf0ccffredc            }
1960f42037eb7b5118015c2caca635538324ccf0ccffredc        }
1970f42037eb7b5118015c2caca635538324ccf0ccffredc
198d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
199d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.writeLock().lock();
2000f42037eb7b5118015c2caca635538324ccf0ccffredc            if (mService != null) {
201d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                mService = null;
202d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                mContext.unbindService(mConnection);
2030f42037eb7b5118015c2caca635538324ccf0ccffredc            }
204d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (Exception re) {
205d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "", re);
206d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
207d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.writeLock().unlock();
2080f42037eb7b5118015c2caca635538324ccf0ccffredc        }
2099bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh    }
2109bb275197df8eb999eab4cdd0a2aff83c2bb2ef6Jaikumar Ganesh
2110f42037eb7b5118015c2caca635538324ccf0ccffredc    public void finalize() {
2122d2d8c28545c687dbb105006ef4554eac8480313Mathias Jeppsson        // The empty finalize needs to be kept or the
2132d2d8c28545c687dbb105006ef4554eac8480313Mathias Jeppsson        // cts signature tests would fail.
2140f42037eb7b5118015c2caca635538324ccf0ccffredc    }
21562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
216f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Initiate connection to a profile of the remote bluetooth device.
217f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
218f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> Currently, the system supports only 1 connection to the
219f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * A2DP profile. The API will automatically disconnect connected
220f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * devices before connecting.
221f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
222f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> This API returns false in scenarios like the profile on the
223f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * device is already connected or Bluetooth is not turned on.
224f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * When this API returns true, it is guaranteed that
225f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * connection state intent for the profile will be broadcasted with
226f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * the state. Users can get the connection state of the profile
227f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * from this intent.
228f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
229f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
230f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
231f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
232f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Remote Bluetooth Device
233f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return false on immediate error,
234f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *               true otherwise
23562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
23762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean connect(BluetoothDevice device) {
23862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("connect(" + device + ")");
239d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
240d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
241d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled() &&
242d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                isValidDevice(device)) {
24362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.connect(device);
24462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
245d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
246d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return false;
247d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
248d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
249d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return false;
250d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
251d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
25562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
256f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Initiate disconnection from a profile
257f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
258f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> This API will return false in scenarios like the profile on the
259f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Bluetooth device is not in connected state etc. When this API returns,
260f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * true, it is guaranteed that the connection state change
261f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * intent will be broadcasted with the state. Users can get the
262f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * disconnection state of the profile from this intent.
263f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
264f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> If the disconnection is initiated by a remote device, the state
265f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * will transition from {@link #STATE_CONNECTED} to
266f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the
267f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * host (local) device the state will transition from
268f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to
269f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * state {@link #STATE_DISCONNECTED}. The transition to
270f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #STATE_DISCONNECTING} can be used to distinguish between the
271f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * two scenarios.
272f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
273f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
274f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
275f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
276f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Remote Bluetooth Device
277f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return false on immediate error,
278f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *               true otherwise
27962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @hide
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
28162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean disconnect(BluetoothDevice device) {
28262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("disconnect(" + device + ")");
283d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
284d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
285d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled() &&
286d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                isValidDevice(device)) {
28762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.disconnect(device);
28862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
289d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
290d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return false;
291d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
292d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
293d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return false;
294d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
295d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
29962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
30062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
301f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan     */
30203cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh    public List<BluetoothDevice> getConnectedDevices() {
303563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getConnectedDevices()");
304d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
305d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
306d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled()) {
30703cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return mService.getConnectedDevices();
30862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
309d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
310d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return new ArrayList<BluetoothDevice>();
311d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
312d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
313d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return new ArrayList<BluetoothDevice>();
314d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
315d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
316f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan        }
317f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan    }
318f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan
31962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
32062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
321f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan     */
32203cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh    public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
323563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getDevicesMatchingStates()");
324d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
325d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
326d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled()) {
32703cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh                return mService.getDevicesMatchingConnectionStates(states);
32862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
329d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
330d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return new ArrayList<BluetoothDevice>();
331d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
332d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
333d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return new ArrayList<BluetoothDevice>();
334d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
335d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
336f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan        }
337f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan    }
338f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan
33962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
34062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * {@inheritDoc}
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
34262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public int getConnectionState(BluetoothDevice device) {
343563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getState(" + device + ")");
344d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
345d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
346d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled()
347d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                && isValidDevice(device)) {
34862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.getConnectionState(device);
34962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
350d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
351d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return BluetoothProfile.STATE_DISCONNECTED;
352d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
353d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
354d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return BluetoothProfile.STATE_DISCONNECTED;
355d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
356d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
35762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        }
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
36062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
361f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Set priority of the profile
362f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
363f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> The device should already be paired.
3640f42037eb7b5118015c2caca635538324ccf0ccffredc     *  Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager
365f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_OFF},
366f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
367f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
368f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * permission.
369f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
370f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Paired bluetooth device
371f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param priority
372f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return true if priority is set, false on error
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
37562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean setPriority(BluetoothDevice device, int priority) {
37662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        if (DBG) log("setPriority(" + device + ", " + priority + ")");
377d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
378d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
379d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled()
380d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                && isValidDevice(device)) {
381d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                if (priority != BluetoothProfile.PRIORITY_OFF &&
382d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                    priority != BluetoothProfile.PRIORITY_ON) {
383d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                    return false;
384d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                }
38562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.setPriority(device, priority);
38662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
387d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
388d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return false;
389d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
390d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
391d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return false;
392d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
393d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
39762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
398f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * Get the priority of the profile.
399f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
400f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p> The priority can be any of:
401f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF},
402f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}
403f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
404f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
405f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     *
406f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @param device Bluetooth device
407f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh     * @return priority of the device
408b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh     * @hide
409b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh     */
4102d49752ee050ab7f1cd848933f6c62a73707e2d9Tor Norbye    @RequiresPermission(Manifest.permission.BLUETOOTH)
41162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public int getPriority(BluetoothDevice device) {
412563e414784eb81e4ea4051667d5c8855b17f7534Matthew Xie        if (VDBG) log("getPriority(" + device + ")");
413d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
414d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
415d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled()
416d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                && isValidDevice(device)) {
41762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.getPriority(device);
41862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
419d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
420d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return BluetoothProfile.PRIORITY_OFF;
421d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
422d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
423d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return BluetoothProfile.PRIORITY_OFF;
424d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
425d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
426b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh        }
427b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh    }
428b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh
42962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    /**
4305a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * Checks if Avrcp device supports the absolute volume feature.
4315a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     *
4325a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @return true if device supports absolute volume
4335a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @hide
4345a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     */
4355a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    public boolean isAvrcpAbsoluteVolumeSupported() {
4365a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (DBG) Log.d(TAG, "isAvrcpAbsoluteVolumeSupported");
437d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
438d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
439d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled()) {
4405a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                return mService.isAvrcpAbsoluteVolumeSupported();
4415a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            }
442d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
443d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return false;
444d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
445d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Error talking to BT service in isAvrcpAbsoluteVolumeSupported()", e);
446d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return false;
447d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
448d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
4495a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        }
4505a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    }
4515a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du
4525a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    /**
4534197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * Tells remote device to adjust volume. Only if absolute volume is
4544197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * supported. Uses the following values:
4554197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <ul>
4564197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <li>{@link AudioManager#ADJUST_LOWER}</li>
4574197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <li>{@link AudioManager#ADJUST_RAISE}</li>
4584197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <li>{@link AudioManager#ADJUST_MUTE}</li>
4594197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <li>{@link AudioManager#ADJUST_UNMUTE}</li>
4604197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * </ul>
4615a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     *
4624197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @param direction One of the supported adjust values.
4635a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @hide
4645a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     */
4655a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    public void adjustAvrcpAbsoluteVolume(int direction) {
4665a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (DBG) Log.d(TAG, "adjustAvrcpAbsoluteVolume");
467d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
468d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
469d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled()) {
4705a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                mService.adjustAvrcpAbsoluteVolume(direction);
4715a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            }
472d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
473d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
474d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Error talking to BT service in adjustAvrcpAbsoluteVolume()", e);
475d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
476d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
4775a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        }
4785a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    }
4795a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du
4805a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    /**
4815a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * Tells remote device to set an absolute volume. Only if absolute volume is supported
4825a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     *
4835a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @param volume Absolute volume to be set on AVRCP side
4845a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @hide
4855a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     */
4865a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    public void setAvrcpAbsoluteVolume(int volume) {
4875a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        if (DBG) Log.d(TAG, "setAvrcpAbsoluteVolume");
488d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
489d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
490d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled()) {
4915a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du                mService.setAvrcpAbsoluteVolume(volume);
4925a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            }
493d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
494d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
495d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Error talking to BT service in setAvrcpAbsoluteVolume()", e);
496d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
497d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
4985a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        }
4995a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    }
5005a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du
5015a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    /**
50262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Check if A2DP profile is streaming music.
50362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
504c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh     * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
50562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
50662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * @param device BluetoothDevice device
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
50862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    public boolean isA2dpPlaying(BluetoothDevice device) {
509d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        try {
510d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().lock();
511d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService != null && isEnabled()
512d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                && isValidDevice(device)) {
51362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh                return mService.isA2dpPlaying(device);
51462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh            }
515d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            if (mService == null) Log.w(TAG, "Proxy not attached to service");
516d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return false;
517d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } catch (RemoteException e) {
518d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
519d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            return false;
520d7d16b9f372116da1658f589df213aed33c2ded6Calvin On        } finally {
521d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            mServiceLock.readLock().unlock();
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
52641d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * This function checks if the remote device is an AVCRP
52741d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * target and thus whether we should send volume keys
52841d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * changes or not.
52941d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     * @hide
53041d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh     */
53141d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh    public boolean shouldSendVolumeKeys(BluetoothDevice device) {
53241d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh        if (isEnabled() && isValidDevice(device)) {
53341d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            ParcelUuid[] uuids = device.getUuids();
53441d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            if (uuids == null) return false;
53541d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh
53641d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            for (ParcelUuid uuid: uuids) {
53741d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh                if (BluetoothUuid.isAvrcpTarget(uuid)) {
53841d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh                    return true;
53941d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh                }
54041d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh            }
54141d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh        }
54241d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh        return false;
54341d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh    }
54441d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh
545a0c680393f2dd03a937c598b2cb9abf98a58152cMatthew Xie    /**
54662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     * Helper for converting a state to a string.
54762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh     *
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * For debug use only - strings are not internationalized.
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static String stateToString(int state) {
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch (state) {
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_DISCONNECTED:
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "disconnected";
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_CONNECTING:
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "connecting";
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_CONNECTED:
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "connected";
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_DISCONNECTING:
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "disconnecting";
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case STATE_PLAYING:
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "playing";
56362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh        case STATE_NOT_PLAYING:
56462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh          return "not playing";
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return "<unknown state " + state + ">";
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
569f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project
5709b6939939901cb82bc6fca93aad3810a4936dfc6Matthew Xie    private final ServiceConnection mConnection = new ServiceConnection() {
5713e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        public void onServiceConnected(ComponentName className, IBinder service) {
5723e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (DBG) Log.d(TAG, "Proxy object connected");
573d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            try {
574d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                mServiceLock.writeLock().lock();
575d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                mService = IBluetoothA2dp.Stub.asInterface(service);
576d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            } finally {
577d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                mServiceLock.writeLock().unlock();
578d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            }
5793e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie
5803e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (mServiceListener != null) {
5813e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                mServiceListener.onServiceConnected(BluetoothProfile.A2DP, BluetoothA2dp.this);
5823e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
5833e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        }
5843e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        public void onServiceDisconnected(ComponentName className) {
5853e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (DBG) Log.d(TAG, "Proxy object disconnected");
586d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            try {
587d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                mServiceLock.writeLock().lock();
588d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                mService = null;
589d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            } finally {
590d7d16b9f372116da1658f589df213aed33c2ded6Calvin On                mServiceLock.writeLock().unlock();
591d7d16b9f372116da1658f589df213aed33c2ded6Calvin On            }
5923e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            if (mServiceListener != null) {
5933e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie                mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP);
5943e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie            }
5953e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie        }
5963e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie    };
5973e8c82edb1feafc796aa52efafedc13f794c4dcdMatthew Xie
59862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private boolean isEnabled() {
59962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true;
60062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       return false;
60162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    }
60262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
60362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    private boolean isValidDevice(BluetoothDevice device) {
60462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (device == null) return false;
60562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
60662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
60762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh       return false;
60862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh    }
60962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh
610f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project    private static void log(String msg) {
61162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh      Log.d(TAG, msg);
612f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project    }
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
614