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