Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 20 | import android.os.Binder; |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 21 | import android.os.IBinder; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 22 | import android.os.RemoteException; |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 28 | /** |
| 29 | * This class provides the APIs to control the Bluetooth PBAP Client Profile. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 30 | * |
| 31 | * @hide |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 32 | */ |
| 33 | public final class BluetoothPbapClient implements BluetoothProfile { |
| 34 | |
| 35 | private static final String TAG = "BluetoothPbapClient"; |
| 36 | private static final boolean DBG = false; |
| 37 | private static final boolean VDBG = false; |
| 38 | |
| 39 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | 37ab815 | 2017-10-02 19:08:30 -0700 | [diff] [blame] | 40 | "android.bluetooth.pbapclient.profile.action.CONNECTION_STATE_CHANGED"; |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 41 | |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 42 | /** There was an error trying to obtain the state */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 43 | public static final int STATE_ERROR = -1; |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 44 | |
| 45 | public static final int RESULT_FAILURE = 0; |
| 46 | public static final int RESULT_SUCCESS = 1; |
| 47 | /** Connection canceled before completion. */ |
| 48 | public static final int RESULT_CANCELED = 2; |
| 49 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 50 | private BluetoothAdapter mAdapter; |
| 51 | private final BluetoothProfileConnector<IBluetoothPbapClient> mProfileConnector = |
| 52 | new BluetoothProfileConnector(this, BluetoothProfile.PBAP_CLIENT, |
| 53 | "BluetoothPbapClient", IBluetoothPbapClient.class.getName()) { |
| 54 | @Override |
| 55 | public IBluetoothPbapClient getServiceInterface(IBinder service) { |
| 56 | return IBluetoothPbapClient.Stub.asInterface(Binder.allowBlocking(service)); |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 57 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 58 | }; |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Create a BluetoothPbapClient proxy object. |
| 62 | */ |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 63 | BluetoothPbapClient(Context context, ServiceListener listener) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 64 | if (DBG) { |
| 65 | Log.d(TAG, "Create BluetoothPbapClient proxy object"); |
| 66 | } |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 67 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 68 | mProfileConnector.connect(context, listener); |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | protected void finalize() throws Throwable { |
| 72 | try { |
| 73 | close(); |
| 74 | } finally { |
| 75 | super.finalize(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Close the connection to the backing service. |
| 81 | * Other public functions of BluetoothPbapClient will return default error |
| 82 | * results once close() has been called. Multiple invocations of close() |
| 83 | * are ok. |
| 84 | */ |
| 85 | public synchronized void close() { |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 86 | mProfileConnector.disconnect(); |
| 87 | } |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 88 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 89 | private IBluetoothPbapClient getService() { |
| 90 | return mProfileConnector.getService(); |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Initiate connection. |
| 95 | * Upon successful connection to remote PBAP server the Client will |
| 96 | * attempt to automatically download the users phonebook and call log. |
| 97 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 98 | * @param device a remote device we want connect to |
| 99 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 100 | * otherwise; |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 101 | */ |
| 102 | public boolean connect(BluetoothDevice device) { |
| 103 | if (DBG) { |
| 104 | log("connect(" + device + ") for PBAP Client."); |
| 105 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 106 | final IBluetoothPbapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 107 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 108 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 109 | return service.connect(device); |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 110 | } catch (RemoteException e) { |
| 111 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 112 | return false; |
| 113 | } |
| 114 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 115 | if (service == null) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 116 | Log.w(TAG, "Proxy not attached to service"); |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Initiate disconnect. |
| 123 | * |
| 124 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 125 | * @return false on error, true otherwise |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 126 | */ |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 127 | public boolean disconnect(BluetoothDevice device) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 128 | if (DBG) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 129 | log("disconnect(" + device + ")" + new Exception()); |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 130 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 131 | final IBluetoothPbapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 132 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 133 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 134 | service.disconnect(device); |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 135 | return true; |
| 136 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 137 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 138 | return false; |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 141 | if (service == null) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 142 | Log.w(TAG, "Proxy not attached to service"); |
| 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get the list of connected devices. |
| 149 | * Currently at most one. |
| 150 | * |
| 151 | * @return list of connected devices |
| 152 | */ |
| 153 | @Override |
| 154 | public List<BluetoothDevice> getConnectedDevices() { |
| 155 | if (DBG) { |
| 156 | log("getConnectedDevices()"); |
| 157 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 158 | final IBluetoothPbapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 159 | if (service != null && isEnabled()) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 160 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 161 | return service.getConnectedDevices(); |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 162 | } catch (RemoteException e) { |
| 163 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 164 | return new ArrayList<BluetoothDevice>(); |
| 165 | } |
| 166 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 167 | if (service == null) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 168 | Log.w(TAG, "Proxy not attached to service"); |
| 169 | } |
| 170 | return new ArrayList<BluetoothDevice>(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get the list of devices matching specified states. Currently at most one. |
| 175 | * |
| 176 | * @return list of matching devices |
| 177 | */ |
| 178 | @Override |
| 179 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 180 | if (DBG) { |
| 181 | log("getDevicesMatchingStates()"); |
| 182 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 183 | final IBluetoothPbapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 184 | if (service != null && isEnabled()) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 185 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 186 | return service.getDevicesMatchingConnectionStates(states); |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 187 | } catch (RemoteException e) { |
| 188 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 189 | return new ArrayList<BluetoothDevice>(); |
| 190 | } |
| 191 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 192 | if (service == null) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 193 | Log.w(TAG, "Proxy not attached to service"); |
| 194 | } |
| 195 | return new ArrayList<BluetoothDevice>(); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Get connection state of device |
| 200 | * |
| 201 | * @return device connection state |
| 202 | */ |
| 203 | @Override |
| 204 | public int getConnectionState(BluetoothDevice device) { |
| 205 | if (DBG) { |
| 206 | log("getConnectionState(" + device + ")"); |
| 207 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 208 | final IBluetoothPbapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 209 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 210 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 211 | return service.getConnectionState(device); |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 212 | } catch (RemoteException e) { |
| 213 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 214 | return BluetoothProfile.STATE_DISCONNECTED; |
| 215 | } |
| 216 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 217 | if (service == null) { |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 218 | Log.w(TAG, "Proxy not attached to service"); |
| 219 | } |
| 220 | return BluetoothProfile.STATE_DISCONNECTED; |
| 221 | } |
| 222 | |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 223 | private static void log(String msg) { |
| 224 | Log.d(TAG, msg); |
| 225 | } |
| 226 | |
| 227 | private boolean isEnabled() { |
| 228 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 229 | if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) { |
| 230 | return true; |
| 231 | } |
| 232 | log("Bluetooth is Not enabled"); |
| 233 | return false; |
| 234 | } |
| 235 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 236 | private static boolean isValidDevice(BluetoothDevice device) { |
| 237 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 238 | } |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * Set priority of the profile |
| 242 | * |
| 243 | * <p> The device should already be paired. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 244 | * Priority can be one of {@link #PRIORITY_ON} or |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 245 | * {@link #PRIORITY_OFF}, |
| 246 | * |
| 247 | * @param device Paired bluetooth device |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 248 | * @param priority Priority of this profile |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 249 | * @return true if priority is set, false on error |
| 250 | */ |
| 251 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 252 | if (DBG) { |
| 253 | log("setPriority(" + device + ", " + priority + ")"); |
| 254 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 255 | final IBluetoothPbapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 256 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 257 | if (priority != BluetoothProfile.PRIORITY_OFF |
| 258 | && priority != BluetoothProfile.PRIORITY_ON) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 259 | return false; |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 260 | } |
| 261 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 262 | return service.setPriority(device, priority); |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 263 | } catch (RemoteException e) { |
| 264 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 265 | return false; |
| 266 | } |
| 267 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 268 | if (service == null) { |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 269 | Log.w(TAG, "Proxy not attached to service"); |
| 270 | } |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Get the priority of the profile. |
| 276 | * |
| 277 | * <p> The priority can be any of: |
| 278 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 279 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 280 | * |
| 281 | * @param device Bluetooth device |
| 282 | * @return priority of the device |
| 283 | */ |
| 284 | public int getPriority(BluetoothDevice device) { |
| 285 | if (VDBG) { |
| 286 | log("getPriority(" + device + ")"); |
| 287 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 288 | final IBluetoothPbapClient service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 289 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 290 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 291 | return service.getPriority(device); |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 292 | } catch (RemoteException e) { |
| 293 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 294 | return PRIORITY_OFF; |
| 295 | } |
| 296 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 297 | if (service == null) { |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 298 | Log.w(TAG, "Proxy not attached to service"); |
| 299 | } |
| 300 | return PRIORITY_OFF; |
| 301 | } |
Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [diff] [blame] | 302 | } |