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