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