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 | |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.ServiceConnection; |
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; |
jovanak | 2a9131f | 2018-10-15 17:50:19 -0700 | [diff] [blame] | 27 | import android.os.UserHandle; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 33 | /** |
| 34 | * This class provides the APIs to control the Bluetooth MAP |
| 35 | * Profile. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 36 | * |
| 37 | * @hide |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 38 | */ |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 39 | public final class BluetoothMap implements BluetoothProfile { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 40 | |
| 41 | private static final String TAG = "BluetoothMap"; |
| 42 | private static final boolean DBG = true; |
| 43 | private static final boolean VDBG = false; |
| 44 | |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 45 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 46 | "android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED"; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 47 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 48 | private volatile IBluetoothMap mService; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 49 | private final Context mContext; |
| 50 | private ServiceListener mServiceListener; |
| 51 | private BluetoothAdapter mAdapter; |
| 52 | |
| 53 | /** There was an error trying to obtain the state */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 54 | public static final int STATE_ERROR = -1; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 55 | |
| 56 | public static final int RESULT_FAILURE = 0; |
| 57 | public static final int RESULT_SUCCESS = 1; |
| 58 | /** Connection canceled before completion. */ |
| 59 | public static final int RESULT_CANCELED = 2; |
| 60 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 61 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 62 | new IBluetoothStateChangeCallback.Stub() { |
| 63 | public void onBluetoothStateChange(boolean up) { |
| 64 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 65 | if (!up) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 66 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 67 | synchronized (mConnection) { |
| 68 | try { |
| 69 | mService = null; |
| 70 | mContext.unbindService(mConnection); |
| 71 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 72 | Log.e(TAG, "", re); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } else { |
| 76 | synchronized (mConnection) { |
| 77 | try { |
| 78 | if (mService == null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 79 | if (VDBG) Log.d(TAG, "Binding service..."); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 80 | doBind(); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 81 | } |
| 82 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 83 | Log.e(TAG, "", re); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 88 | }; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Create a BluetoothMap proxy object. |
| 92 | */ |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 93 | /*package*/ BluetoothMap(Context context, ServiceListener l) { |
| 94 | if (DBG) Log.d(TAG, "Create BluetoothMap proxy object"); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 95 | mContext = context; |
| 96 | mServiceListener = l; |
| 97 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 98 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 99 | if (mgr != null) { |
| 100 | try { |
| 101 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 102 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 103 | Log.e(TAG, "", e); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 106 | doBind(); |
| 107 | } |
| 108 | |
| 109 | boolean doBind() { |
| 110 | Intent intent = new Intent(IBluetoothMap.class.getName()); |
| 111 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 112 | intent.setComponent(comp); |
Dianne Hackborn | 466ce96 | 2014-03-19 18:06:58 -0700 | [diff] [blame] | 113 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
jovanak | 2a9131f | 2018-10-15 17:50:19 -0700 | [diff] [blame] | 114 | UserHandle.CURRENT_OR_SELF)) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 115 | Log.e(TAG, "Could not bind to Bluetooth MAP Service with " + intent); |
| 116 | return false; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 117 | } |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 118 | return true; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | protected void finalize() throws Throwable { |
| 122 | try { |
| 123 | close(); |
| 124 | } finally { |
| 125 | super.finalize(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Close the connection to the backing service. |
| 131 | * Other public functions of BluetoothMap will return default error |
| 132 | * results once close() has been called. Multiple invocations of close() |
| 133 | * are ok. |
| 134 | */ |
| 135 | public synchronized void close() { |
| 136 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 137 | if (mgr != null) { |
| 138 | try { |
| 139 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 140 | } catch (Exception e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 141 | Log.e(TAG, "", e); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | synchronized (mConnection) { |
| 146 | if (mService != null) { |
| 147 | try { |
| 148 | mService = null; |
| 149 | mContext.unbindService(mConnection); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 150 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 151 | Log.e(TAG, "", re); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | mServiceListener = null; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get the current state of the BluetoothMap service. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 160 | * |
| 161 | * @return One of the STATE_ return codes, or STATE_ERROR if this proxy object is currently not |
| 162 | * connected to the Map service. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 163 | */ |
| 164 | public int getState() { |
| 165 | if (VDBG) log("getState()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 166 | final IBluetoothMap service = mService; |
| 167 | if (service != null) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 168 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 169 | return service.getState(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 170 | } catch (RemoteException e) { |
| 171 | Log.e(TAG, e.toString()); |
| 172 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 173 | } else { |
| 174 | Log.w(TAG, "Proxy not attached to service"); |
| 175 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 176 | } |
| 177 | return BluetoothMap.STATE_ERROR; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Get the currently connected remote Bluetooth device (PCE). |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 182 | * |
| 183 | * @return The remote Bluetooth device, or null if not in connected or connecting state, or if |
| 184 | * this proxy object is not connected to the Map service. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 185 | */ |
| 186 | public BluetoothDevice getClient() { |
| 187 | if (VDBG) log("getClient()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 188 | final IBluetoothMap service = mService; |
| 189 | if (service != null) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 190 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 191 | return service.getClient(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 192 | } catch (RemoteException e) { |
| 193 | Log.e(TAG, e.toString()); |
| 194 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 195 | } else { |
| 196 | Log.w(TAG, "Proxy not attached to service"); |
| 197 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 198 | } |
| 199 | return null; |
| 200 | } |
| 201 | |
| 202 | /** |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 203 | * Returns true if the specified Bluetooth device is connected. |
| 204 | * Returns false if not connected, or if this proxy object is not |
| 205 | * currently connected to the Map service. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 206 | */ |
| 207 | public boolean isConnected(BluetoothDevice device) { |
| 208 | if (VDBG) log("isConnected(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 209 | final IBluetoothMap service = mService; |
| 210 | if (service != null) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 211 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 212 | return service.isConnected(device); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 213 | } catch (RemoteException e) { |
| 214 | Log.e(TAG, e.toString()); |
| 215 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 216 | } else { |
| 217 | Log.w(TAG, "Proxy not attached to service"); |
| 218 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | /** |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 224 | * Initiate connection. Initiation of outgoing connections is not |
| 225 | * supported for MAP server. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 226 | */ |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 227 | public boolean connect(BluetoothDevice device) { |
| 228 | if (DBG) log("connect(" + device + ")" + "not supported for MAPS"); |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Initiate disconnect. |
| 234 | * |
| 235 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 236 | * @return false on error, true otherwise |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 237 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 238 | @UnsupportedAppUsage |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 239 | public boolean disconnect(BluetoothDevice device) { |
| 240 | if (DBG) log("disconnect(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 241 | final IBluetoothMap service = mService; |
| 242 | if (service != null && isEnabled() && isValidDevice(device)) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 243 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 244 | return service.disconnect(device); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 245 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 246 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 247 | return false; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 248 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 249 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 250 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Check class bits for possible Map support. |
| 256 | * This is a simple heuristic that tries to guess if a device with the |
| 257 | * given class bits might support Map. It is not accurate for all |
| 258 | * devices. It tries to err on the side of false positives. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 259 | * |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 260 | * @return True if this device might support Map. |
| 261 | */ |
| 262 | public static boolean doesClassMatchSink(BluetoothClass btClass) { |
| 263 | // TODO optimize the rule |
| 264 | switch (btClass.getDeviceClass()) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 265 | case BluetoothClass.Device.COMPUTER_DESKTOP: |
| 266 | case BluetoothClass.Device.COMPUTER_LAPTOP: |
| 267 | case BluetoothClass.Device.COMPUTER_SERVER: |
| 268 | case BluetoothClass.Device.COMPUTER_UNCATEGORIZED: |
| 269 | return true; |
| 270 | default: |
| 271 | return false; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 275 | /** |
| 276 | * Get the list of connected devices. Currently at most one. |
| 277 | * |
| 278 | * @return list of connected devices |
| 279 | */ |
| 280 | public List<BluetoothDevice> getConnectedDevices() { |
| 281 | if (DBG) log("getConnectedDevices()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 282 | final IBluetoothMap service = mService; |
| 283 | if (service != null && isEnabled()) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 284 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 285 | return service.getConnectedDevices(); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 286 | } catch (RemoteException e) { |
| 287 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 288 | return new ArrayList<BluetoothDevice>(); |
| 289 | } |
| 290 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 291 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 292 | return new ArrayList<BluetoothDevice>(); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Get the list of devices matching specified states. Currently at most one. |
| 297 | * |
| 298 | * @return list of matching devices |
| 299 | */ |
| 300 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 301 | if (DBG) log("getDevicesMatchingStates()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 302 | final IBluetoothMap service = mService; |
| 303 | if (service != null && isEnabled()) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 304 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 305 | return service.getDevicesMatchingConnectionStates(states); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 306 | } catch (RemoteException e) { |
| 307 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 308 | return new ArrayList<BluetoothDevice>(); |
| 309 | } |
| 310 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 311 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 312 | return new ArrayList<BluetoothDevice>(); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Get connection state of device |
| 317 | * |
| 318 | * @return device connection state |
| 319 | */ |
| 320 | public int getConnectionState(BluetoothDevice device) { |
| 321 | if (DBG) log("getConnectionState(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 322 | final IBluetoothMap service = mService; |
| 323 | if (service != null && isEnabled() && isValidDevice(device)) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 324 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 325 | return service.getConnectionState(device); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 326 | } catch (RemoteException e) { |
| 327 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 328 | return BluetoothProfile.STATE_DISCONNECTED; |
| 329 | } |
| 330 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 331 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 332 | return BluetoothProfile.STATE_DISCONNECTED; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Set priority of the profile |
| 337 | * |
| 338 | * <p> The device should already be paired. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 339 | * Priority can be one of {@link #PRIORITY_ON} or |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 340 | * {@link #PRIORITY_OFF}, |
| 341 | * |
| 342 | * @param device Paired bluetooth device |
| 343 | * @param priority |
| 344 | * @return true if priority is set, false on error |
| 345 | */ |
| 346 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 347 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 348 | final IBluetoothMap service = mService; |
| 349 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 350 | if (priority != BluetoothProfile.PRIORITY_OFF |
| 351 | && priority != BluetoothProfile.PRIORITY_ON) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 352 | return false; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 353 | } |
| 354 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 355 | return service.setPriority(device, priority); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 356 | } catch (RemoteException e) { |
| 357 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 358 | return false; |
| 359 | } |
| 360 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 361 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 362 | return false; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Get the priority of the profile. |
| 367 | * |
| 368 | * <p> The priority can be any of: |
| 369 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 370 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 371 | * |
| 372 | * @param device Bluetooth device |
| 373 | * @return priority of the device |
| 374 | */ |
| 375 | public int getPriority(BluetoothDevice device) { |
| 376 | if (VDBG) log("getPriority(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 377 | final IBluetoothMap service = mService; |
| 378 | if (service != null && isEnabled() && isValidDevice(device)) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 379 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 380 | return service.getPriority(device); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 381 | } catch (RemoteException e) { |
| 382 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 383 | return PRIORITY_OFF; |
| 384 | } |
| 385 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 386 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 387 | return PRIORITY_OFF; |
| 388 | } |
| 389 | |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 390 | private final ServiceConnection mConnection = new ServiceConnection() { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 391 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 392 | if (DBG) log("Proxy object connected"); |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 393 | mService = IBluetoothMap.Stub.asInterface(Binder.allowBlocking(service)); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 394 | if (mServiceListener != null) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 395 | mServiceListener.onServiceConnected(BluetoothProfile.MAP, BluetoothMap.this); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 398 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 399 | public void onServiceDisconnected(ComponentName className) { |
| 400 | if (DBG) log("Proxy object disconnected"); |
| 401 | mService = null; |
| 402 | if (mServiceListener != null) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 403 | mServiceListener.onServiceDisconnected(BluetoothProfile.MAP); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | }; |
| 407 | |
| 408 | private static void log(String msg) { |
| 409 | Log.d(TAG, msg); |
| 410 | } |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 411 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 412 | private boolean isEnabled() { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 413 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 414 | if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 415 | log("Bluetooth is Not enabled"); |
| 416 | return false; |
| 417 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 418 | private static boolean isValidDevice(BluetoothDevice device) { |
| 419 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 422 | } |