blob: 5b55b23680c37d8f6a4cf6aa67766ba9983cba0d [file] [log] [blame]
Matthew Xiefe3807a2013-07-18 17:31:50 -07001/*
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
17package android.bluetooth;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
Jack Hea355e5e2017-08-22 16:06:54 -070023import android.os.Binder;
24import android.os.IBinder;
25import android.os.RemoteException;
Matthew Xiefe3807a2013-07-18 17:31:50 -070026import android.util.Log;
27
Jack Hea355e5e2017-08-22 16:06:54 -070028import java.util.ArrayList;
29import java.util.List;
30
Matthew Xiefe3807a2013-07-18 17:31:50 -070031/**
32 * This class provides the APIs to control the Bluetooth MAP
33 * Profile.
Jack Hea355e5e2017-08-22 16:06:54 -070034 *
35 * @hide
Matthew Xiefe3807a2013-07-18 17:31:50 -070036 */
Kim Schulz0d376052013-08-22 11:18:02 +020037public final class BluetoothMap implements BluetoothProfile {
Matthew Xiefe3807a2013-07-18 17:31:50 -070038
39 private static final String TAG = "BluetoothMap";
40 private static final boolean DBG = true;
41 private static final boolean VDBG = false;
42
Kim Schulz0d376052013-08-22 11:18:02 +020043 public static final String ACTION_CONNECTION_STATE_CHANGED =
Jack Hea355e5e2017-08-22 16:06:54 -070044 "android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED";
Matthew Xiefe3807a2013-07-18 17:31:50 -070045
Jack He16eeac32017-08-17 12:11:18 -070046 private volatile IBluetoothMap mService;
Matthew Xiefe3807a2013-07-18 17:31:50 -070047 private final Context mContext;
48 private ServiceListener mServiceListener;
49 private BluetoothAdapter mAdapter;
50
51 /** There was an error trying to obtain the state */
Jack Hea355e5e2017-08-22 16:06:54 -070052 public static final int STATE_ERROR = -1;
Matthew Xiefe3807a2013-07-18 17:31:50 -070053
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 He2992cd02017-08-22 21:21:23 -070059 private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
Matthew Xiefe3807a2013-07-18 17:31:50 -070060 new IBluetoothStateChangeCallback.Stub() {
61 public void onBluetoothStateChange(boolean up) {
62 if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
63 if (!up) {
Jack Hea355e5e2017-08-22 16:06:54 -070064 if (VDBG) Log.d(TAG, "Unbinding service...");
Matthew Xiefe3807a2013-07-18 17:31:50 -070065 synchronized (mConnection) {
66 try {
67 mService = null;
68 mContext.unbindService(mConnection);
69 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -070070 Log.e(TAG, "", re);
Matthew Xiefe3807a2013-07-18 17:31:50 -070071 }
72 }
73 } else {
74 synchronized (mConnection) {
75 try {
76 if (mService == null) {
Jack Hea355e5e2017-08-22 16:06:54 -070077 if (VDBG) Log.d(TAG, "Binding service...");
Kim Schulz0d376052013-08-22 11:18:02 +020078 doBind();
Matthew Xiefe3807a2013-07-18 17:31:50 -070079 }
80 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -070081 Log.e(TAG, "", re);
Matthew Xiefe3807a2013-07-18 17:31:50 -070082 }
83 }
84 }
85 }
Jack Hea355e5e2017-08-22 16:06:54 -070086 };
Matthew Xiefe3807a2013-07-18 17:31:50 -070087
88 /**
89 * Create a BluetoothMap proxy object.
90 */
Kim Schulz0d376052013-08-22 11:18:02 +020091 /*package*/ BluetoothMap(Context context, ServiceListener l) {
92 if (DBG) Log.d(TAG, "Create BluetoothMap proxy object");
Matthew Xiefe3807a2013-07-18 17:31:50 -070093 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 Hea355e5e2017-08-22 16:06:54 -0700101 Log.e(TAG, "", e);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700102 }
103 }
Kim Schulz0d376052013-08-22 11:18:02 +0200104 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 Hackborn466ce962014-03-19 18:06:58 -0700111 if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
112 android.os.Process.myUserHandle())) {
Kim Schulz0d376052013-08-22 11:18:02 +0200113 Log.e(TAG, "Could not bind to Bluetooth MAP Service with " + intent);
114 return false;
Matthew Xiefe3807a2013-07-18 17:31:50 -0700115 }
Kim Schulz0d376052013-08-22 11:18:02 +0200116 return true;
Matthew Xiefe3807a2013-07-18 17:31:50 -0700117 }
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 Hea355e5e2017-08-22 16:06:54 -0700139 Log.e(TAG, "", e);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700140 }
141 }
142
143 synchronized (mConnection) {
144 if (mService != null) {
145 try {
146 mService = null;
147 mContext.unbindService(mConnection);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700148 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -0700149 Log.e(TAG, "", re);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700150 }
151 }
152 }
153 mServiceListener = null;
154 }
155
156 /**
157 * Get the current state of the BluetoothMap service.
Jack Hea355e5e2017-08-22 16:06:54 -0700158 *
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 Xiefe3807a2013-07-18 17:31:50 -0700161 */
162 public int getState() {
163 if (VDBG) log("getState()");
Jack He16eeac32017-08-17 12:11:18 -0700164 final IBluetoothMap service = mService;
165 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700166 try {
Jack He16eeac32017-08-17 12:11:18 -0700167 return service.getState();
Jack Hea355e5e2017-08-22 16:06:54 -0700168 } catch (RemoteException e) {
169 Log.e(TAG, e.toString());
170 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700171 } 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 Hea355e5e2017-08-22 16:06:54 -0700180 *
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 Xiefe3807a2013-07-18 17:31:50 -0700183 */
184 public BluetoothDevice getClient() {
185 if (VDBG) log("getClient()");
Jack He16eeac32017-08-17 12:11:18 -0700186 final IBluetoothMap service = mService;
187 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700188 try {
Jack He16eeac32017-08-17 12:11:18 -0700189 return service.getClient();
Jack Hea355e5e2017-08-22 16:06:54 -0700190 } catch (RemoteException e) {
191 Log.e(TAG, e.toString());
192 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700193 } 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 Schulz0d376052013-08-22 11:18:02 +0200201 * 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 Xiefe3807a2013-07-18 17:31:50 -0700204 */
205 public boolean isConnected(BluetoothDevice device) {
206 if (VDBG) log("isConnected(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700207 final IBluetoothMap service = mService;
208 if (service != null) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700209 try {
Jack He16eeac32017-08-17 12:11:18 -0700210 return service.isConnected(device);
Jack Hea355e5e2017-08-22 16:06:54 -0700211 } catch (RemoteException e) {
212 Log.e(TAG, e.toString());
213 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700214 } 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 Schulz0d376052013-08-22 11:18:02 +0200222 * Initiate connection. Initiation of outgoing connections is not
223 * supported for MAP server.
Matthew Xiefe3807a2013-07-18 17:31:50 -0700224 */
Kim Schulz0d376052013-08-22 11:18:02 +0200225 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 Hea355e5e2017-08-22 16:06:54 -0700234 * @return false on error, true otherwise
Kim Schulz0d376052013-08-22 11:18:02 +0200235 */
236 public boolean disconnect(BluetoothDevice device) {
237 if (DBG) log("disconnect(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700238 final IBluetoothMap service = mService;
239 if (service != null && isEnabled() && isValidDevice(device)) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700240 try {
Jack He16eeac32017-08-17 12:11:18 -0700241 return service.disconnect(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200242 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700243 Log.e(TAG, Log.getStackTraceString(new Throwable()));
244 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200245 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700246 }
Jack He16eeac32017-08-17 12:11:18 -0700247 if (service == null) Log.w(TAG, "Proxy not attached to service");
Matthew Xiefe3807a2013-07-18 17:31:50 -0700248 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 Hea355e5e2017-08-22 16:06:54 -0700256 *
Matthew Xiefe3807a2013-07-18 17:31:50 -0700257 * @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 Hea355e5e2017-08-22 16:06:54 -0700262 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 Xiefe3807a2013-07-18 17:31:50 -0700269 }
270 }
271
Kim Schulz0d376052013-08-22 11:18:02 +0200272 /**
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 He16eeac32017-08-17 12:11:18 -0700279 final IBluetoothMap service = mService;
280 if (service != null && isEnabled()) {
Kim Schulz0d376052013-08-22 11:18:02 +0200281 try {
Jack He16eeac32017-08-17 12:11:18 -0700282 return service.getConnectedDevices();
Kim Schulz0d376052013-08-22 11:18:02 +0200283 } catch (RemoteException e) {
284 Log.e(TAG, Log.getStackTraceString(new Throwable()));
285 return new ArrayList<BluetoothDevice>();
286 }
287 }
Jack He16eeac32017-08-17 12:11:18 -0700288 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200289 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 He16eeac32017-08-17 12:11:18 -0700299 final IBluetoothMap service = mService;
300 if (service != null && isEnabled()) {
Kim Schulz0d376052013-08-22 11:18:02 +0200301 try {
Jack He16eeac32017-08-17 12:11:18 -0700302 return service.getDevicesMatchingConnectionStates(states);
Kim Schulz0d376052013-08-22 11:18:02 +0200303 } catch (RemoteException e) {
304 Log.e(TAG, Log.getStackTraceString(new Throwable()));
305 return new ArrayList<BluetoothDevice>();
306 }
307 }
Jack He16eeac32017-08-17 12:11:18 -0700308 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200309 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 He16eeac32017-08-17 12:11:18 -0700319 final IBluetoothMap service = mService;
320 if (service != null && isEnabled() && isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200321 try {
Jack He16eeac32017-08-17 12:11:18 -0700322 return service.getConnectionState(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200323 } catch (RemoteException e) {
324 Log.e(TAG, Log.getStackTraceString(new Throwable()));
325 return BluetoothProfile.STATE_DISCONNECTED;
326 }
327 }
Jack He16eeac32017-08-17 12:11:18 -0700328 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200329 return BluetoothProfile.STATE_DISCONNECTED;
330 }
331
332 /**
333 * Set priority of the profile
334 *
335 * <p> The device should already be paired.
Jack Hea355e5e2017-08-22 16:06:54 -0700336 * Priority can be one of {@link #PRIORITY_ON} or
Kim Schulz0d376052013-08-22 11:18:02 +0200337 * {@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 He16eeac32017-08-17 12:11:18 -0700345 final IBluetoothMap service = mService;
346 if (service != null && isEnabled() && isValidDevice(device)) {
Jack He2992cd02017-08-22 21:21:23 -0700347 if (priority != BluetoothProfile.PRIORITY_OFF
348 && priority != BluetoothProfile.PRIORITY_ON) {
Jack Hea355e5e2017-08-22 16:06:54 -0700349 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200350 }
351 try {
Jack He16eeac32017-08-17 12:11:18 -0700352 return service.setPriority(device, priority);
Kim Schulz0d376052013-08-22 11:18:02 +0200353 } catch (RemoteException e) {
354 Log.e(TAG, Log.getStackTraceString(new Throwable()));
355 return false;
356 }
357 }
Jack He16eeac32017-08-17 12:11:18 -0700358 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200359 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 He16eeac32017-08-17 12:11:18 -0700374 final IBluetoothMap service = mService;
375 if (service != null && isEnabled() && isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200376 try {
Jack He16eeac32017-08-17 12:11:18 -0700377 return service.getPriority(device);
Kim Schulz0d376052013-08-22 11:18:02 +0200378 } catch (RemoteException e) {
379 Log.e(TAG, Log.getStackTraceString(new Throwable()));
380 return PRIORITY_OFF;
381 }
382 }
Jack He16eeac32017-08-17 12:11:18 -0700383 if (service == null) Log.w(TAG, "Proxy not attached to service");
Kim Schulz0d376052013-08-22 11:18:02 +0200384 return PRIORITY_OFF;
385 }
386
Matthew Xie9b693992013-10-10 11:21:40 -0700387 private final ServiceConnection mConnection = new ServiceConnection() {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700388 public void onServiceConnected(ComponentName className, IBinder service) {
389 if (DBG) log("Proxy object connected");
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600390 mService = IBluetoothMap.Stub.asInterface(Binder.allowBlocking(service));
Matthew Xiefe3807a2013-07-18 17:31:50 -0700391 if (mServiceListener != null) {
Kim Schulz0d376052013-08-22 11:18:02 +0200392 mServiceListener.onServiceConnected(BluetoothProfile.MAP, BluetoothMap.this);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700393 }
394 }
Jack Hea355e5e2017-08-22 16:06:54 -0700395
Matthew Xiefe3807a2013-07-18 17:31:50 -0700396 public void onServiceDisconnected(ComponentName className) {
397 if (DBG) log("Proxy object disconnected");
398 mService = null;
399 if (mServiceListener != null) {
Kim Schulz0d376052013-08-22 11:18:02 +0200400 mServiceListener.onServiceDisconnected(BluetoothProfile.MAP);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700401 }
402 }
403 };
404
405 private static void log(String msg) {
406 Log.d(TAG, msg);
407 }
Kim Schulz0d376052013-08-22 11:18:02 +0200408
Jack Hea355e5e2017-08-22 16:06:54 -0700409 private boolean isEnabled() {
Kim Schulz0d376052013-08-22 11:18:02 +0200410 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 He16eeac32017-08-17 12:11:18 -0700415 private static boolean isValidDevice(BluetoothDevice device) {
416 return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
Kim Schulz0d376052013-08-22 11:18:02 +0200417 }
418
Matthew Xiefe3807a2013-07-18 17:31:50 -0700419}