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