Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 20 | import android.app.PendingIntent; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
| 25 | import android.net.Uri; |
| 26 | import android.os.IBinder; |
| 27 | import android.os.RemoteException; |
| 28 | import android.util.Log; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * This class provides the APIs to control the Bluetooth MAP MCE Profile. |
| 35 | * |
| 36 | * @hide |
| 37 | */ |
| 38 | public final class BluetoothMapClient implements BluetoothProfile { |
| 39 | |
| 40 | private static final String TAG = "BluetoothMapClient"; |
| 41 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
| 42 | private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE); |
| 43 | |
| 44 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| 45 | "android.bluetooth.mapmce.profile.action.CONNECTION_STATE_CHANGED"; |
| 46 | public static final String ACTION_MESSAGE_RECEIVED = |
| 47 | "android.bluetooth.mapmce.profile.action.MESSAGE_RECEIVED"; |
| 48 | /* Actions to be used for pending intents */ |
| 49 | public static final String ACTION_MESSAGE_SENT_SUCCESSFULLY = |
| 50 | "android.bluetooth.mapmce.profile.action.MESSAGE_SENT_SUCCESSFULLY"; |
| 51 | public static final String ACTION_MESSAGE_DELIVERED_SUCCESSFULLY = |
| 52 | "android.bluetooth.mapmce.profile.action.MESSAGE_DELIVERED_SUCCESSFULLY"; |
| 53 | |
Srinivas Visvanathan | 86a8c1c | 2017-03-07 10:22:53 -0800 | [diff] [blame] | 54 | /* Extras used in ACTION_MESSAGE_RECEIVED intent. |
| 55 | * NOTE: HANDLE is only valid for a single session with the device. */ |
| 56 | public static final String EXTRA_MESSAGE_HANDLE = |
| 57 | "android.bluetooth.mapmce.profile.extra.MESSAGE_HANDLE"; |
Srinivas Visvanathan | ad90338 | 2017-03-03 09:57:18 -0800 | [diff] [blame] | 58 | public static final String EXTRA_SENDER_CONTACT_URI = |
| 59 | "android.bluetooth.mapmce.profile.extra.SENDER_CONTACT_URI"; |
| 60 | public static final String EXTRA_SENDER_CONTACT_NAME = |
| 61 | "android.bluetooth.mapmce.profile.extra.SENDER_CONTACT_NAME"; |
| 62 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 63 | private volatile IBluetoothMapClient mService; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 64 | private final Context mContext; |
| 65 | private ServiceListener mServiceListener; |
| 66 | private BluetoothAdapter mAdapter; |
| 67 | |
| 68 | /** There was an error trying to obtain the state */ |
| 69 | public static final int STATE_ERROR = -1; |
| 70 | |
| 71 | public static final int RESULT_FAILURE = 0; |
| 72 | public static final int RESULT_SUCCESS = 1; |
| 73 | /** Connection canceled before completion. */ |
| 74 | public static final int RESULT_CANCELED = 2; |
| 75 | |
Vasu Nori | 694752d | 2018-08-17 17:25:28 -0700 | [diff] [blame] | 76 | private static final int UPLOADING_FEATURE_BITMASK = 0x08; |
| 77 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 78 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 79 | new IBluetoothStateChangeCallback.Stub() { |
| 80 | public void onBluetoothStateChange(boolean up) { |
| 81 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 82 | if (!up) { |
| 83 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
| 84 | synchronized (mConnection) { |
| 85 | try { |
| 86 | mService = null; |
| 87 | mContext.unbindService(mConnection); |
| 88 | } catch (Exception re) { |
| 89 | Log.e(TAG, "", re); |
| 90 | } |
| 91 | } |
| 92 | } else { |
| 93 | synchronized (mConnection) { |
| 94 | try { |
| 95 | if (mService == null) { |
| 96 | if (VDBG) Log.d(TAG, "Binding service..."); |
| 97 | doBind(); |
| 98 | } |
| 99 | } catch (Exception re) { |
| 100 | Log.e(TAG, "", re); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * Create a BluetoothMapClient proxy object. |
| 109 | */ |
| 110 | /*package*/ BluetoothMapClient(Context context, ServiceListener l) { |
| 111 | if (DBG) Log.d(TAG, "Create BluetoothMapClient proxy object"); |
| 112 | mContext = context; |
| 113 | mServiceListener = l; |
| 114 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 115 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 116 | if (mgr != null) { |
| 117 | try { |
| 118 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 119 | } catch (RemoteException e) { |
| 120 | Log.e(TAG, "", e); |
| 121 | } |
| 122 | } |
| 123 | doBind(); |
| 124 | } |
| 125 | |
| 126 | boolean doBind() { |
| 127 | Intent intent = new Intent(IBluetoothMapClient.class.getName()); |
| 128 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 129 | intent.setComponent(comp); |
| 130 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 131 | mContext.getUser())) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 132 | Log.e(TAG, "Could not bind to Bluetooth MAP MCE Service with " + intent); |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | protected void finalize() throws Throwable { |
| 139 | try { |
| 140 | close(); |
| 141 | } finally { |
| 142 | super.finalize(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Close the connection to the backing service. |
| 148 | * Other public functions of BluetoothMap will return default error |
| 149 | * results once close() has been called. Multiple invocations of close() |
| 150 | * are ok. |
| 151 | */ |
| 152 | public void close() { |
| 153 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 154 | if (mgr != null) { |
| 155 | try { |
| 156 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 157 | } catch (Exception e) { |
| 158 | Log.e(TAG, "", e); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | synchronized (mConnection) { |
| 163 | if (mService != null) { |
| 164 | try { |
| 165 | mService = null; |
| 166 | mContext.unbindService(mConnection); |
| 167 | } catch (Exception re) { |
| 168 | Log.e(TAG, "", re); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | mServiceListener = null; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns true if the specified Bluetooth device is connected. |
| 177 | * Returns false if not connected, or if this proxy object is not |
| 178 | * currently connected to the Map service. |
| 179 | */ |
| 180 | public boolean isConnected(BluetoothDevice device) { |
| 181 | if (VDBG) Log.d(TAG, "isConnected(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 182 | final IBluetoothMapClient service = mService; |
| 183 | if (service != null) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 184 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 185 | return service.isConnected(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 186 | } catch (RemoteException e) { |
| 187 | Log.e(TAG, e.toString()); |
| 188 | } |
| 189 | } else { |
| 190 | Log.w(TAG, "Proxy not attached to service"); |
| 191 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Initiate connection. Initiation of outgoing connections is not |
| 198 | * supported for MAP server. |
| 199 | */ |
| 200 | public boolean connect(BluetoothDevice device) { |
| 201 | if (DBG) Log.d(TAG, "connect(" + device + ")" + "for MAPS MCE"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 202 | final IBluetoothMapClient service = mService; |
| 203 | if (service != null) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 204 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 205 | return service.connect(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 206 | } catch (RemoteException e) { |
| 207 | Log.e(TAG, e.toString()); |
| 208 | } |
| 209 | } else { |
| 210 | Log.w(TAG, "Proxy not attached to service"); |
| 211 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Initiate disconnect. |
| 218 | * |
| 219 | * @param device Remote Bluetooth Device |
| 220 | * @return false on error, true otherwise |
| 221 | */ |
| 222 | public boolean disconnect(BluetoothDevice device) { |
| 223 | if (DBG) Log.d(TAG, "disconnect(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 224 | final IBluetoothMapClient service = mService; |
| 225 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 226 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 227 | return service.disconnect(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 228 | } catch (RemoteException e) { |
| 229 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 230 | } |
| 231 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 232 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Get the list of connected devices. Currently at most one. |
| 238 | * |
| 239 | * @return list of connected devices |
| 240 | */ |
| 241 | @Override |
| 242 | public List<BluetoothDevice> getConnectedDevices() { |
| 243 | if (DBG) Log.d(TAG, "getConnectedDevices()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 244 | final IBluetoothMapClient service = mService; |
| 245 | if (service != null && isEnabled()) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 246 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 247 | return service.getConnectedDevices(); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 248 | } catch (RemoteException e) { |
| 249 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 250 | return new ArrayList<>(); |
| 251 | } |
| 252 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 253 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 254 | return new ArrayList<>(); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get the list of devices matching specified states. Currently at most one. |
| 259 | * |
| 260 | * @return list of matching devices |
| 261 | */ |
| 262 | @Override |
| 263 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 264 | if (DBG) Log.d(TAG, "getDevicesMatchingStates()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 265 | final IBluetoothMapClient service = mService; |
| 266 | if (service != null && isEnabled()) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 267 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 268 | return service.getDevicesMatchingConnectionStates(states); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 269 | } catch (RemoteException e) { |
| 270 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 271 | return new ArrayList<>(); |
| 272 | } |
| 273 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 274 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 275 | return new ArrayList<>(); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Get connection state of device |
| 280 | * |
| 281 | * @return device connection state |
| 282 | */ |
| 283 | @Override |
| 284 | public int getConnectionState(BluetoothDevice device) { |
| 285 | if (DBG) Log.d(TAG, "getConnectionState(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 286 | final IBluetoothMapClient service = mService; |
| 287 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 288 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 289 | return service.getConnectionState(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 290 | } catch (RemoteException e) { |
| 291 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 292 | return BluetoothProfile.STATE_DISCONNECTED; |
| 293 | } |
| 294 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 295 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 296 | return BluetoothProfile.STATE_DISCONNECTED; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Set priority of the profile |
| 301 | * |
| 302 | * <p> The device should already be paired. Priority can be one of {@link #PRIORITY_ON} or |
| 303 | * {@link #PRIORITY_OFF}, |
| 304 | * |
| 305 | * @param device Paired bluetooth device |
| 306 | * @return true if priority is set, false on error |
| 307 | */ |
| 308 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 309 | if (DBG) Log.d(TAG, "setPriority(" + device + ", " + priority + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 310 | final IBluetoothMapClient service = mService; |
| 311 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 312 | if (priority != BluetoothProfile.PRIORITY_OFF |
| 313 | && priority != BluetoothProfile.PRIORITY_ON) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 314 | return false; |
| 315 | } |
| 316 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 317 | return service.setPriority(device, priority); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 318 | } catch (RemoteException e) { |
| 319 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 320 | return false; |
| 321 | } |
| 322 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 323 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 324 | return false; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Get the priority of the profile. |
| 329 | * |
| 330 | * <p> The priority can be any of: |
| 331 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 332 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 333 | * |
| 334 | * @param device Bluetooth device |
| 335 | * @return priority of the device |
| 336 | */ |
| 337 | public int getPriority(BluetoothDevice device) { |
| 338 | if (VDBG) Log.d(TAG, "getPriority(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 339 | final IBluetoothMapClient service = mService; |
| 340 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 341 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 342 | return service.getPriority(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 343 | } catch (RemoteException e) { |
| 344 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 345 | return PRIORITY_OFF; |
| 346 | } |
| 347 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 348 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 349 | return PRIORITY_OFF; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Send a message. |
| 354 | * |
| 355 | * Send an SMS message to either the contacts primary number or the telephone number specified. |
| 356 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 357 | * @param device Bluetooth device |
| 358 | * @param contacts Uri[] of the contacts |
| 359 | * @param message Message to be sent |
| 360 | * @param sentIntent intent issued when message is sent |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 361 | * @param deliveredIntent intent issued when message is delivered |
| 362 | * @return true if the message is enqueued, false on error |
| 363 | */ |
Mathew Inwood | 4dc66d3 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 364 | @UnsupportedAppUsage |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 365 | public boolean sendMessage(BluetoothDevice device, Uri[] contacts, String message, |
| 366 | PendingIntent sentIntent, PendingIntent deliveredIntent) { |
| 367 | if (DBG) Log.d(TAG, "sendMessage(" + device + ", " + contacts + ", " + message); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 368 | final IBluetoothMapClient service = mService; |
| 369 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 370 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 371 | return service.sendMessage(device, contacts, message, sentIntent, deliveredIntent); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 372 | } catch (RemoteException e) { |
| 373 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 374 | return false; |
| 375 | } |
| 376 | } |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | /** |
Joseph Pirozzo | b8fc067 | 2016-10-06 11:44:53 -0700 | [diff] [blame] | 381 | * Get unread messages. Unread messages will be published via {@link #ACTION_MESSAGE_RECEIVED}. |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 382 | * |
| 383 | * @param device Bluetooth device |
| 384 | * @return true if the message is enqueued, false on error |
| 385 | */ |
| 386 | public boolean getUnreadMessages(BluetoothDevice device) { |
| 387 | if (DBG) Log.d(TAG, "getUnreadMessages(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 388 | final IBluetoothMapClient service = mService; |
| 389 | if (service != null && isEnabled() && isValidDevice(device)) { |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 390 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 391 | return service.getUnreadMessages(device); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 392 | } catch (RemoteException e) { |
| 393 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 394 | return false; |
| 395 | } |
| 396 | } |
| 397 | return false; |
| 398 | } |
| 399 | |
Vasu Nori | 694752d | 2018-08-17 17:25:28 -0700 | [diff] [blame] | 400 | /** |
| 401 | * Returns the "Uploading" feature bit value from the SDP record's |
| 402 | * MapSupportedFeatures field (see Bluetooth MAP 1.4 spec, page 114). |
| 403 | * @param device The Bluetooth device to get this value for. |
| 404 | * @return Returns true if the Uploading bit value in SDP record's |
| 405 | * MapSupportedFeatures field is set. False is returned otherwise. |
| 406 | */ |
| 407 | public boolean isUploadingSupported(BluetoothDevice device) { |
| 408 | try { |
| 409 | return (mService != null && isEnabled() && isValidDevice(device)) |
| 410 | && ((mService.getSupportedFeatures(device) & UPLOADING_FEATURE_BITMASK) > 0); |
| 411 | } catch (RemoteException e) { |
| 412 | Log.e(TAG, e.getMessage()); |
| 413 | } |
| 414 | return false; |
| 415 | } |
| 416 | |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 417 | private final ServiceConnection mConnection = new ServiceConnection() { |
| 418 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 419 | if (DBG) Log.d(TAG, "Proxy object connected"); |
| 420 | mService = IBluetoothMapClient.Stub.asInterface(service); |
| 421 | if (mServiceListener != null) { |
| 422 | mServiceListener.onServiceConnected(BluetoothProfile.MAP_CLIENT, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 423 | BluetoothMapClient.this); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | public void onServiceDisconnected(ComponentName className) { |
| 428 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 429 | mService = null; |
| 430 | if (mServiceListener != null) { |
| 431 | mServiceListener.onServiceDisconnected(BluetoothProfile.MAP_CLIENT); |
| 432 | } |
| 433 | } |
| 434 | }; |
| 435 | |
| 436 | private boolean isEnabled() { |
| 437 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 438 | if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 439 | if (DBG) Log.d(TAG, "Bluetooth is Not enabled"); |
| 440 | return false; |
| 441 | } |
| 442 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 443 | private static boolean isValidDevice(BluetoothDevice device) { |
| 444 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 447 | } |