BluetoothA2dp.java revision f8789167e903b637b1dbe8f710e7c66c4cfd74fd
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; 219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context; 229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.IBinder; 2341d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganeshimport android.os.ParcelUuid; 2462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganeshimport android.os.RemoteException; 2562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganeshimport android.os.ServiceManager; 2662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganeshimport android.server.BluetoothA2dpService; 279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log; 289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2903cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganeshimport java.util.ArrayList; 3003cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganeshimport java.util.List; 3162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh 329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/** 3462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * This class provides the public APIs to control the Bluetooth A2DP 3562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * profile. 369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 3762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh *<p>BluetoothA2dp is a proxy object for controlling the Bluetooth A2DP 3862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get 3962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * the BluetoothA2dp proxy object. 409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 4162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p> Android only supports one connected Bluetooth A2dp device at a time. 4262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Each method is protected with its appropriate permission. 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 4462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganeshpublic final class BluetoothA2dp implements BluetoothProfile { 459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static final String TAG = "BluetoothA2dp"; 46f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project private static final boolean DBG = false; 479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 4962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Intent used to broadcast the change in connection state of the A2DP 5062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * profile. 5162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 5262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p>This intent will have 3 extras: 53c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * <ul> 54c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> 55c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> 56c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> 57c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * </ul> 5862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 590706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of 6062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, 6162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. 6262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 63c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to 64c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * receive. 6562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh */ 6662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 6762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public static final String ACTION_CONNECTION_STATE_CHANGED = 6862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED"; 699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 7062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 7162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Intent used to broadcast the change in the Playing state of the A2DP 7262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * profile. 7362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 7462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p>This intent will have 3 extras: 75c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * <ul> 76c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> 77c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li> 780706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> 79c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * </ul> 8062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 810706fed52075f7f2b25101a40287519ac18d3184Jaikumar Ganesh * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of 8262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@link #STATE_PLAYING}, {@link #STATE_NOT_PLAYING}, 8362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 84c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to 85c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * receive. 869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 8862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public static final String ACTION_PLAYING_STATE_CHANGED = 8962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh "android.bluetooth.a2dp.profile.action.PLAYING_STATE_CHANGED"; 909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 9162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 9262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * A2DP sink device is streaming music. This state can be one of 9362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of 9462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@link #ACTION_PLAYING_STATE_CHANGED} intent. 9562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh */ 9662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public static final int STATE_PLAYING = 10; 979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 9862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 9962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * A2DP sink device is NOT streaming music. This state can be one of 10062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of 10162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@link #ACTION_PLAYING_STATE_CHANGED} intent. 10262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh */ 10362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public static final int STATE_NOT_PLAYING = 11; 104bd022f423a33f0794bb53e5b0720da2d67e4631cNick Pelly 10562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh private ServiceListener mServiceListener; 10662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh private IBluetoothA2dp mService; 10762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh private BluetoothAdapter mAdapter; 1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Create a BluetoothA2dp proxy object for interacting with the local 1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Bluetooth A2DP service. 11262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 11462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /*package*/ BluetoothA2dp(Context mContext, ServiceListener l) { 1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project IBinder b = ServiceManager.getService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE); 11662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh mServiceListener = l; 11762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh mAdapter = BluetoothAdapter.getDefaultAdapter(); 118081f58abb7ceb702fdb0b65ff0e0ff70dd265a30Michael Chan if (b != null) { 119081f58abb7ceb702fdb0b65ff0e0ff70dd265a30Michael Chan mService = IBluetoothA2dp.Stub.asInterface(b); 12062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mServiceListener != null) { 12162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh mServiceListener.onServiceConnected(BluetoothProfile.A2DP, this); 12262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 123081f58abb7ceb702fdb0b65ff0e0ff70dd265a30Michael Chan } else { 124081f58abb7ceb702fdb0b65ff0e0ff70dd265a30Michael Chan Log.w(TAG, "Bluetooth A2DP service not available!"); 125081f58abb7ceb702fdb0b65ff0e0ff70dd265a30Michael Chan 126081f58abb7ceb702fdb0b65ff0e0ff70dd265a30Michael Chan // Instead of throwing an exception which prevents people from going 127081f58abb7ceb702fdb0b65ff0e0ff70dd265a30Michael Chan // into Wireless settings in the emulator. Let it crash later when it is actually used. 128081f58abb7ceb702fdb0b65ff0e0ff70dd265a30Michael Chan mService = null; 1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 13262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 133f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * Initiate connection to a profile of the remote bluetooth device. 134f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 135f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * <p> Currently, the system supports only 1 connection to the 136f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * A2DP profile. The API will automatically disconnect connected 137f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * devices before connecting. 138f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 139f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * <p> This API returns false in scenarios like the profile on the 140f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * device is already connected or Bluetooth is not turned on. 141f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * When this API returns true, it is guaranteed that 142f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * connection state intent for the profile will be broadcasted with 143f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * the state. Users can get the connection state of the profile 144f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * from this intent. 145f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 146f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 147f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * permission. 148f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 149f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * @param device Remote Bluetooth Device 150f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * @return false on immediate error, 151f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * true otherwise 15262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * @hide 1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 15462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public boolean connect(BluetoothDevice device) { 15562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (DBG) log("connect(" + device + ")"); 15662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService != null && isEnabled() && 15762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh isValidDevice(device)) { 15862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh try { 15962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return mService.connect(device); 16062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } catch (RemoteException e) { 16162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 16262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 16362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService == null) Log.w(TAG, "Proxy not attached to service"); 16662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 16962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 170f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * Initiate disconnection from a profile 171f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 172f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * <p> This API will return false in scenarios like the profile on the 173f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * Bluetooth device is not in connected state etc. When this API returns, 174f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * true, it is guaranteed that the connection state change 175f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * intent will be broadcasted with the state. Users can get the 176f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * disconnection state of the profile from this intent. 177f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 178f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * <p> If the disconnection is initiated by a remote device, the state 179f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * will transition from {@link #STATE_CONNECTED} to 180f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the 181f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * host (local) device the state will transition from 182f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to 183f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * state {@link #STATE_DISCONNECTED}. The transition to 184f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * {@link #STATE_DISCONNECTING} can be used to distinguish between the 185f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * two scenarios. 186f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 187f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 188f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * permission. 189f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 190f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * @param device Remote Bluetooth Device 191f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * @return false on immediate error, 192f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * true otherwise 19362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * @hide 1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 19562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public boolean disconnect(BluetoothDevice device) { 19662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (DBG) log("disconnect(" + device + ")"); 19762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService != null && isEnabled() && 19862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh isValidDevice(device)) { 19962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh try { 20062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return mService.disconnect(device); 20162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } catch (RemoteException e) { 20262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 20362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 20462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 20662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService == null) Log.w(TAG, "Proxy not attached to service"); 20762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 21062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 21162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@inheritDoc} 212f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan */ 21303cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh public List<BluetoothDevice> getConnectedDevices() { 21462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (DBG) log("getConnectedDevices()"); 21562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService != null && isEnabled()) { 21662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh try { 21703cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh return mService.getConnectedDevices(); 21862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } catch (RemoteException e) { 21962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 22003cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh return new ArrayList<BluetoothDevice>(); 22162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 222f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan } 22362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService == null) Log.w(TAG, "Proxy not attached to service"); 22403cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh return new ArrayList<BluetoothDevice>(); 225f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan } 226f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan 22762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 22862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@inheritDoc} 229f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan */ 23003cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { 23162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (DBG) log("getDevicesMatchingStates()"); 23262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService != null && isEnabled()) { 23362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh try { 23403cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh return mService.getDevicesMatchingConnectionStates(states); 23562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } catch (RemoteException e) { 23662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 23703cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh return new ArrayList<BluetoothDevice>(); 23862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 239f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan } 24062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService == null) Log.w(TAG, "Proxy not attached to service"); 24103cd78cf5e51c3adb78d2e3d314838dcf3e36b26Jaikumar Ganesh return new ArrayList<BluetoothDevice>(); 242f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan } 243f9bbe1e71a502fe7bd1f4a23ba5bbe4dde0d9d57Zhu Lan 24462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 24562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * {@inheritDoc} 2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 24762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public int getConnectionState(BluetoothDevice device) { 24862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (DBG) log("getState(" + device + ")"); 24962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService != null && isEnabled() 25062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh && isValidDevice(device)) { 25162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh try { 25262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return mService.getConnectionState(device); 25362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } catch (RemoteException e) { 25462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 25562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return BluetoothProfile.STATE_DISCONNECTED; 25662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 25762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 25862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService == null) Log.w(TAG, "Proxy not attached to service"); 25962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return BluetoothProfile.STATE_DISCONNECTED; 2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 26262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 263f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * Set priority of the profile 264f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 265f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * <p> The device should already be paired. 266f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * Priority can be one of {@link #PRIORITY_ON} or 267f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * {@link #PRIORITY_OFF}, 268f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 269f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 270f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * permission. 271f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 272f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * @param device Paired bluetooth device 273f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * @param priority 274f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * @return true if priority is set, false on error 2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @hide 2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 27762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public boolean setPriority(BluetoothDevice device, int priority) { 27862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (DBG) log("setPriority(" + device + ", " + priority + ")"); 27962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService != null && isEnabled() 28062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh && isValidDevice(device)) { 28162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (priority != BluetoothProfile.PRIORITY_OFF && 28262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh priority != BluetoothProfile.PRIORITY_ON) { 28362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 28462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 28562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh try { 28662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return mService.setPriority(device, priority); 28762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } catch (RemoteException e) { 28862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 28962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 29062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 29262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService == null) Log.w(TAG, "Proxy not attached to service"); 29362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 29662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 297f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * Get the priority of the profile. 298f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 299f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * <p> The priority can be any of: 300f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, 301f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} 302f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 303f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. 304f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * 305f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * @param device Bluetooth device 306f8789167e903b637b1dbe8f710e7c66c4cfd74fdJaikumar Ganesh * @return priority of the device 307b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh * @hide 308b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh */ 30962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public int getPriority(BluetoothDevice device) { 31062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (DBG) log("getPriority(" + device + ")"); 31162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService != null && isEnabled() 31262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh && isValidDevice(device)) { 31362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh try { 31462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return mService.getPriority(device); 31562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } catch (RemoteException e) { 31662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 31762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return BluetoothProfile.PRIORITY_OFF; 31862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 319b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh } 32062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService == null) Log.w(TAG, "Proxy not attached to service"); 32162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return BluetoothProfile.PRIORITY_OFF; 322b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh } 323b16c4f7dd92dabf0cc27438a5d3d9ebd203a38b3Jaikumar Ganesh 32462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 32562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Check if A2DP profile is streaming music. 32662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 327c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. 32862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 32962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * @param device BluetoothDevice device 3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 33162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public boolean isA2dpPlaying(BluetoothDevice device) { 33262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService != null && isEnabled() 33362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh && isValidDevice(device)) { 33462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh try { 33562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return mService.isA2dpPlaying(device); 33662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } catch (RemoteException e) { 33762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 33862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 33962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 34162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService == null) Log.w(TAG, "Proxy not attached to service"); 34262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 34662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Initiate suspend from an A2DP sink. 34762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 34862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p> This API will return false in scenarios like the A2DP 34962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * device is not in connected state etc. When this API returns, 350c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * true, it is guaranteed that {@link #ACTION_CONNECTION_STATE_CHANGED} 35162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * intent will be broadcasted with the state. Users can get the 35262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * state of the A2DP device from this intent. 35362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 35462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 355c8fa4ff838a0c3d2c67db65540fa751e5abe27edJaikumar Ganesh * permission. 35662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 35762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * @param device Remote A2DP sink 35862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * @return false on immediate error, 35962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * true otherwise 36062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * @hide 3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 36262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public boolean suspendSink(BluetoothDevice device) { 36362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService != null && isEnabled() 36462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh && isValidDevice(device)) { 36562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh try { 36662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return mService.suspendSink(device); 36762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } catch (RemoteException e) { 36862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 36962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 37062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 37262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService == null) Log.w(TAG, "Proxy not attached to service"); 37362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 37762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Initiate resume from a suspended A2DP sink. 37862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 37962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p> This API will return false in scenarios like the A2DP 38062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * device is not in suspended state etc. When this API returns, 38162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * true, it is guaranteed that {@link #ACTION_SINK_STATE_CHANGED} 38262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * intent will be broadcasted with the state. Users can get the 38362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * state of the A2DP device from this intent. 38462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 38562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} 38662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 38762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * @param device Remote A2DP sink 38862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * @return false on immediate error, 38962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * true otherwise 39062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * @hide 3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 39262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh public boolean resumeSink(BluetoothDevice device) { 39362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService != null && isEnabled() 39462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh && isValidDevice(device)) { 39562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh try { 39662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return mService.resumeSink(device); 39762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } catch (RemoteException e) { 39862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); 39962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 40062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 40262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mService == null) Log.w(TAG, "Proxy not attached to service"); 40362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 40641d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh /** 40741d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh * This function checks if the remote device is an AVCRP 40841d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh * target and thus whether we should send volume keys 40941d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh * changes or not. 41041d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh * @hide 41141d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh */ 41241d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh public boolean shouldSendVolumeKeys(BluetoothDevice device) { 41341d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh if (isEnabled() && isValidDevice(device)) { 41441d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh ParcelUuid[] uuids = device.getUuids(); 41541d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh if (uuids == null) return false; 41641d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh 41741d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh for (ParcelUuid uuid: uuids) { 41841d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh if (BluetoothUuid.isAvrcpTarget(uuid)) { 41941d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh return true; 42041d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh } 42141d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh } 42241d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh } 42341d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh return false; 42441d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh } 42541d5c805d96aef0aaa9a2aaa86ccc4b77ca75e11Jaikumar Ganesh 42662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh /** 42762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * Helper for converting a state to a string. 42862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh * 4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * For debug use only - strings are not internationalized. 4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @hide 4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static String stateToString(int state) { 4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project switch (state) { 4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project case STATE_DISCONNECTED: 4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return "disconnected"; 4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project case STATE_CONNECTING: 4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return "connecting"; 4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project case STATE_CONNECTED: 4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return "connected"; 4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project case STATE_DISCONNECTING: 4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return "disconnecting"; 4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project case STATE_PLAYING: 4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return "playing"; 44462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh case STATE_NOT_PLAYING: 44562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return "not playing"; 4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project default: 4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return "<unknown state " + state + ">"; 4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 450f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project 45162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh private boolean isEnabled() { 45262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; 45362c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 45462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 45562c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh 45662c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh private boolean isValidDevice(BluetoothDevice device) { 45762c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (device == null) return false; 45862c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh 45962c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; 46062c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh return false; 46162c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh } 46262c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh 463f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project private static void log(String msg) { 46462c37efc9e894809b29a004c142a8e0a6b374db7Jaikumar Ganesh Log.d(TAG, msg); 465f5b4b98fada53d91c4c2ebeb5a1d33ccc95c94d2The Android Open Source Project } 4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 467