blob: 30c0d3c6d04823276e870255bf5b7440b746320f [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
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 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
Matthew Xiefe3807a2013-07-18 17:31:50 -070059 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 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()");
164 if (mService != null) {
165 try {
166 return mService.getState();
Jack Hea355e5e2017-08-22 16:06:54 -0700167 } catch (RemoteException e) {
168 Log.e(TAG, e.toString());
169 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700170 } 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 Hea355e5e2017-08-22 16:06:54 -0700179 *
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 Xiefe3807a2013-07-18 17:31:50 -0700182 */
183 public BluetoothDevice getClient() {
184 if (VDBG) log("getClient()");
185 if (mService != null) {
186 try {
187 return mService.getClient();
Jack Hea355e5e2017-08-22 16:06:54 -0700188 } catch (RemoteException e) {
189 Log.e(TAG, e.toString());
190 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700191 } 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 Schulz0d376052013-08-22 11:18:02 +0200199 * 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 Xiefe3807a2013-07-18 17:31:50 -0700202 */
203 public boolean isConnected(BluetoothDevice device) {
204 if (VDBG) log("isConnected(" + device + ")");
205 if (mService != null) {
206 try {
207 return mService.isConnected(device);
Jack Hea355e5e2017-08-22 16:06:54 -0700208 } catch (RemoteException e) {
209 Log.e(TAG, e.toString());
210 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700211 } 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 Schulz0d376052013-08-22 11:18:02 +0200219 * Initiate connection. Initiation of outgoing connections is not
220 * supported for MAP server.
Matthew Xiefe3807a2013-07-18 17:31:50 -0700221 */
Kim Schulz0d376052013-08-22 11:18:02 +0200222 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 Hea355e5e2017-08-22 16:06:54 -0700231 * @return false on error, true otherwise
Kim Schulz0d376052013-08-22 11:18:02 +0200232 */
233 public boolean disconnect(BluetoothDevice device) {
234 if (DBG) log("disconnect(" + device + ")");
235 if (mService != null && isEnabled() &&
Jack Hea355e5e2017-08-22 16:06:54 -0700236 isValidDevice(device)) {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700237 try {
Kim Schulz0d376052013-08-22 11:18:02 +0200238 return mService.disconnect(device);
239 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700240 Log.e(TAG, Log.getStackTraceString(new Throwable()));
241 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200242 }
Matthew Xiefe3807a2013-07-18 17:31:50 -0700243 }
Kim Schulz0d376052013-08-22 11:18:02 +0200244 if (mService == null) Log.w(TAG, "Proxy not attached to service");
Matthew Xiefe3807a2013-07-18 17:31:50 -0700245 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 Hea355e5e2017-08-22 16:06:54 -0700253 *
Matthew Xiefe3807a2013-07-18 17:31:50 -0700254 * @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 Hea355e5e2017-08-22 16:06:54 -0700259 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 Xiefe3807a2013-07-18 17:31:50 -0700266 }
267 }
268
Kim Schulz0d376052013-08-22 11:18:02 +0200269 /**
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 Hea355e5e2017-08-22 16:06:54 -0700315 isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200316 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 Hea355e5e2017-08-22 16:06:54 -0700331 * Priority can be one of {@link #PRIORITY_ON} or
Kim Schulz0d376052013-08-22 11:18:02 +0200332 * {@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 Hea355e5e2017-08-22 16:06:54 -0700341 isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200342 if (priority != BluetoothProfile.PRIORITY_OFF &&
Jack Hea355e5e2017-08-22 16:06:54 -0700343 priority != BluetoothProfile.PRIORITY_ON) {
344 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200345 }
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 Hea355e5e2017-08-22 16:06:54 -0700370 isValidDevice(device)) {
Kim Schulz0d376052013-08-22 11:18:02 +0200371 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 Xie9b693992013-10-10 11:21:40 -0700382 private final ServiceConnection mConnection = new ServiceConnection() {
Matthew Xiefe3807a2013-07-18 17:31:50 -0700383 public void onServiceConnected(ComponentName className, IBinder service) {
384 if (DBG) log("Proxy object connected");
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600385 mService = IBluetoothMap.Stub.asInterface(Binder.allowBlocking(service));
Matthew Xiefe3807a2013-07-18 17:31:50 -0700386 if (mServiceListener != null) {
Kim Schulz0d376052013-08-22 11:18:02 +0200387 mServiceListener.onServiceConnected(BluetoothProfile.MAP, BluetoothMap.this);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700388 }
389 }
Jack Hea355e5e2017-08-22 16:06:54 -0700390
Matthew Xiefe3807a2013-07-18 17:31:50 -0700391 public void onServiceDisconnected(ComponentName className) {
392 if (DBG) log("Proxy object disconnected");
393 mService = null;
394 if (mServiceListener != null) {
Kim Schulz0d376052013-08-22 11:18:02 +0200395 mServiceListener.onServiceDisconnected(BluetoothProfile.MAP);
Matthew Xiefe3807a2013-07-18 17:31:50 -0700396 }
397 }
398 };
399
400 private static void log(String msg) {
401 Log.d(TAG, msg);
402 }
Kim Schulz0d376052013-08-22 11:18:02 +0200403
Jack Hea355e5e2017-08-22 16:06:54 -0700404 private boolean isEnabled() {
Kim Schulz0d376052013-08-22 11:18:02 +0200405 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 Schulz0d376052013-08-22 11:18:02 +0200410
Jack Hea355e5e2017-08-22 16:06:54 -0700411 private boolean isValidDevice(BluetoothDevice device) {
412 if (device == null) return false;
413
414 if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
415 return false;
Kim Schulz0d376052013-08-22 11:18:02 +0200416 }
417
418
Matthew Xiefe3807a2013-07-18 17:31:50 -0700419}