blob: b7a37f42b3fffa80ded3f9b15f2d9bd892ec8053 [file] [log] [blame]
Danica Chang6fdd0c62010-08-11 14:54:43 -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.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
fredc0f420372012-04-12 00:02:00 -070021import android.content.ComponentName;
Danica Chang6fdd0c62010-08-11 14:54:43 -070022import android.content.Context;
fredc0f420372012-04-12 00:02:00 -070023import android.content.Intent;
24import android.content.ServiceConnection;
Danica Chang6fdd0c62010-08-11 14:54:43 -070025import android.os.IBinder;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070026import android.os.RemoteException;
27import android.os.ServiceManager;
Danica Chang6fdd0c62010-08-11 14:54:43 -070028import android.util.Log;
29
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070030import java.util.ArrayList;
31import java.util.List;
Danica Chang6fdd0c62010-08-11 14:54:43 -070032
33/**
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -080034 * This class provides the APIs to control the Bluetooth Pan
35 * Profile.
36 *
37 *<p>BluetoothPan is a proxy object for controlling the Bluetooth
38 * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get
39 * the BluetoothPan proxy object.
40 *
41 *<p>Each method is protected with its appropriate permission.
42 *@hide
Danica Chang6fdd0c62010-08-11 14:54:43 -070043 */
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -080044public final class BluetoothPan implements BluetoothProfile {
Danica Chang6fdd0c62010-08-11 14:54:43 -070045 private static final String TAG = "BluetoothPan";
fredc0f420372012-04-12 00:02:00 -070046 private static final boolean DBG = true;
Matthew Xie563e4142012-10-09 22:10:37 -070047 private static final boolean VDBG = false;
Danica Chang6fdd0c62010-08-11 14:54:43 -070048
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -080049 /**
50 * Intent used to broadcast the change in connection state of the Pan
51 * profile.
52 *
53 * <p>This intent will have 4 extras:
54 * <ul>
55 * <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
56 * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li>
57 * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
58 * <li> {@link #EXTRA_LOCAL_ROLE} - Which local role the remote device is
59 * bound to. </li>
60 * </ul>
61 *
62 * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
63 * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
64 * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
65 *
66 * <p> {@link #EXTRA_LOCAL_ROLE} can be one of {@link #LOCAL_NAP_ROLE} or
67 * {@link #LOCAL_PANU_ROLE}
68 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
69 * receive.
70 */
71 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
72 public static final String ACTION_CONNECTION_STATE_CHANGED =
73 "android.bluetooth.pan.profile.action.CONNECTION_STATE_CHANGED";
Jaikumar Ganesh5200c8a2010-12-14 14:26:46 -080074
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -080075 /**
76 * Extra for {@link #ACTION_CONNECTION_STATE_CHANGED} intent
77 * The local role of the PAN profile that the remote device is bound to.
78 * It can be one of {@link #LOCAL_NAP_ROLE} or {@link #LOCAL_PANU_ROLE}.
79 */
Jaikumar Ganesh5200c8a2010-12-14 14:26:46 -080080 public static final String EXTRA_LOCAL_ROLE = "android.bluetooth.pan.extra.LOCAL_ROLE";
81
fredc0f420372012-04-12 00:02:00 -070082 public static final int PAN_ROLE_NONE = 0;
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -080083 /**
84 * The local device is acting as a Network Access Point.
85 */
Jaikumar Ganesh5200c8a2010-12-14 14:26:46 -080086 public static final int LOCAL_NAP_ROLE = 1;
fredc0f420372012-04-12 00:02:00 -070087 public static final int REMOTE_NAP_ROLE = 1;
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -080088
89 /**
90 * The local device is acting as a PAN User.
91 */
Jaikumar Ganesh5200c8a2010-12-14 14:26:46 -080092 public static final int LOCAL_PANU_ROLE = 2;
fredc0f420372012-04-12 00:02:00 -070093 public static final int REMOTE_PANU_ROLE = 2;
Jaikumar Ganesh5200c8a2010-12-14 14:26:46 -080094
95 /**
Jaikumar Ganeshfbe807d2011-01-19 13:59:32 -080096 * Return codes for the connect and disconnect Bluez / Dbus calls.
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -080097 * @hide
Jaikumar Ganeshfbe807d2011-01-19 13:59:32 -080098 */
99 public static final int PAN_DISCONNECT_FAILED_NOT_CONNECTED = 1000;
100
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800101 /**
102 * @hide
103 */
Jaikumar Ganeshfbe807d2011-01-19 13:59:32 -0800104 public static final int PAN_CONNECT_FAILED_ALREADY_CONNECTED = 1001;
105
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800106 /**
107 * @hide
108 */
Jaikumar Ganeshfbe807d2011-01-19 13:59:32 -0800109 public static final int PAN_CONNECT_FAILED_ATTEMPT_FAILED = 1002;
110
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800111 /**
112 * @hide
113 */
Jaikumar Ganeshfbe807d2011-01-19 13:59:32 -0800114 public static final int PAN_OPERATION_GENERIC_FAILURE = 1003;
115
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800116 /**
117 * @hide
118 */
Jaikumar Ganeshfbe807d2011-01-19 13:59:32 -0800119 public static final int PAN_OPERATION_SUCCESS = 1004;
120
fredc0f420372012-04-12 00:02:00 -0700121 private Context mContext;
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800122 private ServiceListener mServiceListener;
123 private BluetoothAdapter mAdapter;
fredc0f420372012-04-12 00:02:00 -0700124 private IBluetoothPan mPanService;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700125
126 /**
127 * Create a BluetoothPan proxy object for interacting with the local
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800128 * Bluetooth Service which handles the Pan profile
129 *
Danica Chang6fdd0c62010-08-11 14:54:43 -0700130 */
fredc0f420372012-04-12 00:02:00 -0700131 /*package*/ BluetoothPan(Context context, ServiceListener l) {
132 mContext = context;
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800133 mServiceListener = l;
134 mAdapter = BluetoothAdapter.getDefaultAdapter();
fredc903ac6f2012-04-24 03:59:57 -0700135 try {
136 mAdapter.getBluetoothManager().registerStateChangeCallback(mStateChangeCallback);
137 } catch (RemoteException re) {
138 Log.w(TAG,"Unable to register BluetoothStateChangeCallback",re);
139 }
Dianne Hackborn221ea892013-08-04 16:50:16 -0700140 if (VDBG) Log.d(TAG, "BluetoothPan() call bindService");
141 doBind();
142 if (VDBG) Log.d(TAG, "BluetoothPan(), bindService called");
143 }
144
145 boolean doBind() {
146 Intent intent = new Intent(IBluetoothPan.class.getName());
147 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
148 intent.setComponent(comp);
149 if (comp == null || !mContext.bindService(intent, mConnection, 0)) {
150 Log.e(TAG, "Could not bind to Bluetooth Pan Service with " + intent);
151 return false;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700152 }
Dianne Hackborn221ea892013-08-04 16:50:16 -0700153 return true;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700154 }
155
Jaikumar Ganesh9bb27512011-11-28 09:59:08 -0800156 /*package*/ void close() {
Matthew Xie563e4142012-10-09 22:10:37 -0700157 if (VDBG) log("close()");
Matthew Xie9b693992013-10-10 11:21:40 -0700158
159 IBluetoothManager mgr = mAdapter.getBluetoothManager();
160 if (mgr != null) {
161 try {
162 mgr.unregisterStateChangeCallback(mStateChangeCallback);
163 } catch (RemoteException re) {
164 Log.w(TAG,"Unable to unregister BluetoothStateChangeCallback",re);
165 }
166 }
167
168 synchronized (mConnection) {
169 if (mPanService != null) {
170 try {
171 mPanService = null;
172 mContext.unbindService(mConnection);
173 } catch (Exception re) {
174 Log.e(TAG,"",re);
175 }
176 }
fredc0f420372012-04-12 00:02:00 -0700177 }
Jaikumar Ganesh9bb27512011-11-28 09:59:08 -0800178 mServiceListener = null;
179 }
180
fredc903ac6f2012-04-24 03:59:57 -0700181 protected void finalize() {
182 close();
183 }
184
Matthew Xie9b693992013-10-10 11:21:40 -0700185 final private IBluetoothStateChangeCallback mStateChangeCallback = new IBluetoothStateChangeCallback.Stub() {
fredc903ac6f2012-04-24 03:59:57 -0700186
187 @Override
188 public void onBluetoothStateChange(boolean on) throws RemoteException {
189 //Handle enable request to bind again.
190 if (on) {
191 Log.d(TAG, "onBluetoothStateChange(on) call bindService");
Dianne Hackborn221ea892013-08-04 16:50:16 -0700192 doBind();
193 if (VDBG) Log.d(TAG, "BluetoothPan(), bindService called");
fredc903ac6f2012-04-24 03:59:57 -0700194 } else {
Matthew Xie563e4142012-10-09 22:10:37 -0700195 if (VDBG) Log.d(TAG,"Unbinding service...");
fredc903ac6f2012-04-24 03:59:57 -0700196 synchronized (mConnection) {
197 try {
198 mPanService = null;
199 mContext.unbindService(mConnection);
200 } catch (Exception re) {
201 Log.e(TAG,"",re);
202 }
203 }
204 }
205 }
206 };
207
Danica Chang6fdd0c62010-08-11 14:54:43 -0700208 /**
Jaikumar Ganeshf8789162011-05-26 13:56:40 -0700209 * Initiate connection to a profile of the remote bluetooth device.
210 *
211 * <p> This API returns false in scenarios like the profile on the
212 * device is already connected or Bluetooth is not turned on.
213 * When this API returns true, it is guaranteed that
214 * connection state intent for the profile will be broadcasted with
215 * the state. Users can get the connection state of the profile
216 * from this intent.
217 *
218 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
219 * permission.
220 *
221 * @param device Remote Bluetooth Device
222 * @return false on immediate error,
223 * true otherwise
Danica Chang6fdd0c62010-08-11 14:54:43 -0700224 * @hide
225 */
226 public boolean connect(BluetoothDevice device) {
227 if (DBG) log("connect(" + device + ")");
fredc0f420372012-04-12 00:02:00 -0700228 if (mPanService != null && isEnabled() &&
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800229 isValidDevice(device)) {
230 try {
fredc0f420372012-04-12 00:02:00 -0700231 return mPanService.connect(device);
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800232 } catch (RemoteException e) {
233 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
234 return false;
fredc0f420372012-04-12 00:02:00 -0700235 }
Danica Chang6fdd0c62010-08-11 14:54:43 -0700236 }
fredc0f420372012-04-12 00:02:00 -0700237 if (mPanService == null) Log.w(TAG, "Proxy not attached to service");
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800238 return false;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700239 }
240
241 /**
Jaikumar Ganeshf8789162011-05-26 13:56:40 -0700242 * Initiate disconnection from a profile
243 *
244 * <p> This API will return false in scenarios like the profile on the
245 * Bluetooth device is not in connected state etc. When this API returns,
246 * true, it is guaranteed that the connection state change
247 * intent will be broadcasted with the state. Users can get the
248 * disconnection state of the profile from this intent.
249 *
250 * <p> If the disconnection is initiated by a remote device, the state
251 * will transition from {@link #STATE_CONNECTED} to
252 * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the
253 * host (local) device the state will transition from
254 * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to
255 * state {@link #STATE_DISCONNECTED}. The transition to
256 * {@link #STATE_DISCONNECTING} can be used to distinguish between the
257 * two scenarios.
258 *
259 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
260 * permission.
261 *
262 * @param device Remote Bluetooth Device
263 * @return false on immediate error,
264 * true otherwise
Danica Chang6fdd0c62010-08-11 14:54:43 -0700265 * @hide
266 */
267 public boolean disconnect(BluetoothDevice device) {
268 if (DBG) log("disconnect(" + device + ")");
fredc0f420372012-04-12 00:02:00 -0700269 if (mPanService != null && isEnabled() &&
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800270 isValidDevice(device)) {
271 try {
fredc0f420372012-04-12 00:02:00 -0700272 return mPanService.disconnect(device);
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800273 } catch (RemoteException e) {
274 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
275 return false;
fredc0f420372012-04-12 00:02:00 -0700276 }
Danica Chang6fdd0c62010-08-11 14:54:43 -0700277 }
fredc0f420372012-04-12 00:02:00 -0700278 if (mPanService == null) Log.w(TAG, "Proxy not attached to service");
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800279 return false;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700280 }
281
Jaikumar Ganesh5200c8a2010-12-14 14:26:46 -0800282 /**
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800283 * {@inheritDoc}
Jaikumar Ganesh5200c8a2010-12-14 14:26:46 -0800284 */
285 public List<BluetoothDevice> getConnectedDevices() {
Matthew Xie563e4142012-10-09 22:10:37 -0700286 if (VDBG) log("getConnectedDevices()");
fredc0f420372012-04-12 00:02:00 -0700287 if (mPanService != null && isEnabled()) {
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800288 try {
fredc0f420372012-04-12 00:02:00 -0700289 return mPanService.getConnectedDevices();
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800290 } catch (RemoteException e) {
291 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
292 return new ArrayList<BluetoothDevice>();
fredc0f420372012-04-12 00:02:00 -0700293 }
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800294 }
fredc0f420372012-04-12 00:02:00 -0700295 if (mPanService == null) Log.w(TAG, "Proxy not attached to service");
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800296 return new ArrayList<BluetoothDevice>();
Jaikumar Ganesh5200c8a2010-12-14 14:26:46 -0800297 }
Danica Chang6fdd0c62010-08-11 14:54:43 -0700298
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800299 /**
300 * {@inheritDoc}
301 */
302 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
Matthew Xie563e4142012-10-09 22:10:37 -0700303 if (VDBG) log("getDevicesMatchingStates()");
fredc0f420372012-04-12 00:02:00 -0700304 if (mPanService != null && isEnabled()) {
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800305 try {
fredc0f420372012-04-12 00:02:00 -0700306 return mPanService.getDevicesMatchingConnectionStates(states);
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800307 } catch (RemoteException e) {
308 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
309 return new ArrayList<BluetoothDevice>();
fredc0f420372012-04-12 00:02:00 -0700310 }
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800311 }
fredc0f420372012-04-12 00:02:00 -0700312 if (mPanService == null) Log.w(TAG, "Proxy not attached to service");
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800313 return new ArrayList<BluetoothDevice>();
314 }
315
316 /**
317 * {@inheritDoc}
318 */
319 public int getConnectionState(BluetoothDevice device) {
Matthew Xie563e4142012-10-09 22:10:37 -0700320 if (VDBG) log("getState(" + device + ")");
fredc0f420372012-04-12 00:02:00 -0700321 if (mPanService != null && isEnabled()
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800322 && isValidDevice(device)) {
323 try {
fredc0f420372012-04-12 00:02:00 -0700324 return mPanService.getConnectionState(device);
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800325 } catch (RemoteException e) {
326 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
327 return BluetoothProfile.STATE_DISCONNECTED;
fredc0f420372012-04-12 00:02:00 -0700328 }
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800329 }
fredc0f420372012-04-12 00:02:00 -0700330 if (mPanService == null) Log.w(TAG, "Proxy not attached to service");
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800331 return BluetoothProfile.STATE_DISCONNECTED;
332 }
333
Jaikumar Ganeshb70765c2010-09-02 12:17:05 -0700334 public void setBluetoothTethering(boolean value) {
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800335 if (DBG) log("setBluetoothTethering(" + value + ")");
Danica Chang6fdd0c62010-08-11 14:54:43 -0700336 try {
fredc0f420372012-04-12 00:02:00 -0700337 mPanService.setBluetoothTethering(value);
Danica Chang6fdd0c62010-08-11 14:54:43 -0700338 } catch (RemoteException e) {
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800339 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
fredc0f420372012-04-12 00:02:00 -0700340 }
Danica Chang6fdd0c62010-08-11 14:54:43 -0700341 }
342
343 public boolean isTetheringOn() {
Matthew Xie563e4142012-10-09 22:10:37 -0700344 if (VDBG) log("isTetheringOn()");
Danica Chang6fdd0c62010-08-11 14:54:43 -0700345 try {
fredc0f420372012-04-12 00:02:00 -0700346 return mPanService.isTetheringOn();
Danica Chang6fdd0c62010-08-11 14:54:43 -0700347 } catch (RemoteException e) {
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800348 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
fredc0f420372012-04-12 00:02:00 -0700349 }
Jaikumar Ganeshe4caddb2012-01-25 16:16:48 -0800350 return false;
Danica Chang6fdd0c62010-08-11 14:54:43 -0700351 }
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800352
Matthew Xie9b693992013-10-10 11:21:40 -0700353 private final ServiceConnection mConnection = new ServiceConnection() {
fredc0f420372012-04-12 00:02:00 -0700354 public void onServiceConnected(ComponentName className, IBinder service) {
355 if (DBG) Log.d(TAG, "BluetoothPAN Proxy object connected");
356 mPanService = IBluetoothPan.Stub.asInterface(service);
357
358 if (mServiceListener != null) {
359 mServiceListener.onServiceConnected(BluetoothProfile.PAN,
360 BluetoothPan.this);
361 }
362 }
363 public void onServiceDisconnected(ComponentName className) {
364 if (DBG) Log.d(TAG, "BluetoothPAN Proxy object disconnected");
365 mPanService = null;
366 if (mServiceListener != null) {
367 mServiceListener.onServiceDisconnected(BluetoothProfile.PAN);
368 }
369 }
370 };
371
Jaikumar Ganesh74ef1192011-02-23 10:22:15 -0800372 private boolean isEnabled() {
373 if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true;
374 return false;
375 }
376
377 private boolean isValidDevice(BluetoothDevice device) {
378 if (device == null) return false;
379
380 if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
381 return false;
382 }
383
384 private static void log(String msg) {
385 Log.d(TAG, msg);
386 }
Jaikumar Ganesh9bb27512011-11-28 09:59:08 -0800387}