Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [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 | |
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; |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 23 | import android.content.Context; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 24 | import android.os.Binder; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 25 | import android.os.IBinder; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 26 | import android.os.RemoteException; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 32 | /** |
| 33 | * This class provides the APIs to control the Bluetooth SIM |
| 34 | * Access Profile (SAP). |
| 35 | * |
| 36 | * <p>BluetoothSap is a proxy object for controlling the Bluetooth |
| 37 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 38 | * the BluetoothSap proxy object. |
| 39 | * |
| 40 | * <p>Each method is protected with its appropriate permission. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 41 | * |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 42 | * @hide |
| 43 | */ |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 44 | public final class BluetoothSap implements BluetoothProfile { |
| 45 | |
| 46 | private static final String TAG = "BluetoothSap"; |
| 47 | private static final boolean DBG = true; |
| 48 | private static final boolean VDBG = false; |
| 49 | |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Intent used to broadcast the change in connection state of the profile. |
| 52 | * |
| 53 | * <p>This intent will have 4 extras: |
| 54 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 55 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 56 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 57 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 58 | * </ul> |
| 59 | * |
| 60 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 61 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 62 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 63 | * |
| 64 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 65 | * receive. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 66 | * |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 67 | * @hide |
| 68 | */ |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 69 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 70 | "android.bluetooth.sap.profile.action.CONNECTION_STATE_CHANGED"; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 71 | |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 72 | /** |
| 73 | * There was an error trying to obtain the state. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 74 | * |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 75 | * @hide |
| 76 | */ |
| 77 | public static final int STATE_ERROR = -1; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 78 | |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Connection state change succceeded. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 81 | * |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 82 | * @hide |
| 83 | */ |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 84 | public static final int RESULT_SUCCESS = 1; |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Connection canceled before completion. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 88 | * |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 89 | * @hide |
| 90 | */ |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 91 | public static final int RESULT_CANCELED = 2; |
| 92 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 93 | private BluetoothAdapter mAdapter; |
| 94 | private final BluetoothProfileConnector<IBluetoothSap> mProfileConnector = |
| 95 | new BluetoothProfileConnector(this, BluetoothProfile.SAP, |
| 96 | "BluetoothSap", IBluetoothSap.class.getName()) { |
| 97 | @Override |
| 98 | public IBluetoothSap getServiceInterface(IBinder service) { |
| 99 | return IBluetoothSap.Stub.asInterface(Binder.allowBlocking(service)); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 100 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 101 | }; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Create a BluetoothSap proxy object. |
| 105 | */ |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 106 | /*package*/ BluetoothSap(Context context, ServiceListener listener) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 107 | if (DBG) Log.d(TAG, "Create BluetoothSap proxy object"); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 108 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 109 | mProfileConnector.connect(context, listener); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | protected void finalize() throws Throwable { |
| 113 | try { |
| 114 | close(); |
| 115 | } finally { |
| 116 | super.finalize(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Close the connection to the backing service. |
| 122 | * Other public functions of BluetoothSap will return default error |
| 123 | * results once close() has been called. Multiple invocations of close() |
| 124 | * are ok. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 125 | * |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 126 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 127 | */ |
| 128 | public synchronized void close() { |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 129 | mProfileConnector.disconnect(); |
| 130 | } |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 131 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 132 | private IBluetoothSap getService() { |
| 133 | return mProfileConnector.getService(); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get the current state of the BluetoothSap service. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 138 | * |
| 139 | * @return One of the STATE_ return codes, or STATE_ERROR if this proxy object is currently not |
| 140 | * connected to the Sap service. |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 141 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 142 | */ |
| 143 | public int getState() { |
| 144 | if (VDBG) log("getState()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 145 | final IBluetoothSap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 146 | if (service != null) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 147 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 148 | return service.getState(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 149 | } catch (RemoteException e) { |
| 150 | Log.e(TAG, e.toString()); |
| 151 | } |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 152 | } else { |
| 153 | Log.w(TAG, "Proxy not attached to service"); |
| 154 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 155 | } |
| 156 | return BluetoothSap.STATE_ERROR; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the currently connected remote Bluetooth device (PCE). |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 161 | * |
| 162 | * @return The remote Bluetooth device, or null if not in connected or connecting state, or if |
| 163 | * this proxy object is not connected to the Sap service. |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 164 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 165 | */ |
| 166 | public BluetoothDevice getClient() { |
| 167 | if (VDBG) log("getClient()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 168 | final IBluetoothSap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 169 | if (service != null) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 170 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 171 | return service.getClient(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 172 | } catch (RemoteException e) { |
| 173 | Log.e(TAG, e.toString()); |
| 174 | } |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 175 | } else { |
| 176 | Log.w(TAG, "Proxy not attached to service"); |
| 177 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 178 | } |
| 179 | return null; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Returns true if the specified Bluetooth device is connected. |
| 184 | * Returns false if not connected, or if this proxy object is not |
| 185 | * currently connected to the Sap service. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 186 | * |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 187 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 188 | */ |
| 189 | public boolean isConnected(BluetoothDevice device) { |
| 190 | if (VDBG) log("isConnected(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 191 | final IBluetoothSap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 192 | if (service != null) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 193 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 194 | return service.isConnected(device); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 195 | } catch (RemoteException e) { |
| 196 | Log.e(TAG, e.toString()); |
| 197 | } |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 198 | } else { |
| 199 | Log.w(TAG, "Proxy not attached to service"); |
| 200 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Initiate connection. Initiation of outgoing connections is not |
| 207 | * supported for SAP server. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 208 | * |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 209 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 210 | */ |
| 211 | public boolean connect(BluetoothDevice device) { |
| 212 | if (DBG) log("connect(" + device + ")" + "not supported for SAPS"); |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Initiate disconnect. |
| 218 | * |
| 219 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 220 | * @return false on error, true otherwise |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 221 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 222 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 223 | @UnsupportedAppUsage |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 224 | public boolean disconnect(BluetoothDevice device) { |
| 225 | if (DBG) log("disconnect(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 226 | final IBluetoothSap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 227 | if (service != null && isEnabled() && isValidDevice(device)) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 228 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 229 | return service.disconnect(device); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 230 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 231 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 232 | return false; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 233 | } |
| 234 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 235 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get the list of connected devices. Currently at most one. |
| 241 | * |
| 242 | * @return list of connected devices |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 243 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 244 | */ |
| 245 | public List<BluetoothDevice> getConnectedDevices() { |
| 246 | if (DBG) log("getConnectedDevices()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 247 | final IBluetoothSap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 248 | if (service != null && isEnabled()) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 249 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 250 | return service.getConnectedDevices(); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 251 | } catch (RemoteException e) { |
| 252 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 253 | return new ArrayList<BluetoothDevice>(); |
| 254 | } |
| 255 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 256 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 257 | return new ArrayList<BluetoothDevice>(); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Get the list of devices matching specified states. Currently at most one. |
| 262 | * |
| 263 | * @return list of matching devices |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 264 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 265 | */ |
| 266 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 267 | if (DBG) log("getDevicesMatchingStates()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 268 | final IBluetoothSap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 269 | if (service != null && isEnabled()) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 270 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 271 | return service.getDevicesMatchingConnectionStates(states); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 272 | } catch (RemoteException e) { |
| 273 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 274 | return new ArrayList<BluetoothDevice>(); |
| 275 | } |
| 276 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 277 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 278 | return new ArrayList<BluetoothDevice>(); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Get connection state of device |
| 283 | * |
| 284 | * @return device connection state |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 285 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 286 | */ |
| 287 | public int getConnectionState(BluetoothDevice device) { |
| 288 | if (DBG) log("getConnectionState(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 289 | final IBluetoothSap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 290 | if (service != null && isEnabled() && isValidDevice(device)) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 291 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 292 | return service.getConnectionState(device); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 293 | } catch (RemoteException e) { |
| 294 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 295 | return BluetoothProfile.STATE_DISCONNECTED; |
| 296 | } |
| 297 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 298 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 299 | return BluetoothProfile.STATE_DISCONNECTED; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Set priority of the profile |
| 304 | * |
| 305 | * <p> The device should already be paired. |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 306 | * Priority can be one of {@link #PRIORITY_ON} or {@link #PRIORITY_OFF}, |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 307 | * |
| 308 | * @param device Paired bluetooth device |
| 309 | * @param priority |
| 310 | * @return true if priority is set, false on error |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 311 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 312 | */ |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 313 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 314 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 315 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 316 | return setConnectionPolicy(device, BluetoothAdapter.priorityToConnectionPolicy(priority)); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Set connection policy of the profile |
| 321 | * |
| 322 | * <p> The device should already be paired. |
| 323 | * Connection policy can be one of {@link #CONNECTION_POLICY_ALLOWED}, |
| 324 | * {@link #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN} |
| 325 | * |
| 326 | * @param device Paired bluetooth device |
| 327 | * @param connectionPolicy is the connection policy to set to for this profile |
| 328 | * @return true if connectionPolicy is set, false on error |
| 329 | * @hide |
| 330 | */ |
| 331 | @SystemApi |
| 332 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
| 333 | public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) { |
| 334 | if (DBG) log("setConnectionPolicy(" + device + ", " + connectionPolicy + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 335 | final IBluetoothSap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 336 | if (service != null && isEnabled() && isValidDevice(device)) { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 337 | if (connectionPolicy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN |
| 338 | && connectionPolicy != BluetoothProfile.CONNECTION_POLICY_ALLOWED) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 339 | return false; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 340 | } |
| 341 | try { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 342 | return service.setConnectionPolicy(device, connectionPolicy); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 343 | } catch (RemoteException e) { |
| 344 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 345 | return false; |
| 346 | } |
| 347 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 348 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Get the priority of the profile. |
| 354 | * |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 355 | * <p> The priority can be any of: |
| 356 | * {@link #PRIORITY_OFF}, {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 357 | * |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 358 | * @param device Bluetooth device |
| 359 | * @return priority of the device |
Andre Eisenbach | ab25813 | 2015-05-04 13:28:04 -0700 | [diff] [blame] | 360 | * @hide |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 361 | */ |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 362 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 363 | public int getPriority(BluetoothDevice device) { |
| 364 | if (VDBG) log("getPriority(" + device + ")"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 365 | return BluetoothAdapter.connectionPolicyToPriority(getConnectionPolicy(device)); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Get the connection policy of the profile. |
| 370 | * |
| 371 | * <p> The connection policy can be any of: |
| 372 | * {@link #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN}, |
| 373 | * {@link #CONNECTION_POLICY_UNKNOWN} |
| 374 | * |
| 375 | * @param device Bluetooth device |
| 376 | * @return connection policy of the device |
| 377 | * @hide |
| 378 | */ |
| 379 | @SystemApi |
| 380 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 381 | public int getConnectionPolicy(BluetoothDevice device) { |
| 382 | if (VDBG) log("getConnectionPolicy(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 383 | final IBluetoothSap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 384 | if (service != null && isEnabled() && isValidDevice(device)) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 385 | try { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 386 | return service.getConnectionPolicy(device); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 387 | } catch (RemoteException e) { |
| 388 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 389 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 390 | } |
| 391 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 392 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 393 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 396 | private static void log(String msg) { |
| 397 | Log.d(TAG, msg); |
| 398 | } |
| 399 | |
| 400 | private boolean isEnabled() { |
| 401 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 402 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 403 | if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) { |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 404 | return true; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 405 | } |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 406 | log("Bluetooth is Not enabled"); |
| 407 | return false; |
| 408 | } |
| 409 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 410 | private static boolean isValidDevice(BluetoothDevice device) { |
| 411 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Casper Bonde | 2a5f608 | 2015-03-19 10:36:45 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | } |