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