Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [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; |
Artur Satayev | 76c1d9d | 2019-12-10 17:47:52 +0000 | [diff] [blame^] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 24 | import android.os.Binder; |
| 25 | import android.os.IBinder; |
| 26 | import android.os.RemoteException; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [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 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 32 | /** |
| 33 | * This class provides the APIs to control the Bluetooth MAP |
| 34 | * Profile. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 35 | * |
| 36 | * @hide |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 37 | */ |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 38 | public final class BluetoothMap implements BluetoothProfile { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 39 | |
| 40 | private static final String TAG = "BluetoothMap"; |
| 41 | private static final boolean DBG = true; |
| 42 | private static final boolean VDBG = false; |
| 43 | |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 44 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 45 | "android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED"; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 46 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 47 | /** There was an error trying to obtain the state */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 48 | public static final int STATE_ERROR = -1; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 49 | |
| 50 | public static final int RESULT_FAILURE = 0; |
| 51 | public static final int RESULT_SUCCESS = 1; |
| 52 | /** Connection canceled before completion. */ |
| 53 | public static final int RESULT_CANCELED = 2; |
| 54 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 55 | private BluetoothAdapter mAdapter; |
| 56 | private final BluetoothProfileConnector<IBluetoothMap> mProfileConnector = |
| 57 | new BluetoothProfileConnector(this, BluetoothProfile.MAP, |
| 58 | "BluetoothMap", IBluetoothMap.class.getName()) { |
| 59 | @Override |
| 60 | public IBluetoothMap getServiceInterface(IBinder service) { |
| 61 | return IBluetoothMap.Stub.asInterface(Binder.allowBlocking(service)); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 62 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 63 | }; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Create a BluetoothMap proxy object. |
| 67 | */ |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 68 | /*package*/ BluetoothMap(Context context, ServiceListener listener) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 69 | if (DBG) Log.d(TAG, "Create BluetoothMap proxy object"); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 70 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 71 | mProfileConnector.connect(context, listener); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [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 BluetoothMap 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 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 91 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 92 | private IBluetoothMap getService() { |
| 93 | return mProfileConnector.getService(); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the current state of the BluetoothMap service. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 98 | * |
| 99 | * @return One of the STATE_ return codes, or STATE_ERROR if this proxy object is currently not |
| 100 | * connected to the Map service. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 101 | */ |
| 102 | public int getState() { |
| 103 | if (VDBG) log("getState()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 104 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 105 | if (service != null) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 106 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 107 | return service.getState(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 108 | } catch (RemoteException e) { |
| 109 | Log.e(TAG, e.toString()); |
| 110 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 111 | } else { |
| 112 | Log.w(TAG, "Proxy not attached to service"); |
| 113 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 114 | } |
| 115 | return BluetoothMap.STATE_ERROR; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get the currently connected remote Bluetooth device (PCE). |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 120 | * |
| 121 | * @return The remote Bluetooth device, or null if not in connected or connecting state, or if |
| 122 | * this proxy object is not connected to the Map service. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 123 | */ |
| 124 | public BluetoothDevice getClient() { |
| 125 | if (VDBG) log("getClient()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 126 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 127 | if (service != null) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 128 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 129 | return service.getClient(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 130 | } catch (RemoteException e) { |
| 131 | Log.e(TAG, e.toString()); |
| 132 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 133 | } else { |
| 134 | Log.w(TAG, "Proxy not attached to service"); |
| 135 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 136 | } |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | /** |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 141 | * Returns true if the specified Bluetooth device is connected. |
| 142 | * Returns false if not connected, or if this proxy object is not |
| 143 | * currently connected to the Map service. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 144 | */ |
| 145 | public boolean isConnected(BluetoothDevice device) { |
| 146 | if (VDBG) log("isConnected(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 147 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 148 | if (service != null) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 149 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 150 | return service.isConnected(device); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 151 | } catch (RemoteException e) { |
| 152 | Log.e(TAG, e.toString()); |
| 153 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 154 | } else { |
| 155 | Log.w(TAG, "Proxy not attached to service"); |
| 156 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | /** |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 162 | * Initiate connection. Initiation of outgoing connections is not |
| 163 | * supported for MAP server. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 164 | */ |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 165 | public boolean connect(BluetoothDevice device) { |
| 166 | if (DBG) log("connect(" + device + ")" + "not supported for MAPS"); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Initiate disconnect. |
| 172 | * |
| 173 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 174 | * @return false on error, true otherwise |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 175 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 176 | @UnsupportedAppUsage |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 177 | public boolean disconnect(BluetoothDevice device) { |
| 178 | if (DBG) log("disconnect(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 179 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 180 | if (service != null && isEnabled() && isValidDevice(device)) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 181 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 182 | return service.disconnect(device); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 183 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 184 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 185 | return false; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 186 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 187 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 188 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Check class bits for possible Map support. |
| 194 | * This is a simple heuristic that tries to guess if a device with the |
| 195 | * given class bits might support Map. It is not accurate for all |
| 196 | * devices. It tries to err on the side of false positives. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 197 | * |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 198 | * @return True if this device might support Map. |
| 199 | */ |
| 200 | public static boolean doesClassMatchSink(BluetoothClass btClass) { |
| 201 | // TODO optimize the rule |
| 202 | switch (btClass.getDeviceClass()) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 203 | case BluetoothClass.Device.COMPUTER_DESKTOP: |
| 204 | case BluetoothClass.Device.COMPUTER_LAPTOP: |
| 205 | case BluetoothClass.Device.COMPUTER_SERVER: |
| 206 | case BluetoothClass.Device.COMPUTER_UNCATEGORIZED: |
| 207 | return true; |
| 208 | default: |
| 209 | return false; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 213 | /** |
| 214 | * Get the list of connected devices. Currently at most one. |
| 215 | * |
| 216 | * @return list of connected devices |
| 217 | */ |
| 218 | public List<BluetoothDevice> getConnectedDevices() { |
| 219 | if (DBG) log("getConnectedDevices()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 220 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 221 | if (service != null && isEnabled()) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 222 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 223 | return service.getConnectedDevices(); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 224 | } catch (RemoteException e) { |
| 225 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 226 | return new ArrayList<BluetoothDevice>(); |
| 227 | } |
| 228 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 229 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 230 | return new ArrayList<BluetoothDevice>(); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Get the list of devices matching specified states. Currently at most one. |
| 235 | * |
| 236 | * @return list of matching devices |
| 237 | */ |
| 238 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 239 | if (DBG) log("getDevicesMatchingStates()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 240 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 241 | if (service != null && isEnabled()) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 242 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 243 | return service.getDevicesMatchingConnectionStates(states); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 244 | } catch (RemoteException e) { |
| 245 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 246 | return new ArrayList<BluetoothDevice>(); |
| 247 | } |
| 248 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 249 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 250 | return new ArrayList<BluetoothDevice>(); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Get connection state of device |
| 255 | * |
| 256 | * @return device connection state |
| 257 | */ |
| 258 | public int getConnectionState(BluetoothDevice device) { |
| 259 | if (DBG) log("getConnectionState(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 260 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 261 | if (service != null && isEnabled() && isValidDevice(device)) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 262 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 263 | return service.getConnectionState(device); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 264 | } catch (RemoteException e) { |
| 265 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 266 | return BluetoothProfile.STATE_DISCONNECTED; |
| 267 | } |
| 268 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 269 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 270 | return BluetoothProfile.STATE_DISCONNECTED; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Set priority of the profile |
| 275 | * |
| 276 | * <p> The device should already be paired. |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 277 | * Priority can be one of {@link #PRIORITY_ON} or {@link #PRIORITY_OFF}, |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 278 | * |
| 279 | * @param device Paired bluetooth device |
| 280 | * @param priority |
| 281 | * @return true if priority is set, false on error |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 282 | * @hide |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 283 | */ |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 284 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 285 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 286 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 287 | return setConnectionPolicy(device, BluetoothAdapter.priorityToConnectionPolicy(priority)); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Set connection policy of the profile |
| 292 | * |
| 293 | * <p> The device should already be paired. |
| 294 | * Connection policy can be one of {@link #CONNECTION_POLICY_ALLOWED}, |
| 295 | * {@link #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN} |
| 296 | * |
| 297 | * @param device Paired bluetooth device |
| 298 | * @param connectionPolicy is the connection policy to set to for this profile |
| 299 | * @return true if connectionPolicy is set, false on error |
| 300 | * @hide |
| 301 | */ |
| 302 | @SystemApi |
| 303 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
| 304 | public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) { |
| 305 | if (DBG) log("setConnectionPolicy(" + device + ", " + connectionPolicy + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 306 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 307 | if (service != null && isEnabled() && isValidDevice(device)) { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 308 | if (connectionPolicy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN |
| 309 | && connectionPolicy != BluetoothProfile.CONNECTION_POLICY_ALLOWED) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 310 | return false; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 311 | } |
| 312 | try { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 313 | return service.setConnectionPolicy(device, connectionPolicy); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 314 | } catch (RemoteException e) { |
| 315 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 316 | return false; |
| 317 | } |
| 318 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 319 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 320 | return false; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Get the priority of the profile. |
| 325 | * |
| 326 | * <p> The priority can be any of: |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 327 | * {@link #PRIORITY_OFF}, {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 328 | * |
| 329 | * @param device Bluetooth device |
| 330 | * @return priority of the device |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 331 | * @hide |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 332 | */ |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 333 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 334 | public int getPriority(BluetoothDevice device) { |
| 335 | if (VDBG) log("getPriority(" + device + ")"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 336 | return BluetoothAdapter.connectionPolicyToPriority(getConnectionPolicy(device)); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Get the connection policy of the profile. |
| 341 | * |
| 342 | * <p> The connection policy can be any of: |
| 343 | * {@link #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN}, |
| 344 | * {@link #CONNECTION_POLICY_UNKNOWN} |
| 345 | * |
| 346 | * @param device Bluetooth device |
| 347 | * @return connection policy of the device |
| 348 | * @hide |
| 349 | */ |
| 350 | @SystemApi |
| 351 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 352 | public int getConnectionPolicy(BluetoothDevice device) { |
| 353 | if (VDBG) log("getConnectionPolicy(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 354 | final IBluetoothMap service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 355 | if (service != null && isEnabled() && isValidDevice(device)) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 356 | try { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 357 | return service.getConnectionPolicy(device); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 358 | } catch (RemoteException e) { |
| 359 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 360 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 361 | } |
| 362 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 363 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 364 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 367 | private static void log(String msg) { |
| 368 | Log.d(TAG, msg); |
| 369 | } |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 370 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 371 | private boolean isEnabled() { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 372 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 373 | if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 374 | log("Bluetooth is Not enabled"); |
| 375 | return false; |
| 376 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 377 | private static boolean isValidDevice(BluetoothDevice device) { |
| 378 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 381 | } |