BluetoothPbap.java revision 221ea892dcc661bd07d6f36ff012edca2c48aed4
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 17package android.bluetooth; 18 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.content.ServiceConnection; 23import android.os.RemoteException; 24import android.os.IBinder; 25import android.os.ServiceManager; 26import android.util.Log; 27 28/** 29 * The Android Bluetooth API is not finalized, and *will* change. Use at your 30 * own risk. 31 * 32 * Public API for controlling the Bluetooth Pbap Service. This includes 33 * Bluetooth Phone book Access profile. 34 * BluetoothPbap is a proxy object for controlling the Bluetooth Pbap 35 * Service via IPC. 36 * 37 * Creating a BluetoothPbap object will create a binding with the 38 * BluetoothPbap service. Users of this object should call close() when they 39 * are finished with the BluetoothPbap, so that this proxy object can unbind 40 * from the service. 41 * 42 * This BluetoothPbap object is not immediately bound to the 43 * BluetoothPbap service. Use the ServiceListener interface to obtain a 44 * notification when it is bound, this is especially important if you wish to 45 * immediately call methods on BluetoothPbap after construction. 46 * 47 * Android only supports one connected Bluetooth Pce at a time. 48 * 49 * @hide 50 */ 51public class BluetoothPbap { 52 53 private static final String TAG = "BluetoothPbap"; 54 private static final boolean DBG = true; 55 private static final boolean VDBG = false; 56 57 /** int extra for PBAP_STATE_CHANGED_ACTION */ 58 public static final String PBAP_STATE = 59 "android.bluetooth.pbap.intent.PBAP_STATE"; 60 /** int extra for PBAP_STATE_CHANGED_ACTION */ 61 public static final String PBAP_PREVIOUS_STATE = 62 "android.bluetooth.pbap.intent.PBAP_PREVIOUS_STATE"; 63 64 /** Indicates the state of a pbap connection state has changed. 65 * This intent will always contain PBAP_STATE, PBAP_PREVIOUS_STATE and 66 * BluetoothIntent.ADDRESS extras. 67 */ 68 public static final String PBAP_STATE_CHANGED_ACTION = 69 "android.bluetooth.pbap.intent.action.PBAP_STATE_CHANGED"; 70 71 private IBluetoothPbap mService; 72 private final Context mContext; 73 private ServiceListener mServiceListener; 74 private BluetoothAdapter mAdapter; 75 76 /** There was an error trying to obtain the state */ 77 public static final int STATE_ERROR = -1; 78 /** No client currently connected */ 79 public static final int STATE_DISCONNECTED = 0; 80 /** Connection attempt in progress */ 81 public static final int STATE_CONNECTING = 1; 82 /** Client is currently connected */ 83 public static final int STATE_CONNECTED = 2; 84 85 public static final int RESULT_FAILURE = 0; 86 public static final int RESULT_SUCCESS = 1; 87 /** Connection canceled before completion. */ 88 public static final int RESULT_CANCELED = 2; 89 90 /** 91 * An interface for notifying Bluetooth PCE IPC clients when they have 92 * been connected to the BluetoothPbap service. 93 */ 94 public interface ServiceListener { 95 /** 96 * Called to notify the client when this proxy object has been 97 * connected to the BluetoothPbap service. Clients must wait for 98 * this callback before making IPC calls on the BluetoothPbap 99 * service. 100 */ 101 public void onServiceConnected(BluetoothPbap proxy); 102 103 /** 104 * Called to notify the client that this proxy object has been 105 * disconnected from the BluetoothPbap service. Clients must not 106 * make IPC calls on the BluetoothPbap service after this callback. 107 * This callback will currently only occur if the application hosting 108 * the BluetoothPbap service, but may be called more often in future. 109 */ 110 public void onServiceDisconnected(); 111 } 112 113 final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback = 114 new IBluetoothStateChangeCallback.Stub() { 115 public void onBluetoothStateChange(boolean up) { 116 if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); 117 if (!up) { 118 if (VDBG) Log.d(TAG,"Unbinding service..."); 119 synchronized (mConnection) { 120 try { 121 mService = null; 122 mContext.unbindService(mConnection); 123 } catch (Exception re) { 124 Log.e(TAG,"",re); 125 } 126 } 127 } else { 128 synchronized (mConnection) { 129 try { 130 if (mService == null) { 131 if (VDBG) Log.d(TAG,"Binding service..."); 132 doBind(); 133 } 134 } catch (Exception re) { 135 Log.e(TAG,"",re); 136 } 137 } 138 } 139 } 140 }; 141 142 /** 143 * Create a BluetoothPbap proxy object. 144 */ 145 public BluetoothPbap(Context context, ServiceListener l) { 146 mContext = context; 147 mServiceListener = l; 148 mAdapter = BluetoothAdapter.getDefaultAdapter(); 149 IBluetoothManager mgr = mAdapter.getBluetoothManager(); 150 if (mgr != null) { 151 try { 152 mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); 153 } catch (RemoteException e) { 154 Log.e(TAG,"",e); 155 } 156 } 157 doBind(); 158 } 159 160 boolean doBind() { 161 Intent intent = new Intent(IBluetoothPbap.class.getName()); 162 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); 163 intent.setComponent(comp); 164 if (comp == null || !mContext.bindService(intent, mConnection, 0)) { 165 Log.e(TAG, "Could not bind to Bluetooth Pbap Service with " + intent); 166 return false; 167 } 168 return true; 169 } 170 171 protected void finalize() throws Throwable { 172 try { 173 close(); 174 } finally { 175 super.finalize(); 176 } 177 } 178 179 /** 180 * Close the connection to the backing service. 181 * Other public functions of BluetoothPbap will return default error 182 * results once close() has been called. Multiple invocations of close() 183 * are ok. 184 */ 185 public synchronized void close() { 186 IBluetoothManager mgr = mAdapter.getBluetoothManager(); 187 if (mgr != null) { 188 try { 189 mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); 190 } catch (Exception e) { 191 Log.e(TAG,"",e); 192 } 193 } 194 195 synchronized (mConnection) { 196 if (mService != null) { 197 try { 198 mService = null; 199 mContext.unbindService(mConnection); 200 mConnection = null; 201 } catch (Exception re) { 202 Log.e(TAG,"",re); 203 } 204 } 205 } 206 mServiceListener = null; 207 } 208 209 /** 210 * Get the current state of the BluetoothPbap service. 211 * @return One of the STATE_ return codes, or STATE_ERROR if this proxy 212 * object is currently not connected to the Pbap service. 213 */ 214 public int getState() { 215 if (VDBG) log("getState()"); 216 if (mService != null) { 217 try { 218 return mService.getState(); 219 } catch (RemoteException e) {Log.e(TAG, e.toString());} 220 } else { 221 Log.w(TAG, "Proxy not attached to service"); 222 if (DBG) log(Log.getStackTraceString(new Throwable())); 223 } 224 return BluetoothPbap.STATE_ERROR; 225 } 226 227 /** 228 * Get the currently connected remote Bluetooth device (PCE). 229 * @return The remote Bluetooth device, or null if not in connected or 230 * connecting state, or if this proxy object is not connected to 231 * the Pbap service. 232 */ 233 public BluetoothDevice getClient() { 234 if (VDBG) log("getClient()"); 235 if (mService != null) { 236 try { 237 return mService.getClient(); 238 } catch (RemoteException e) {Log.e(TAG, e.toString());} 239 } else { 240 Log.w(TAG, "Proxy not attached to service"); 241 if (DBG) log(Log.getStackTraceString(new Throwable())); 242 } 243 return null; 244 } 245 246 /** 247 * Returns true if the specified Bluetooth device is connected (does not 248 * include connecting). Returns false if not connected, or if this proxy 249 * object is not currently connected to the Pbap service. 250 */ 251 public boolean isConnected(BluetoothDevice device) { 252 if (VDBG) log("isConnected(" + device + ")"); 253 if (mService != null) { 254 try { 255 return mService.isConnected(device); 256 } catch (RemoteException e) {Log.e(TAG, e.toString());} 257 } else { 258 Log.w(TAG, "Proxy not attached to service"); 259 if (DBG) log(Log.getStackTraceString(new Throwable())); 260 } 261 return false; 262 } 263 264 /** 265 * Disconnects the current Pbap client (PCE). Currently this call blocks, 266 * it may soon be made asynchronous. Returns false if this proxy object is 267 * not currently connected to the Pbap service. 268 */ 269 public boolean disconnect() { 270 if (DBG) log("disconnect()"); 271 if (mService != null) { 272 try { 273 mService.disconnect(); 274 return true; 275 } catch (RemoteException e) {Log.e(TAG, e.toString());} 276 } else { 277 Log.w(TAG, "Proxy not attached to service"); 278 if (DBG) log(Log.getStackTraceString(new Throwable())); 279 } 280 return false; 281 } 282 283 /** 284 * Check class bits for possible PBAP support. 285 * This is a simple heuristic that tries to guess if a device with the 286 * given class bits might support PBAP. It is not accurate for all 287 * devices. It tries to err on the side of false positives. 288 * @return True if this device might support PBAP. 289 */ 290 public static boolean doesClassMatchSink(BluetoothClass btClass) { 291 // TODO optimize the rule 292 switch (btClass.getDeviceClass()) { 293 case BluetoothClass.Device.COMPUTER_DESKTOP: 294 case BluetoothClass.Device.COMPUTER_LAPTOP: 295 case BluetoothClass.Device.COMPUTER_SERVER: 296 case BluetoothClass.Device.COMPUTER_UNCATEGORIZED: 297 return true; 298 default: 299 return false; 300 } 301 } 302 303 private ServiceConnection mConnection = new ServiceConnection() { 304 public void onServiceConnected(ComponentName className, IBinder service) { 305 if (DBG) log("Proxy object connected"); 306 mService = IBluetoothPbap.Stub.asInterface(service); 307 if (mServiceListener != null) { 308 mServiceListener.onServiceConnected(BluetoothPbap.this); 309 } 310 } 311 public void onServiceDisconnected(ComponentName className) { 312 if (DBG) log("Proxy object disconnected"); 313 mService = null; 314 if (mServiceListener != null) { 315 mServiceListener.onServiceDisconnected(); 316 } 317 } 318 }; 319 320 private static void log(String msg) { 321 Log.d(TAG, msg); 322 } 323} 324