Joseph Pirozzo | cfa8a64 | 2016-03-04 13:02:54 -0800 | [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 | |
| 19 | import java.util.List; |
| 20 | import java.util.ArrayList; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
| 25 | import android.os.RemoteException; |
| 26 | import android.os.IBinder; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | /** |
| 30 | * This class provides the APIs to control the Bluetooth PBAP Client Profile. |
| 31 | *@hide |
| 32 | */ |
| 33 | public final class BluetoothPbapClient implements BluetoothProfile { |
| 34 | |
| 35 | private static final String TAG = "BluetoothPbapClient"; |
| 36 | private static final boolean DBG = false; |
| 37 | private static final boolean VDBG = false; |
| 38 | |
| 39 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| 40 | "android.bluetooth.pbap.profile.action.CONNECTION_STATE_CHANGED"; |
| 41 | |
| 42 | private IBluetoothPbapClient mService; |
| 43 | private BluetoothDevice mDevice; |
| 44 | private final Context mContext; |
| 45 | private ServiceListener mServiceListener; |
| 46 | private BluetoothAdapter mAdapter; |
| 47 | |
| 48 | /** There was an error trying to obtain the state */ |
| 49 | public static final int STATE_ERROR = -1; |
| 50 | |
| 51 | public static final int RESULT_FAILURE = 0; |
| 52 | public static final int RESULT_SUCCESS = 1; |
| 53 | /** Connection canceled before completion. */ |
| 54 | public static final int RESULT_CANCELED = 2; |
| 55 | |
| 56 | final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
| 57 | new IBluetoothStateChangeCallback.Stub() { |
| 58 | public void onBluetoothStateChange(boolean up) { |
| 59 | if (DBG) { |
| 60 | Log.d(TAG, "onBluetoothStateChange: PBAP CLIENT up=" + up); |
| 61 | } |
| 62 | if (!up) { |
| 63 | if (VDBG) { |
| 64 | Log.d(TAG,"Unbinding service..."); |
| 65 | } |
| 66 | synchronized (mConnection) { |
| 67 | try { |
| 68 | mService = null; |
| 69 | mContext.unbindService(mConnection); |
| 70 | } catch (Exception re) { |
| 71 | Log.e(TAG,"",re); |
| 72 | } |
| 73 | } |
| 74 | } else { |
| 75 | synchronized (mConnection) { |
| 76 | try { |
| 77 | if (mService == null) { |
| 78 | if (VDBG) { |
| 79 | Log.d(TAG,"Binding service..."); |
| 80 | } |
| 81 | doBind(); |
| 82 | } |
| 83 | } catch (Exception re) { |
| 84 | Log.e(TAG,"",re); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * Create a BluetoothPbapClient proxy object. |
| 93 | */ |
| 94 | BluetoothPbapClient(Context context, ServiceListener l) { |
| 95 | if (DBG) { |
| 96 | Log.d(TAG, "Create BluetoothPbapClient proxy object"); |
| 97 | } |
| 98 | mContext = context; |
| 99 | mServiceListener = l; |
| 100 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 101 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 102 | if (mgr != null) { |
| 103 | try { |
| 104 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 105 | } catch (RemoteException e) { |
| 106 | Log.e(TAG,"",e); |
| 107 | } |
| 108 | } |
| 109 | doBind(); |
| 110 | } |
| 111 | |
| 112 | private boolean doBind() { |
| 113 | Intent intent = new Intent(IBluetoothPbapClient.class.getName()); |
| 114 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 115 | intent.setComponent(comp); |
| 116 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
| 117 | android.os.Process.myUserHandle())) { |
| 118 | Log.e(TAG, "Could not bind to Bluetooth PBAP Client Service with " + intent); |
| 119 | return false; |
| 120 | } |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | protected void finalize() throws Throwable { |
| 125 | try { |
| 126 | close(); |
| 127 | } finally { |
| 128 | super.finalize(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Close the connection to the backing service. |
| 134 | * Other public functions of BluetoothPbapClient will return default error |
| 135 | * results once close() has been called. Multiple invocations of close() |
| 136 | * are ok. |
| 137 | */ |
| 138 | public synchronized void close() { |
| 139 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 140 | if (mgr != null) { |
| 141 | try { |
| 142 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 143 | } catch (Exception e) { |
| 144 | Log.e(TAG,"",e); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | synchronized (mConnection) { |
| 149 | if (mService != null) { |
| 150 | try { |
| 151 | mService = null; |
| 152 | mContext.unbindService(mConnection); |
| 153 | } catch (Exception re) { |
| 154 | Log.e(TAG,"",re); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | mServiceListener = null; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Initiate connection. |
| 163 | * Upon successful connection to remote PBAP server the Client will |
| 164 | * attempt to automatically download the users phonebook and call log. |
| 165 | * |
| 166 | * @param device a remote device we want connect to |
| 167 | * @return <code>true</code> if command has been issued successfully; |
| 168 | * <code>false</code> otherwise; |
| 169 | */ |
| 170 | public boolean connect(BluetoothDevice device) { |
| 171 | if (DBG) { |
| 172 | log("connect(" + device + ") for PBAP Client."); |
| 173 | } |
| 174 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 175 | try { |
| 176 | mDevice = device; |
| 177 | return mService.connect(device); |
| 178 | } catch (RemoteException e) { |
| 179 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | if (mService == null) { |
| 184 | Log.w(TAG, "Proxy not attached to service"); |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Initiate disconnect. |
| 191 | * |
| 192 | * @param device Remote Bluetooth Device |
| 193 | * @return false on error, |
| 194 | * true otherwise |
| 195 | */ |
| 196 | public boolean disconnect() { |
| 197 | if (DBG) { |
| 198 | log("disconnect(" + mDevice + ")"); |
| 199 | } |
| 200 | if (mService != null && isEnabled() && isValidDevice(mDevice)) { |
| 201 | try { |
| 202 | mService.disconnect(mDevice); |
| 203 | return true; |
| 204 | } catch (RemoteException e) { |
| 205 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | if (mService == null) { |
| 210 | Log.w(TAG, "Proxy not attached to service"); |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Get the list of connected devices. |
| 217 | * Currently at most one. |
| 218 | * |
| 219 | * @return list of connected devices |
| 220 | */ |
| 221 | @Override |
| 222 | public List<BluetoothDevice> getConnectedDevices() { |
| 223 | if (DBG) { |
| 224 | log("getConnectedDevices()"); |
| 225 | } |
| 226 | if (mService != null && isEnabled()) { |
| 227 | try { |
| 228 | return mService.getConnectedDevices(); |
| 229 | } catch (RemoteException e) { |
| 230 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 231 | return new ArrayList<BluetoothDevice>(); |
| 232 | } |
| 233 | } |
| 234 | if (mService == null) { |
| 235 | Log.w(TAG, "Proxy not attached to service"); |
| 236 | } |
| 237 | return new ArrayList<BluetoothDevice>(); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Get the list of devices matching specified states. Currently at most one. |
| 242 | * |
| 243 | * @return list of matching devices |
| 244 | */ |
| 245 | @Override |
| 246 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 247 | if (DBG) { |
| 248 | log("getDevicesMatchingStates()"); |
| 249 | } |
| 250 | if (mService != null && isEnabled()) { |
| 251 | try { |
| 252 | return mService.getDevicesMatchingConnectionStates(states); |
| 253 | } catch (RemoteException e) { |
| 254 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 255 | return new ArrayList<BluetoothDevice>(); |
| 256 | } |
| 257 | } |
| 258 | if (mService == null) { |
| 259 | Log.w(TAG, "Proxy not attached to service"); |
| 260 | } |
| 261 | return new ArrayList<BluetoothDevice>(); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Get connection state of device |
| 266 | * |
| 267 | * @return device connection state |
| 268 | */ |
| 269 | @Override |
| 270 | public int getConnectionState(BluetoothDevice device) { |
| 271 | if (DBG) { |
| 272 | log("getConnectionState(" + device + ")"); |
| 273 | } |
| 274 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 275 | try { |
| 276 | return mService.getConnectionState(device); |
| 277 | } catch (RemoteException e) { |
| 278 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 279 | return BluetoothProfile.STATE_DISCONNECTED; |
| 280 | } |
| 281 | } |
| 282 | if (mService == null) { |
| 283 | Log.w(TAG, "Proxy not attached to service"); |
| 284 | } |
| 285 | return BluetoothProfile.STATE_DISCONNECTED; |
| 286 | } |
| 287 | |
| 288 | private final ServiceConnection mConnection = new ServiceConnection() { |
| 289 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 290 | if (DBG) { |
| 291 | log("Proxy object connected"); |
| 292 | } |
| 293 | mService = IBluetoothPbapClient.Stub.asInterface(service); |
| 294 | if (mServiceListener != null) { |
| 295 | mServiceListener.onServiceConnected(BluetoothProfile.PBAP_CLIENT, BluetoothPbapClient.this); |
| 296 | } |
| 297 | } |
| 298 | public void onServiceDisconnected(ComponentName className) { |
| 299 | if (DBG) { |
| 300 | log("Proxy object disconnected"); |
| 301 | } |
| 302 | mService = null; |
| 303 | if (mServiceListener != null) { |
| 304 | mServiceListener.onServiceDisconnected(BluetoothProfile.PBAP_CLIENT); |
| 305 | } |
| 306 | } |
| 307 | }; |
| 308 | |
| 309 | private static void log(String msg) { |
| 310 | Log.d(TAG, msg); |
| 311 | } |
| 312 | |
| 313 | private boolean isEnabled() { |
| 314 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 315 | if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) { |
| 316 | return true; |
| 317 | } |
| 318 | log("Bluetooth is Not enabled"); |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | private boolean isValidDevice(BluetoothDevice device) { |
| 323 | if (device == null) { |
| 324 | return false; |
| 325 | } |
| 326 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) { |
| 327 | return true; |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | } |