The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Pavlin Radoslavov | 64a9435 | 2017-12-19 13:20:06 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 21 | import android.annotation.RequiresPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.annotation.SdkConstant; |
| 23 | import android.annotation.SdkConstant.SdkConstantType; |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 24 | import android.annotation.UnsupportedAppUsage; |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 25 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.Context; |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.content.ServiceConnection; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 29 | import android.os.Binder; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 30 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.IBinder; |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 32 | import android.os.ParcelUuid; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 33 | import android.os.RemoteException; |
Cheney Ni | 4b6d6c2 | 2018-08-21 12:17:42 +0800 | [diff] [blame] | 34 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 37 | import com.android.internal.annotations.GuardedBy; |
| 38 | |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
| 40 | import java.util.List; |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 41 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 45 | * This class provides the public APIs to control the Bluetooth A2DP |
| 46 | * profile. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 48 | * <p>BluetoothA2dp is a proxy object for controlling the Bluetooth A2DP |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 49 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 50 | * the BluetoothA2dp proxy object. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | * |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 52 | * <p> Android only supports one connected Bluetooth A2dp device at a time. |
| 53 | * Each method is protected with its appropriate permission. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | */ |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 55 | public final class BluetoothA2dp implements BluetoothProfile { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | private static final String TAG = "BluetoothA2dp"; |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 57 | private static final boolean DBG = true; |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 58 | private static final boolean VDBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Intent used to broadcast the change in connection state of the A2DP |
| 62 | * profile. |
| 63 | * |
| 64 | * <p>This intent will have 3 extras: |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 65 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 66 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 67 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 68 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 69 | * </ul> |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 70 | * |
Jaikumar Ganesh | 0706fed | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 71 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 72 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 73 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 74 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 75 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 76 | * receive. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | */ |
| 78 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 79 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 80 | "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Intent used to broadcast the change in the Playing state of the A2DP |
| 84 | * profile. |
| 85 | * |
| 86 | * <p>This intent will have 3 extras: |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 87 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 88 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 89 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li> |
| 90 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 91 | * </ul> |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 92 | * |
Jaikumar Ganesh | 0706fed | 2011-01-26 11:46:56 -0800 | [diff] [blame] | 93 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 94 | * {@link #STATE_PLAYING}, {@link #STATE_NOT_PLAYING}, |
| 95 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 96 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 97 | * receive. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 98 | */ |
| 99 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 100 | public static final String ACTION_PLAYING_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 101 | "android.bluetooth.a2dp.profile.action.PLAYING_STATE_CHANGED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
Hemant Gupta | 3b76a4b | 2014-02-14 19:53:31 +0530 | [diff] [blame] | 103 | /** @hide */ |
| 104 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 105 | public static final String ACTION_AVRCP_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 106 | "android.bluetooth.a2dp.profile.action.AVRCP_CONNECTION_STATE_CHANGED"; |
Hemant Gupta | 3b76a4b | 2014-02-14 19:53:31 +0530 | [diff] [blame] | 107 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 108 | /** |
Pavlin Radoslavov | 64a9435 | 2017-12-19 13:20:06 -0800 | [diff] [blame] | 109 | * Intent used to broadcast the selection of a connected device as active. |
| 110 | * |
| 111 | * <p>This intent will have one extra: |
| 112 | * <ul> |
| 113 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. It can |
| 114 | * be null if no device is active. </li> |
| 115 | * </ul> |
| 116 | * |
| 117 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 118 | * receive. |
| 119 | * |
| 120 | * @hide |
| 121 | */ |
| 122 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 123 | @UnsupportedAppUsage |
Pavlin Radoslavov | 64a9435 | 2017-12-19 13:20:06 -0800 | [diff] [blame] | 124 | public static final String ACTION_ACTIVE_DEVICE_CHANGED = |
| 125 | "android.bluetooth.a2dp.profile.action.ACTIVE_DEVICE_CHANGED"; |
| 126 | |
| 127 | /** |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 128 | * Intent used to broadcast the change in the Audio Codec state of the |
| 129 | * A2DP Source profile. |
| 130 | * |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 131 | * <p>This intent will have 2 extras: |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 132 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 133 | * <li> {@link BluetoothCodecStatus#EXTRA_CODEC_STATUS} - The codec status. </li> |
| 134 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device if the device is currently |
| 135 | * connected, otherwise it is not included.</li> |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 136 | * </ul> |
| 137 | * |
| 138 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 139 | * receive. |
| 140 | * |
| 141 | * @hide |
| 142 | */ |
| 143 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 144 | @UnsupportedAppUsage |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 145 | public static final String ACTION_CODEC_CONFIG_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 146 | "android.bluetooth.a2dp.profile.action.CODEC_CONFIG_CHANGED"; |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 147 | |
| 148 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 149 | * A2DP sink device is streaming music. This state can be one of |
| 150 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 151 | * {@link #ACTION_PLAYING_STATE_CHANGED} intent. |
| 152 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 153 | public static final int STATE_PLAYING = 10; |
Nick Pelly | bd022f4 | 2009-08-14 18:33:38 -0700 | [diff] [blame] | 154 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 155 | /** |
| 156 | * A2DP sink device is NOT streaming music. This state can be one of |
| 157 | * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of |
| 158 | * {@link #ACTION_PLAYING_STATE_CHANGED} intent. |
| 159 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 160 | public static final int STATE_NOT_PLAYING = 11; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 161 | |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 162 | /** |
| 163 | * We don't have a stored preference for whether or not the given A2DP sink device supports |
| 164 | * optional codecs. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 165 | * |
| 166 | * @hide |
| 167 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 168 | @UnsupportedAppUsage |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 169 | public static final int OPTIONAL_CODECS_SUPPORT_UNKNOWN = -1; |
| 170 | |
| 171 | /** |
| 172 | * The given A2DP sink device does not support optional codecs. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 173 | * |
| 174 | * @hide |
| 175 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 176 | @UnsupportedAppUsage |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 177 | public static final int OPTIONAL_CODECS_NOT_SUPPORTED = 0; |
| 178 | |
| 179 | /** |
| 180 | * The given A2DP sink device does support optional codecs. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 181 | * |
| 182 | * @hide |
| 183 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 184 | @UnsupportedAppUsage |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 185 | public static final int OPTIONAL_CODECS_SUPPORTED = 1; |
| 186 | |
| 187 | /** |
| 188 | * We don't have a stored preference for whether optional codecs should be enabled or disabled |
| 189 | * for the given A2DP device. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 190 | * |
| 191 | * @hide |
| 192 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 193 | @UnsupportedAppUsage |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 194 | public static final int OPTIONAL_CODECS_PREF_UNKNOWN = -1; |
| 195 | |
| 196 | /** |
| 197 | * Optional codecs should be disabled for the given A2DP device. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 198 | * |
| 199 | * @hide |
| 200 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 201 | @UnsupportedAppUsage |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 202 | public static final int OPTIONAL_CODECS_PREF_DISABLED = 0; |
| 203 | |
| 204 | /** |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 205 | * Optional codecs should be enabled for the given A2DP device. |
| 206 | * |
| 207 | * @hide |
| 208 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 209 | @UnsupportedAppUsage |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 210 | public static final int OPTIONAL_CODECS_PREF_ENABLED = 1; |
| 211 | |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 212 | private Context mContext; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 213 | private ServiceListener mServiceListener; |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 214 | private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 215 | @GuardedBy("mServiceLock") |
| 216 | private IBluetoothA2dp mService; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 217 | private BluetoothAdapter mAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 219 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 220 | new IBluetoothStateChangeCallback.Stub() { |
| 221 | public void onBluetoothStateChange(boolean up) { |
| 222 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 223 | if (!up) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 224 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
| 225 | try { |
| 226 | mServiceLock.writeLock().lock(); |
| 227 | mService = null; |
| 228 | mContext.unbindService(mConnection); |
| 229 | } catch (Exception re) { |
| 230 | Log.e(TAG, "", re); |
| 231 | } finally { |
| 232 | mServiceLock.writeLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 233 | } |
| 234 | } else { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 235 | try { |
| 236 | mServiceLock.readLock().lock(); |
| 237 | if (mService == null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 238 | if (VDBG) Log.d(TAG, "Binding service..."); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 239 | doBind(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 240 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 241 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 242 | Log.e(TAG, "", re); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 243 | } finally { |
| 244 | mServiceLock.readLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | /** |
| 251 | * Create a BluetoothA2dp proxy object for interacting with the local |
| 252 | * Bluetooth A2DP service. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | */ |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 254 | /*package*/ BluetoothA2dp(Context context, ServiceListener l) { |
| 255 | mContext = context; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 256 | mServiceListener = l; |
| 257 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 258 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 259 | if (mgr != null) { |
| 260 | try { |
| 261 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 262 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 263 | Log.e(TAG, "", e); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 267 | doBind(); |
| 268 | } |
| 269 | |
| 270 | boolean doBind() { |
| 271 | Intent intent = new Intent(IBluetoothA2dp.class.getName()); |
| 272 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 273 | intent.setComponent(comp); |
Dianne Hackborn | 466ce96 | 2014-03-19 18:06:58 -0700 | [diff] [blame] | 274 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
Cheney Ni | 4b6d6c2 | 2018-08-21 12:17:42 +0800 | [diff] [blame] | 275 | UserHandle.CURRENT_OR_SELF)) { |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 276 | Log.e(TAG, "Could not bind to Bluetooth A2DP Service with " + intent); |
| 277 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | } |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 279 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 282 | @UnsupportedAppUsage |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 283 | /*package*/ void close() { |
| 284 | mServiceListener = null; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 285 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 286 | if (mgr != null) { |
| 287 | try { |
| 288 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 289 | } catch (Exception e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 290 | Log.e(TAG, "", e); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 294 | try { |
| 295 | mServiceLock.writeLock().lock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 296 | if (mService != null) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 297 | mService = null; |
| 298 | mContext.unbindService(mConnection); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 299 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 300 | } catch (Exception re) { |
| 301 | Log.e(TAG, "", re); |
| 302 | } finally { |
| 303 | mServiceLock.writeLock().unlock(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 304 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 307 | @Override |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 308 | public void finalize() { |
Mathias Jeppsson | 2d2d8c2 | 2013-08-06 11:41:55 +0200 | [diff] [blame] | 309 | // The empty finalize needs to be kept or the |
| 310 | // cts signature tests would fail. |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 311 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 312 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 313 | /** |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 314 | * Initiate connection to a profile of the remote Bluetooth device. |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 315 | * |
| 316 | * <p> This API returns false in scenarios like the profile on the |
| 317 | * device is already connected or Bluetooth is not turned on. |
| 318 | * When this API returns true, it is guaranteed that |
| 319 | * connection state intent for the profile will be broadcasted with |
| 320 | * the state. Users can get the connection state of the profile |
| 321 | * from this intent. |
| 322 | * |
| 323 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 324 | * permission. |
| 325 | * |
| 326 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 327 | * @return false on immediate error, true otherwise |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 328 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 330 | @UnsupportedAppUsage |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 331 | public boolean connect(BluetoothDevice device) { |
| 332 | if (DBG) log("connect(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 333 | try { |
| 334 | mServiceLock.readLock().lock(); |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 335 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 336 | return mService.connect(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 337 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 338 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 339 | return false; |
| 340 | } catch (RemoteException e) { |
| 341 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 342 | return false; |
| 343 | } finally { |
| 344 | mServiceLock.readLock().unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 348 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 349 | * Initiate disconnection from a profile |
| 350 | * |
| 351 | * <p> This API will return false in scenarios like the profile on the |
| 352 | * Bluetooth device is not in connected state etc. When this API returns, |
| 353 | * true, it is guaranteed that the connection state change |
| 354 | * intent will be broadcasted with the state. Users can get the |
| 355 | * disconnection state of the profile from this intent. |
| 356 | * |
| 357 | * <p> If the disconnection is initiated by a remote device, the state |
| 358 | * will transition from {@link #STATE_CONNECTED} to |
| 359 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 360 | * host (local) device the state will transition from |
| 361 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 362 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 363 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 364 | * two scenarios. |
| 365 | * |
| 366 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 367 | * permission. |
| 368 | * |
| 369 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 370 | * @return false on immediate error, true otherwise |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 371 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 373 | @UnsupportedAppUsage |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 374 | public boolean disconnect(BluetoothDevice device) { |
| 375 | if (DBG) log("disconnect(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 376 | try { |
| 377 | mServiceLock.readLock().lock(); |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 378 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 379 | return mService.disconnect(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 380 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 381 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 382 | return false; |
| 383 | } catch (RemoteException e) { |
| 384 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 385 | return false; |
| 386 | } finally { |
| 387 | mServiceLock.readLock().unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 391 | /** |
| 392 | * {@inheritDoc} |
| 393 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 394 | @Override |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 395 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 396 | if (VDBG) log("getConnectedDevices()"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 397 | try { |
| 398 | mServiceLock.readLock().lock(); |
| 399 | if (mService != null && isEnabled()) { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 400 | return mService.getConnectedDevices(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 401 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 402 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 403 | return new ArrayList<BluetoothDevice>(); |
| 404 | } catch (RemoteException e) { |
| 405 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 406 | return new ArrayList<BluetoothDevice>(); |
| 407 | } finally { |
| 408 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 409 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
| 413 | * {@inheritDoc} |
| 414 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 415 | @Override |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 416 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 417 | if (VDBG) log("getDevicesMatchingStates()"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 418 | try { |
| 419 | mServiceLock.readLock().lock(); |
| 420 | if (mService != null && isEnabled()) { |
Jaikumar Ganesh | 03cd78c | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 421 | return mService.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 422 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 423 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 424 | return new ArrayList<BluetoothDevice>(); |
| 425 | } catch (RemoteException e) { |
| 426 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 427 | return new ArrayList<BluetoothDevice>(); |
| 428 | } finally { |
| 429 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 430 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
| 434 | * {@inheritDoc} |
| 435 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 436 | @Override |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 437 | public @BtProfileState int getConnectionState(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 438 | if (VDBG) log("getState(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 439 | try { |
| 440 | mServiceLock.readLock().lock(); |
| 441 | if (mService != null && isEnabled() |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 442 | && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 443 | return mService.getConnectionState(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 444 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 445 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 446 | return BluetoothProfile.STATE_DISCONNECTED; |
| 447 | } catch (RemoteException e) { |
| 448 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 449 | return BluetoothProfile.STATE_DISCONNECTED; |
| 450 | } finally { |
| 451 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 452 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /** |
Pavlin Radoslavov | 64a9435 | 2017-12-19 13:20:06 -0800 | [diff] [blame] | 456 | * Select a connected device as active. |
| 457 | * |
| 458 | * The active device selection is per profile. An active device's |
| 459 | * purpose is profile-specific. For example, A2DP audio streaming |
| 460 | * is to the active A2DP Sink device. If a remote device is not |
| 461 | * connected, it cannot be selected as active. |
| 462 | * |
| 463 | * <p> This API returns false in scenarios like the profile on the |
| 464 | * device is not connected or Bluetooth is not turned on. |
| 465 | * When this API returns true, it is guaranteed that the |
| 466 | * {@link #ACTION_ACTIVE_DEVICE_CHANGED} intent will be broadcasted |
| 467 | * with the active device. |
| 468 | * |
| 469 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 470 | * permission. |
| 471 | * |
| 472 | * @param device the remote Bluetooth device. Could be null to clear |
| 473 | * the active device and stop streaming audio to a Bluetooth device. |
| 474 | * @return false on immediate error, true otherwise |
| 475 | * @hide |
| 476 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 477 | @UnsupportedAppUsage |
Pavlin Radoslavov | 64a9435 | 2017-12-19 13:20:06 -0800 | [diff] [blame] | 478 | public boolean setActiveDevice(@Nullable BluetoothDevice device) { |
| 479 | if (DBG) log("setActiveDevice(" + device + ")"); |
| 480 | try { |
| 481 | mServiceLock.readLock().lock(); |
| 482 | if (mService != null && isEnabled() |
| 483 | && ((device == null) || isValidDevice(device))) { |
| 484 | return mService.setActiveDevice(device); |
| 485 | } |
| 486 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 487 | return false; |
| 488 | } catch (RemoteException e) { |
| 489 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 490 | return false; |
| 491 | } finally { |
| 492 | mServiceLock.readLock().unlock(); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Get the connected device that is active. |
| 498 | * |
| 499 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} |
| 500 | * permission. |
| 501 | * |
| 502 | * @return the connected device that is active or null if no device |
| 503 | * is active |
| 504 | * @hide |
| 505 | */ |
| 506 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 507 | @Nullable |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 508 | @UnsupportedAppUsage |
Pavlin Radoslavov | 64a9435 | 2017-12-19 13:20:06 -0800 | [diff] [blame] | 509 | public BluetoothDevice getActiveDevice() { |
| 510 | if (VDBG) log("getActiveDevice()"); |
| 511 | try { |
| 512 | mServiceLock.readLock().lock(); |
| 513 | if (mService != null && isEnabled()) { |
| 514 | return mService.getActiveDevice(); |
| 515 | } |
| 516 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 517 | return null; |
| 518 | } catch (RemoteException e) { |
| 519 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 520 | return null; |
| 521 | } finally { |
| 522 | mServiceLock.readLock().unlock(); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 527 | * Set priority of the profile |
| 528 | * |
| 529 | * <p> The device should already be paired. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 530 | * Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 531 | * {@link #PRIORITY_OFF}, |
| 532 | * |
| 533 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 534 | * permission. |
| 535 | * |
| 536 | * @param device Paired bluetooth device |
| 537 | * @param priority |
| 538 | * @return true if priority is set, false on error |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 539 | * @hide |
| 540 | */ |
| 541 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 542 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 543 | try { |
| 544 | mServiceLock.readLock().lock(); |
| 545 | if (mService != null && isEnabled() |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 546 | && isValidDevice(device)) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 547 | if (priority != BluetoothProfile.PRIORITY_OFF |
| 548 | && priority != BluetoothProfile.PRIORITY_ON) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 549 | return false; |
| 550 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 551 | return mService.setPriority(device, priority); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 552 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 553 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 554 | return false; |
| 555 | } catch (RemoteException e) { |
| 556 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 557 | return false; |
| 558 | } finally { |
| 559 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 560 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 564 | * Get the priority of the profile. |
| 565 | * |
| 566 | * <p> The priority can be any of: |
| 567 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 568 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 569 | * |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 570 | * @param device Bluetooth device |
| 571 | * @return priority of the device |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 572 | * @hide |
| 573 | */ |
Tor Norbye | 2d49752 | 2015-04-23 17:10:21 -0700 | [diff] [blame] | 574 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 575 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 576 | public int getPriority(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 577 | if (VDBG) log("getPriority(" + device + ")"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 578 | try { |
| 579 | mServiceLock.readLock().lock(); |
| 580 | if (mService != null && isEnabled() |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 581 | && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 582 | return mService.getPriority(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 583 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 584 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 585 | return BluetoothProfile.PRIORITY_OFF; |
| 586 | } catch (RemoteException e) { |
| 587 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 588 | return BluetoothProfile.PRIORITY_OFF; |
| 589 | } finally { |
| 590 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 591 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /** |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 595 | * Checks if Avrcp device supports the absolute volume feature. |
| 596 | * |
| 597 | * @return true if device supports absolute volume |
| 598 | * @hide |
| 599 | */ |
| 600 | public boolean isAvrcpAbsoluteVolumeSupported() { |
| 601 | if (DBG) Log.d(TAG, "isAvrcpAbsoluteVolumeSupported"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 602 | try { |
| 603 | mServiceLock.readLock().lock(); |
| 604 | if (mService != null && isEnabled()) { |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 605 | return mService.isAvrcpAbsoluteVolumeSupported(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 606 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 607 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 608 | return false; |
| 609 | } catch (RemoteException e) { |
| 610 | Log.e(TAG, "Error talking to BT service in isAvrcpAbsoluteVolumeSupported()", e); |
| 611 | return false; |
| 612 | } finally { |
| 613 | mServiceLock.readLock().unlock(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 614 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /** |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 618 | * Tells remote device to set an absolute volume. Only if absolute volume is supported |
| 619 | * |
| 620 | * @param volume Absolute volume to be set on AVRCP side |
| 621 | * @hide |
| 622 | */ |
| 623 | public void setAvrcpAbsoluteVolume(int volume) { |
| 624 | if (DBG) Log.d(TAG, "setAvrcpAbsoluteVolume"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 625 | try { |
| 626 | mServiceLock.readLock().lock(); |
| 627 | if (mService != null && isEnabled()) { |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 628 | mService.setAvrcpAbsoluteVolume(volume); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 629 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 630 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 631 | } catch (RemoteException e) { |
| 632 | Log.e(TAG, "Error talking to BT service in setAvrcpAbsoluteVolume()", e); |
| 633 | } finally { |
| 634 | mServiceLock.readLock().unlock(); |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 635 | } |
John Du | 5a0cf7a | 2013-07-19 11:30:34 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 639 | * Check if A2DP profile is streaming music. |
| 640 | * |
Jaikumar Ganesh | c8fa4ff | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 641 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 642 | * |
| 643 | * @param device BluetoothDevice device |
| 644 | */ |
| 645 | public boolean isA2dpPlaying(BluetoothDevice device) { |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 646 | try { |
| 647 | mServiceLock.readLock().lock(); |
| 648 | if (mService != null && isEnabled() |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 649 | && isValidDevice(device)) { |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 650 | return mService.isA2dpPlaying(device); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 651 | } |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 652 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 653 | return false; |
| 654 | } catch (RemoteException e) { |
| 655 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 656 | return false; |
| 657 | } finally { |
| 658 | mServiceLock.readLock().unlock(); |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 659 | } |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /** |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 663 | * This function checks if the remote device is an AVCRP |
| 664 | * target and thus whether we should send volume keys |
| 665 | * changes or not. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 666 | * |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 667 | * @hide |
| 668 | */ |
| 669 | public boolean shouldSendVolumeKeys(BluetoothDevice device) { |
| 670 | if (isEnabled() && isValidDevice(device)) { |
| 671 | ParcelUuid[] uuids = device.getUuids(); |
| 672 | if (uuids == null) return false; |
| 673 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 674 | for (ParcelUuid uuid : uuids) { |
Jaikumar Ganesh | 41d5c80 | 2010-10-13 19:11:28 -0700 | [diff] [blame] | 675 | if (BluetoothUuid.isAvrcpTarget(uuid)) { |
| 676 | return true; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | return false; |
| 681 | } |
| 682 | |
Matthew Xie | a0c6803 | 2011-06-25 21:47:07 -0700 | [diff] [blame] | 683 | /** |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 684 | * Gets the current codec status (configuration and capability). |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 685 | * |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 686 | * @param device the remote Bluetooth device. If null, use the current |
| 687 | * active A2DP Bluetooth device. |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 688 | * @return the current codec status |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 689 | * @hide |
| 690 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 691 | @UnsupportedAppUsage |
Jean-Michel Trivi | 5885037 | 2018-09-14 16:01:28 -0700 | [diff] [blame] | 692 | public @Nullable BluetoothCodecStatus getCodecStatus(BluetoothDevice device) { |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 693 | if (DBG) Log.d(TAG, "getCodecStatus(" + device + ")"); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 694 | try { |
| 695 | mServiceLock.readLock().lock(); |
| 696 | if (mService != null && isEnabled()) { |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 697 | return mService.getCodecStatus(device); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 698 | } |
| 699 | if (mService == null) { |
| 700 | Log.w(TAG, "Proxy not attached to service"); |
| 701 | } |
| 702 | return null; |
| 703 | } catch (RemoteException e) { |
Pavlin Radoslavov | b37f181 | 2017-01-25 16:54:07 -0800 | [diff] [blame] | 704 | Log.e(TAG, "Error talking to BT service in getCodecStatus()", e); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 705 | return null; |
| 706 | } finally { |
| 707 | mServiceLock.readLock().unlock(); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Sets the codec configuration preference. |
| 713 | * |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 714 | * @param device the remote Bluetooth device. If null, use the current |
| 715 | * active A2DP Bluetooth device. |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 716 | * @param codecConfig the codec configuration preference |
| 717 | * @hide |
| 718 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 719 | @UnsupportedAppUsage |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 720 | public void setCodecConfigPreference(BluetoothDevice device, |
| 721 | BluetoothCodecConfig codecConfig) { |
| 722 | if (DBG) Log.d(TAG, "setCodecConfigPreference(" + device + ")"); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 723 | try { |
| 724 | mServiceLock.readLock().lock(); |
| 725 | if (mService != null && isEnabled()) { |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 726 | mService.setCodecConfigPreference(device, codecConfig); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 727 | } |
| 728 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 729 | return; |
| 730 | } catch (RemoteException e) { |
| 731 | Log.e(TAG, "Error talking to BT service in setCodecConfigPreference()", e); |
| 732 | return; |
| 733 | } finally { |
| 734 | mServiceLock.readLock().unlock(); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /** |
Pavlin Radoslavov | 011597b | 2017-02-24 10:19:14 -0800 | [diff] [blame] | 739 | * Enables the optional codecs. |
| 740 | * |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 741 | * @param device the remote Bluetooth device. If null, use the currect |
| 742 | * active A2DP Bluetooth device. |
Pavlin Radoslavov | 011597b | 2017-02-24 10:19:14 -0800 | [diff] [blame] | 743 | * @hide |
| 744 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 745 | @UnsupportedAppUsage |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 746 | public void enableOptionalCodecs(BluetoothDevice device) { |
| 747 | if (DBG) Log.d(TAG, "enableOptionalCodecs(" + device + ")"); |
| 748 | enableDisableOptionalCodecs(device, true); |
Pavlin Radoslavov | 011597b | 2017-02-24 10:19:14 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Disables the optional codecs. |
| 753 | * |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 754 | * @param device the remote Bluetooth device. If null, use the currect |
| 755 | * active A2DP Bluetooth device. |
Pavlin Radoslavov | 011597b | 2017-02-24 10:19:14 -0800 | [diff] [blame] | 756 | * @hide |
| 757 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 758 | @UnsupportedAppUsage |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 759 | public void disableOptionalCodecs(BluetoothDevice device) { |
| 760 | if (DBG) Log.d(TAG, "disableOptionalCodecs(" + device + ")"); |
| 761 | enableDisableOptionalCodecs(device, false); |
Pavlin Radoslavov | 011597b | 2017-02-24 10:19:14 -0800 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Enables or disables the optional codecs. |
| 766 | * |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 767 | * @param device the remote Bluetooth device. If null, use the currect |
| 768 | * active A2DP Bluetooth device. |
Pavlin Radoslavov | 011597b | 2017-02-24 10:19:14 -0800 | [diff] [blame] | 769 | * @param enable if true, enable the optional codecs, other disable them |
| 770 | */ |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 771 | private void enableDisableOptionalCodecs(BluetoothDevice device, boolean enable) { |
Pavlin Radoslavov | 011597b | 2017-02-24 10:19:14 -0800 | [diff] [blame] | 772 | try { |
| 773 | mServiceLock.readLock().lock(); |
| 774 | if (mService != null && isEnabled()) { |
| 775 | if (enable) { |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 776 | mService.enableOptionalCodecs(device); |
Pavlin Radoslavov | 011597b | 2017-02-24 10:19:14 -0800 | [diff] [blame] | 777 | } else { |
Pavlin Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 778 | mService.disableOptionalCodecs(device); |
Pavlin Radoslavov | 011597b | 2017-02-24 10:19:14 -0800 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 782 | return; |
| 783 | } catch (RemoteException e) { |
| 784 | Log.e(TAG, "Error talking to BT service in enableDisableOptionalCodecs()", e); |
| 785 | return; |
| 786 | } finally { |
| 787 | mServiceLock.readLock().unlock(); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /** |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 792 | * Returns whether this device supports optional codecs. |
| 793 | * |
| 794 | * @param device The device to check |
| 795 | * @return one of OPTIONAL_CODECS_SUPPORT_UNKNOWN, OPTIONAL_CODECS_NOT_SUPPORTED, or |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 796 | * OPTIONAL_CODECS_SUPPORTED. |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 797 | * @hide |
| 798 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 799 | @UnsupportedAppUsage |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 800 | public int supportsOptionalCodecs(BluetoothDevice device) { |
| 801 | try { |
| 802 | mServiceLock.readLock().lock(); |
| 803 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 804 | return mService.supportsOptionalCodecs(device); |
| 805 | } |
| 806 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 807 | return OPTIONAL_CODECS_SUPPORT_UNKNOWN; |
| 808 | } catch (RemoteException e) { |
| 809 | Log.e(TAG, "Error talking to BT service in getSupportsOptionalCodecs()", e); |
| 810 | return OPTIONAL_CODECS_SUPPORT_UNKNOWN; |
| 811 | } finally { |
| 812 | mServiceLock.readLock().unlock(); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Returns whether this device should have optional codecs enabled. |
| 818 | * |
| 819 | * @param device The device in question. |
| 820 | * @return one of OPTIONAL_CODECS_PREF_UNKNOWN, OPTIONAL_CODECS_PREF_ENABLED, or |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 821 | * OPTIONAL_CODECS_PREF_DISABLED. |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 822 | * @hide |
| 823 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 824 | @UnsupportedAppUsage |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 825 | public int getOptionalCodecsEnabled(BluetoothDevice device) { |
| 826 | try { |
| 827 | mServiceLock.readLock().lock(); |
| 828 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 829 | return mService.getOptionalCodecsEnabled(device); |
| 830 | } |
| 831 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 832 | return OPTIONAL_CODECS_PREF_UNKNOWN; |
| 833 | } catch (RemoteException e) { |
| 834 | Log.e(TAG, "Error talking to BT service in getSupportsOptionalCodecs()", e); |
| 835 | return OPTIONAL_CODECS_PREF_UNKNOWN; |
| 836 | } finally { |
| 837 | mServiceLock.readLock().unlock(); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * Sets a persistent preference for whether a given device should have optional codecs enabled. |
| 843 | * |
| 844 | * @param device The device to set this preference for. |
| 845 | * @param value Whether the optional codecs should be enabled for this device. This should be |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 846 | * one of OPTIONAL_CODECS_PREF_UNKNOWN, OPTIONAL_CODECS_PREF_ENABLED, or |
| 847 | * OPTIONAL_CODECS_PREF_DISABLED. |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 848 | * @hide |
| 849 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 850 | @UnsupportedAppUsage |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 851 | public void setOptionalCodecsEnabled(BluetoothDevice device, int value) { |
| 852 | try { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 853 | if (value != BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN |
| 854 | && value != BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED |
| 855 | && value != BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED) { |
Antony Sargent | f5772c6 | 2017-04-26 16:37:53 -0700 | [diff] [blame] | 856 | Log.e(TAG, "Invalid value passed to setOptionalCodecsEnabled: " + value); |
| 857 | return; |
| 858 | } |
| 859 | mServiceLock.readLock().lock(); |
| 860 | if (mService != null && isEnabled() |
| 861 | && isValidDevice(device)) { |
| 862 | mService.setOptionalCodecsEnabled(device, value); |
| 863 | } |
| 864 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 865 | return; |
| 866 | } catch (RemoteException e) { |
| 867 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 868 | return; |
| 869 | } finally { |
| 870 | mServiceLock.readLock().unlock(); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | /** |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 875 | * Helper for converting a state to a string. |
| 876 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | * For debug use only - strings are not internationalized. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 878 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | * @hide |
| 880 | */ |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 881 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | public static String stateToString(int state) { |
| 883 | switch (state) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 884 | case STATE_DISCONNECTED: |
| 885 | return "disconnected"; |
| 886 | case STATE_CONNECTING: |
| 887 | return "connecting"; |
| 888 | case STATE_CONNECTED: |
| 889 | return "connected"; |
| 890 | case STATE_DISCONNECTING: |
| 891 | return "disconnecting"; |
| 892 | case STATE_PLAYING: |
| 893 | return "playing"; |
| 894 | case STATE_NOT_PLAYING: |
| 895 | return "not playing"; |
| 896 | default: |
| 897 | return "<unknown state " + state + ">"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | } |
| 899 | } |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 900 | |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 901 | private final ServiceConnection mConnection = new ServiceConnection() { |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 902 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 903 | if (DBG) Log.d(TAG, "Proxy object connected"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 904 | try { |
| 905 | mServiceLock.writeLock().lock(); |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 906 | mService = IBluetoothA2dp.Stub.asInterface(Binder.allowBlocking(service)); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 907 | } finally { |
| 908 | mServiceLock.writeLock().unlock(); |
| 909 | } |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 910 | |
| 911 | if (mServiceListener != null) { |
| 912 | mServiceListener.onServiceConnected(BluetoothProfile.A2DP, BluetoothA2dp.this); |
| 913 | } |
| 914 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 915 | |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 916 | public void onServiceDisconnected(ComponentName className) { |
| 917 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
Calvin On | d7d16b9 | 2016-06-20 15:59:48 -0700 | [diff] [blame] | 918 | try { |
| 919 | mServiceLock.writeLock().lock(); |
| 920 | mService = null; |
| 921 | } finally { |
| 922 | mServiceLock.writeLock().unlock(); |
| 923 | } |
Matthew Xie | 3e8c82e | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 924 | if (mServiceListener != null) { |
| 925 | mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP); |
| 926 | } |
| 927 | } |
| 928 | }; |
| 929 | |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 930 | private boolean isEnabled() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 931 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 932 | return false; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | private boolean isValidDevice(BluetoothDevice device) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 936 | if (device == null) return false; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 937 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 938 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 939 | return false; |
Jaikumar Ganesh | 62c37ef | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 940 | } |
| 941 | |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 942 | private static void log(String msg) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 943 | Log.d(TAG, msg); |
The Android Open Source Project | f5b4b98 | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 944 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | } |