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