diff options
author | fredc <fredc@broadcom.com> | 2012-04-12 00:02:00 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-07-16 21:20:54 -0700 |
commit | 3c7196448a62414bf1bfcc94947bfcfa2d60278a (patch) | |
tree | 8c6be2ed45eeadc53202eb7956519f9b96808b6b /framework/java/android/bluetooth/BluetoothPan.java | |
parent | a5d5a9c3c2dd28a5bd5a719af0a52f5e5a03f6e1 (diff) |
Non persistent adapter service
Change-Id: Ib13d5c77416e58161df0e04d7a15ec0dddbde8b5
Conflicts:
core/java/android/bluetooth/BluetoothInputDevice.java
Conflicts:
core/java/com/android/internal/app/ShutdownThread.java
services/java/com/android/server/SystemServer.java
Conflicts:
services/java/com/android/server/SystemServer.java
services/java/com/android/server/pm/ShutdownThread.java
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothPan.java')
-rw-r--r-- | framework/java/android/bluetooth/BluetoothPan.java | 119 |
1 files changed, 64 insertions, 55 deletions
diff --git a/framework/java/android/bluetooth/BluetoothPan.java b/framework/java/android/bluetooth/BluetoothPan.java index 13526e81fa..13d9078752 100644 --- a/framework/java/android/bluetooth/BluetoothPan.java +++ b/framework/java/android/bluetooth/BluetoothPan.java @@ -18,7 +18,10 @@ package android.bluetooth; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; +import android.content.ComponentName; import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; import android.os.IBinder; import android.os.RemoteException; import android.os.ServiceManager; @@ -27,7 +30,6 @@ import android.util.Log; import java.util.ArrayList; import java.util.List; - /** * This class provides the APIs to control the Bluetooth Pan * Profile. @@ -41,7 +43,7 @@ import java.util.List; */ public final class BluetoothPan implements BluetoothProfile { private static final String TAG = "BluetoothPan"; - private static final boolean DBG = false; + private static final boolean DBG = true; /** * Intent used to broadcast the change in connection state of the Pan @@ -76,15 +78,18 @@ public final class BluetoothPan implements BluetoothProfile { */ public static final String EXTRA_LOCAL_ROLE = "android.bluetooth.pan.extra.LOCAL_ROLE"; + public static final int PAN_ROLE_NONE = 0; /** * The local device is acting as a Network Access Point. */ public static final int LOCAL_NAP_ROLE = 1; + public static final int REMOTE_NAP_ROLE = 1; /** * The local device is acting as a PAN User. */ public static final int LOCAL_PANU_ROLE = 2; + public static final int REMOTE_PANU_ROLE = 2; /** * Return codes for the connect and disconnect Bluez / Dbus calls. @@ -112,34 +117,34 @@ public final class BluetoothPan implements BluetoothProfile { */ public static final int PAN_OPERATION_SUCCESS = 1004; + private Context mContext; private ServiceListener mServiceListener; private BluetoothAdapter mAdapter; - private IBluetooth mService; + private IBluetoothPan mPanService; /** * Create a BluetoothPan proxy object for interacting with the local * Bluetooth Service which handles the Pan profile * */ - /*package*/ BluetoothPan(Context mContext, ServiceListener l) { - IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE); + /*package*/ BluetoothPan(Context context, ServiceListener l) { + mContext = context; mServiceListener = l; mAdapter = BluetoothAdapter.getDefaultAdapter(); - if (b != null) { - mService = IBluetooth.Stub.asInterface(b); - if (mServiceListener != null) { - mServiceListener.onServiceConnected(BluetoothProfile.PAN, this); - } - } else { - Log.w(TAG, "Bluetooth Service not available!"); - - // Instead of throwing an exception which prevents people from going - // into Wireless settings in the emulator. Let it crash later when it is actually used. - mService = null; + Log.d(TAG, "BluetoothPan() call bindService"); + if (!context.bindService(new Intent(IBluetoothPan.class.getName()), + mConnection, 0)) { + Log.e(TAG, "Could not bind to Bluetooth HID Service"); } + Log.d(TAG, "BluetoothPan(), bindService called"); } /*package*/ void close() { + if (DBG) log("close()"); + if (mConnection != null) { + mContext.unbindService(mConnection); + mConnection = null; + } mServiceListener = null; } @@ -163,18 +168,16 @@ public final class BluetoothPan implements BluetoothProfile { */ public boolean connect(BluetoothDevice device) { if (DBG) log("connect(" + device + ")"); - if (mService != null && isEnabled() && + if (mPanService != null && isEnabled() && isValidDevice(device)) { - //TODO(BT - /* try { - return mService.connectPanDevice(device); + return mPanService.connect(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; - }*/ + } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -206,18 +209,16 @@ public final class BluetoothPan implements BluetoothProfile { */ public boolean disconnect(BluetoothDevice device) { if (DBG) log("disconnect(" + device + ")"); - if (mService != null && isEnabled() && + if (mPanService != null && isEnabled() && isValidDevice(device)) { - //TODO(BT - /* try { - return mService.disconnectPanDevice(device); + return mPanService.disconnect(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; - }*/ + } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -226,17 +227,15 @@ public final class BluetoothPan implements BluetoothProfile { */ public List<BluetoothDevice> getConnectedDevices() { if (DBG) log("getConnectedDevices()"); - if (mService != null && isEnabled()) { - //TODO(BT - /* + if (mPanService != null && isEnabled()) { try { - return mService.getConnectedPanDevices(); + return mPanService.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); - }*/ + } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -245,17 +244,15 @@ public final class BluetoothPan implements BluetoothProfile { */ public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { if (DBG) log("getDevicesMatchingStates()"); - if (mService != null && isEnabled()) { - //TODO(BT - /* + if (mPanService != null && isEnabled()) { try { - return mService.getPanDevicesMatchingConnectionStates(states); + return mPanService.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); - }*/ + } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -264,45 +261,57 @@ public final class BluetoothPan implements BluetoothProfile { */ public int getConnectionState(BluetoothDevice device) { if (DBG) log("getState(" + device + ")"); - if (mService != null && isEnabled() + if (mPanService != null && isEnabled() && isValidDevice(device)) { - //TODO(BT - /* try { - return mService.getPanDeviceConnectionState(device); + return mPanService.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return BluetoothProfile.STATE_DISCONNECTED; - }*/ + } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.STATE_DISCONNECTED; } public void setBluetoothTethering(boolean value) { if (DBG) log("setBluetoothTethering(" + value + ")"); - //TODO(BT - /* try { - mService.setBluetoothTethering(value); + mPanService.setBluetoothTethering(value); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); - }*/ + } } public boolean isTetheringOn() { if (DBG) log("isTetheringOn()"); - //TODO(BT - /* try { - return mService.isTetheringOn(); + return mPanService.isTetheringOn(); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); - return false; - }*/ + } return false; } + private ServiceConnection mConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName className, IBinder service) { + if (DBG) Log.d(TAG, "BluetoothPAN Proxy object connected"); + mPanService = IBluetoothPan.Stub.asInterface(service); + + if (mServiceListener != null) { + mServiceListener.onServiceConnected(BluetoothProfile.PAN, + BluetoothPan.this); + } + } + public void onServiceDisconnected(ComponentName className) { + if (DBG) Log.d(TAG, "BluetoothPAN Proxy object disconnected"); + mPanService = null; + if (mServiceListener != null) { + mServiceListener.onServiceDisconnected(BluetoothProfile.PAN); + } + } + }; + private boolean isEnabled() { if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; return false; |