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 | |
| 46 | private IBluetoothMap mService; |
| 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 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 59 | final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
| 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()"); |
| 164 | if (mService != null) { |
| 165 | try { |
| 166 | return mService.getState(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 167 | } catch (RemoteException e) { |
| 168 | Log.e(TAG, e.toString()); |
| 169 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 170 | } else { |
| 171 | Log.w(TAG, "Proxy not attached to service"); |
| 172 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 173 | } |
| 174 | return BluetoothMap.STATE_ERROR; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Get the currently connected remote Bluetooth device (PCE). |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 179 | * |
| 180 | * @return The remote Bluetooth device, or null if not in connected or connecting state, or if |
| 181 | * this proxy object is not connected to the Map service. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 182 | */ |
| 183 | public BluetoothDevice getClient() { |
| 184 | if (VDBG) log("getClient()"); |
| 185 | if (mService != null) { |
| 186 | try { |
| 187 | return mService.getClient(); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 188 | } catch (RemoteException e) { |
| 189 | Log.e(TAG, e.toString()); |
| 190 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 191 | } else { |
| 192 | Log.w(TAG, "Proxy not attached to service"); |
| 193 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 194 | } |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | /** |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 199 | * Returns true if the specified Bluetooth device is connected. |
| 200 | * Returns false if not connected, or if this proxy object is not |
| 201 | * currently connected to the Map service. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 202 | */ |
| 203 | public boolean isConnected(BluetoothDevice device) { |
| 204 | if (VDBG) log("isConnected(" + device + ")"); |
| 205 | if (mService != null) { |
| 206 | try { |
| 207 | return mService.isConnected(device); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 208 | } catch (RemoteException e) { |
| 209 | Log.e(TAG, e.toString()); |
| 210 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 211 | } else { |
| 212 | Log.w(TAG, "Proxy not attached to service"); |
| 213 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | /** |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 219 | * Initiate connection. Initiation of outgoing connections is not |
| 220 | * supported for MAP server. |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 221 | */ |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 222 | public boolean connect(BluetoothDevice device) { |
| 223 | if (DBG) log("connect(" + device + ")" + "not supported for MAPS"); |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Initiate disconnect. |
| 229 | * |
| 230 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 231 | * @return false on error, true otherwise |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 232 | */ |
| 233 | public boolean disconnect(BluetoothDevice device) { |
| 234 | if (DBG) log("disconnect(" + device + ")"); |
| 235 | if (mService != null && isEnabled() && |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 236 | isValidDevice(device)) { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 237 | try { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 238 | return mService.disconnect(device); |
| 239 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 240 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 241 | return false; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 242 | } |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 243 | } |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 244 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Check class bits for possible Map support. |
| 250 | * This is a simple heuristic that tries to guess if a device with the |
| 251 | * given class bits might support Map. It is not accurate for all |
| 252 | * devices. It tries to err on the side of false positives. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 253 | * |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 254 | * @return True if this device might support Map. |
| 255 | */ |
| 256 | public static boolean doesClassMatchSink(BluetoothClass btClass) { |
| 257 | // TODO optimize the rule |
| 258 | switch (btClass.getDeviceClass()) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 259 | case BluetoothClass.Device.COMPUTER_DESKTOP: |
| 260 | case BluetoothClass.Device.COMPUTER_LAPTOP: |
| 261 | case BluetoothClass.Device.COMPUTER_SERVER: |
| 262 | case BluetoothClass.Device.COMPUTER_UNCATEGORIZED: |
| 263 | return true; |
| 264 | default: |
| 265 | return false; |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 269 | /** |
| 270 | * Get the list of connected devices. Currently at most one. |
| 271 | * |
| 272 | * @return list of connected devices |
| 273 | */ |
| 274 | public List<BluetoothDevice> getConnectedDevices() { |
| 275 | if (DBG) log("getConnectedDevices()"); |
| 276 | if (mService != null && isEnabled()) { |
| 277 | try { |
| 278 | return mService.getConnectedDevices(); |
| 279 | } catch (RemoteException e) { |
| 280 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 281 | return new ArrayList<BluetoothDevice>(); |
| 282 | } |
| 283 | } |
| 284 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 285 | return new ArrayList<BluetoothDevice>(); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Get the list of devices matching specified states. Currently at most one. |
| 290 | * |
| 291 | * @return list of matching devices |
| 292 | */ |
| 293 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 294 | if (DBG) log("getDevicesMatchingStates()"); |
| 295 | if (mService != null && isEnabled()) { |
| 296 | try { |
| 297 | return mService.getDevicesMatchingConnectionStates(states); |
| 298 | } catch (RemoteException e) { |
| 299 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 300 | return new ArrayList<BluetoothDevice>(); |
| 301 | } |
| 302 | } |
| 303 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 304 | return new ArrayList<BluetoothDevice>(); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Get connection state of device |
| 309 | * |
| 310 | * @return device connection state |
| 311 | */ |
| 312 | public int getConnectionState(BluetoothDevice device) { |
| 313 | if (DBG) log("getConnectionState(" + device + ")"); |
| 314 | if (mService != null && isEnabled() && |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 315 | isValidDevice(device)) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 316 | try { |
| 317 | return mService.getConnectionState(device); |
| 318 | } catch (RemoteException e) { |
| 319 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 320 | return BluetoothProfile.STATE_DISCONNECTED; |
| 321 | } |
| 322 | } |
| 323 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 324 | return BluetoothProfile.STATE_DISCONNECTED; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Set priority of the profile |
| 329 | * |
| 330 | * <p> The device should already be paired. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 331 | * Priority can be one of {@link #PRIORITY_ON} or |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 332 | * {@link #PRIORITY_OFF}, |
| 333 | * |
| 334 | * @param device Paired bluetooth device |
| 335 | * @param priority |
| 336 | * @return true if priority is set, false on error |
| 337 | */ |
| 338 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 339 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
| 340 | if (mService != null && isEnabled() && |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 341 | isValidDevice(device)) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 342 | if (priority != BluetoothProfile.PRIORITY_OFF && |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 343 | priority != BluetoothProfile.PRIORITY_ON) { |
| 344 | return false; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 345 | } |
| 346 | try { |
| 347 | return mService.setPriority(device, priority); |
| 348 | } catch (RemoteException e) { |
| 349 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 350 | return false; |
| 351 | } |
| 352 | } |
| 353 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Get the priority of the profile. |
| 359 | * |
| 360 | * <p> The priority can be any of: |
| 361 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 362 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 363 | * |
| 364 | * @param device Bluetooth device |
| 365 | * @return priority of the device |
| 366 | */ |
| 367 | public int getPriority(BluetoothDevice device) { |
| 368 | if (VDBG) log("getPriority(" + device + ")"); |
| 369 | if (mService != null && isEnabled() && |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 370 | isValidDevice(device)) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 371 | try { |
| 372 | return mService.getPriority(device); |
| 373 | } catch (RemoteException e) { |
| 374 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 375 | return PRIORITY_OFF; |
| 376 | } |
| 377 | } |
| 378 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 379 | return PRIORITY_OFF; |
| 380 | } |
| 381 | |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 382 | private final ServiceConnection mConnection = new ServiceConnection() { |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 383 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 384 | if (DBG) log("Proxy object connected"); |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 385 | mService = IBluetoothMap.Stub.asInterface(Binder.allowBlocking(service)); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 386 | if (mServiceListener != null) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 387 | mServiceListener.onServiceConnected(BluetoothProfile.MAP, BluetoothMap.this); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 390 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 391 | public void onServiceDisconnected(ComponentName className) { |
| 392 | if (DBG) log("Proxy object disconnected"); |
| 393 | mService = null; |
| 394 | if (mServiceListener != null) { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 395 | mServiceListener.onServiceDisconnected(BluetoothProfile.MAP); |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | }; |
| 399 | |
| 400 | private static void log(String msg) { |
| 401 | Log.d(TAG, msg); |
| 402 | } |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 403 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 404 | private boolean isEnabled() { |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 405 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 406 | if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 407 | log("Bluetooth is Not enabled"); |
| 408 | return false; |
| 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 isValidDevice(BluetoothDevice device) { |
| 412 | if (device == null) return false; |
| 413 | |
| 414 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 415 | return false; |
Kim Schulz | 0d37605 | 2013-08-22 11:18:02 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | |
Matthew Xie | fe3807a | 2013-07-18 17:31:50 -0700 | [diff] [blame] | 419 | } |