Bluetooth: API change.

Split BluetoothDevice into BluetoothDevice and BluetoothAdapter.

BluetoothAdapter: Represents the local BT adapter. Operations on the local
                  adapter (start a scan, etc).
BluetoothDevice: Represents a remote BT device. Operations on remote devices
                 (pair, connect, etc).

IBluetoothDevice.aidl -> Bluetooth.aidl
BluetoothDeviceService.java -> BluetoothDeviceService.java

TODO:
Javadoc
diff --git a/core/java/android/bluetooth/BluetoothA2dp.java b/core/java/android/bluetooth/BluetoothA2dp.java
index 2ea45d5..6e48b66 100644
--- a/core/java/android/bluetooth/BluetoothA2dp.java
+++ b/core/java/android/bluetooth/BluetoothA2dp.java
@@ -25,7 +25,10 @@
 import android.os.IBinder;
 import android.util.Log;
 
-import java.util.List;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Set;
+import java.util.HashSet;
 
 /**
  * Public API for controlling the Bluetooth A2DP Profile Service.
@@ -47,7 +50,7 @@
  *
  * @hide
  */
-public class BluetoothA2dp {
+public final class BluetoothA2dp {
     private static final String TAG = "BluetoothA2dp";
     private static final boolean DBG = false;
 
@@ -79,6 +82,7 @@
     /** Default priority for a2dp devices that should not allow incoming
      * connections */
     public static final int PRIORITY_OFF = 0;
+
     private final IBluetoothA2dp mService;
     private final Context mContext;
 
@@ -89,6 +93,7 @@
      */
     public BluetoothA2dp(Context c) {
         mContext = c;
+
         IBinder b = ServiceManager.getService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE);
         if (b == null) {
             throw new RuntimeException("Bluetooth A2DP service not available!");
@@ -99,14 +104,14 @@
     /** Initiate a connection to an A2DP sink.
      *  Listen for SINK_STATE_CHANGED_ACTION to find out when the
      *  connection is completed.
-     *  @param address Remote BT address.
+     *  @param device Remote BT device.
      *  @return Result code, negative indicates an immediate error.
      *  @hide
      */
-    public int connectSink(String address) {
-        if (DBG) log("connectSink(" + address + ")");
+    public int connectSink(BluetoothDevice device) {
+        if (DBG) log("connectSink(" + device + ")");
         try {
-            return mService.connectSink(address);
+            return mService.connectSink(device);
         } catch (RemoteException e) {
             Log.w(TAG, "", e);
             return BluetoothError.ERROR_IPC;
@@ -116,14 +121,14 @@
     /** Initiate disconnect from an A2DP sink.
      *  Listen for SINK_STATE_CHANGED_ACTION to find out when
      *  disconnect is completed.
-     *  @param address Remote BT address.
+     *  @param device Remote BT device.
      *  @return Result code, negative indicates an immediate error.
      *  @hide
      */
-    public int disconnectSink(String address) {
-        if (DBG) log("disconnectSink(" + address + ")");
+    public int disconnectSink(BluetoothDevice device) {
+        if (DBG) log("disconnectSink(" + device + ")");
         try {
-            return mService.disconnectSink(address);
+            return mService.disconnectSink(device);
         } catch (RemoteException e) {
             Log.w(TAG, "", e);
             return BluetoothError.ERROR_IPC;
@@ -131,24 +136,25 @@
     }
 
     /** Check if a specified A2DP sink is connected.
-     *  @param address Remote BT address.
+     *  @param device Remote BT device.
      *  @return True if connected (or playing), false otherwise and on error.
      *  @hide
      */
-    public boolean isSinkConnected(String address) {
-        if (DBG) log("isSinkConnected(" + address + ")");
-        int state = getSinkState(address);
+    public boolean isSinkConnected(BluetoothDevice device) {
+        if (DBG) log("isSinkConnected(" + device + ")");
+        int state = getSinkState(device);
         return state == STATE_CONNECTED || state == STATE_PLAYING;
     }
 
     /** Check if any A2DP sink is connected.
-     * @return a List of connected A2DP sinks, or null on error.
+     * @return a unmodifiable set of connected A2DP sinks, or null on error.
      * @hide
      */
-    public List<String> listConnectedSinks() {
-        if (DBG) log("listConnectedSinks()");
+    public Set<BluetoothDevice> getConnectedSinks() {
+        if (DBG) log("getConnectedSinks()");
         try {
-            return mService.listConnectedSinks();
+            return Collections.unmodifiableSet(
+                    new HashSet<BluetoothDevice>(Arrays.asList(mService.getConnectedSinks())));
         } catch (RemoteException e) {
             Log.w(TAG, "", e);
             return null;
@@ -156,14 +162,14 @@
     }
 
     /** Get the state of an A2DP sink
-     *  @param address Remote BT address.
+     *  @param device Remote BT device.
      *  @return State code, or negative on error
      *  @hide
      */
-    public int getSinkState(String address) {
-        if (DBG) log("getSinkState(" + address + ")");
+    public int getSinkState(BluetoothDevice device) {
+        if (DBG) log("getSinkState(" + device + ")");
         try {
-            return mService.getSinkState(address);
+            return mService.getSinkState(device);
         } catch (RemoteException e) {
             Log.w(TAG, "", e);
             return BluetoothError.ERROR_IPC;
@@ -177,15 +183,15 @@
      * Sinks with priority greater than zero will accept incoming connections
      * (if no sink is currently connected).
      * Priority for unpaired sink must be PRIORITY_NONE.
-     * @param address Paired sink
+     * @param device Paired sink
      * @param priority Integer priority, for example PRIORITY_AUTO or
      *                 PRIORITY_NONE
      * @return Result code, negative indicates an error
      */
-    public int setSinkPriority(String address, int priority) {
-        if (DBG) log("setSinkPriority(" + address + ", " + priority + ")");
+    public int setSinkPriority(BluetoothDevice device, int priority) {
+        if (DBG) log("setSinkPriority(" + device + ", " + priority + ")");
         try {
-            return mService.setSinkPriority(address, priority);
+            return mService.setSinkPriority(device, priority);
         } catch (RemoteException e) {
             Log.w(TAG, "", e);
             return BluetoothError.ERROR_IPC;
@@ -194,13 +200,13 @@
 
     /**
      * Get priority of a2dp sink.
-     * @param address Sink
+     * @param device Sink
      * @return non-negative priority, or negative error code on error.
      */
-    public int getSinkPriority(String address) {
-        if (DBG) log("getSinkPriority(" + address + ")");
+    public int getSinkPriority(BluetoothDevice device) {
+        if (DBG) log("getSinkPriority(" + device + ")");
         try {
-            return mService.getSinkPriority(address);
+            return mService.getSinkPriority(device);
         } catch (RemoteException e) {
             Log.w(TAG, "", e);
             return BluetoothError.ERROR_IPC;
diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java
new file mode 100644
index 0000000..d207540
--- /dev/null
+++ b/core/java/android/bluetooth/BluetoothAdapter.java
@@ -0,0 +1,331 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.bluetooth;
+
+import android.os.RemoteException;
+import android.util.Log;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Set;
+import java.util.HashSet;
+
+/**
+ * Represents the local Bluetooth adapter.
+ *
+ * @hide
+ */
+public final class BluetoothAdapter {
+    private static final String TAG = "BluetoothAdapter";
+
+    public static final int BLUETOOTH_STATE_OFF = 0;
+    public static final int BLUETOOTH_STATE_TURNING_ON = 1;
+    public static final int BLUETOOTH_STATE_ON = 2;
+    public static final int BLUETOOTH_STATE_TURNING_OFF = 3;
+
+    /** Inquiry scan and page scan are both off.
+     *  Device is neither discoverable nor connectable */
+    public static final int SCAN_MODE_NONE = 0;
+    /** Page scan is on, inquiry scan is off.
+     *  Device is connectable, but not discoverable */
+    public static final int SCAN_MODE_CONNECTABLE = 1;
+    /** Page scan and inquiry scan are on.
+     *  Device is connectable and discoverable */
+    public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE = 3;
+
+    public static final int RESULT_FAILURE = -1;
+    public static final int RESULT_SUCCESS = 0;
+
+    /* The user will be prompted to enter a pin */
+    public static final int PAIRING_VARIANT_PIN = 0;
+    /* The user will be prompted to enter a passkey */
+    public static final int PAIRING_VARIANT_PASSKEY = 1;
+    /* The user will be prompted to confirm the passkey displayed on the screen */
+    public static final int PAIRING_VARIANT_CONFIRMATION = 2;
+
+    private final IBluetooth mService;
+
+    /**
+     * Do not use this constructor. Use Context.getSystemService() instead.
+     * @hide
+     */
+    public BluetoothAdapter(IBluetooth service) {
+        if (service == null) {
+            throw new IllegalArgumentException("service is null");
+        }
+        mService = service;
+    }
+
+    /**
+     * Get the remote BluetoothDevice associated with the given MAC address.
+     * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB".
+     * @param address valid Bluetooth MAC address
+     */
+    public BluetoothDevice getRemoteDevice(String address) {
+        return new BluetoothDevice(address);
+    }
+
+    /**
+     * Is Bluetooth currently turned on.
+     *
+     * @return true if Bluetooth enabled, false otherwise.
+     */
+    public boolean isEnabled() {
+        try {
+            return mService.isEnabled();
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return false;
+    }
+
+    /**
+     * Get the current state of Bluetooth.
+     *
+     * @return One of BLUETOOTH_STATE_ or BluetoothError.ERROR.
+     */
+    public int getBluetoothState() {
+        try {
+            return mService.getBluetoothState();
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return BluetoothError.ERROR;
+    }
+
+    /**
+     * Enable the Bluetooth device.
+     * Turn on the underlying hardware.
+     * This is an asynchronous call,
+     * BluetoothIntent.BLUETOOTH_STATE_CHANGED_ACTION can be used to check if
+     * and when the device is sucessfully enabled.
+     * @return false if we cannot enable the Bluetooth device. True does not
+     * imply the device was enabled, it only implies that so far there were no
+     * problems.
+     */
+    public boolean enable() {
+        try {
+            return mService.enable();
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return false;
+    }
+
+    /**
+     * Disable the Bluetooth device.
+     * This turns off the underlying hardware.
+     *
+     * @return true if successful, false otherwise.
+     */
+    public boolean disable() {
+        try {
+            return mService.disable(true);
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return false;
+    }
+
+    public String getAddress() {
+        try {
+            return mService.getAddress();
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return null;
+    }
+
+    /**
+     * Get the friendly Bluetooth name of this device.
+     *
+     * This name is visible to remote Bluetooth devices. Currently it is only
+     * possible to retrieve the Bluetooth name when Bluetooth is enabled.
+     *
+     * @return the Bluetooth name, or null if there was a problem.
+     */
+    public String getName() {
+        try {
+            return mService.getName();
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return null;
+    }
+
+    /**
+     * Set the friendly Bluetooth name of this device.
+     *
+     * This name is visible to remote Bluetooth devices. The Bluetooth Service
+     * is responsible for persisting this name.
+     *
+     * @param name the name to set
+     * @return     true, if the name was successfully set. False otherwise.
+     */
+    public boolean setName(String name) {
+        try {
+            return mService.setName(name);
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return false;
+    }
+
+    /**
+     * Get the current scan mode.
+     * Used to determine if the local device is connectable and/or discoverable
+     * @return Scan mode, one of SCAN_MODE_* or an error code
+     */
+    public int getScanMode() {
+        try {
+            return mService.getScanMode();
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return BluetoothError.ERROR_IPC;
+    }
+
+    /**
+     * Set the current scan mode.
+     * Used to make the local device connectable and/or discoverable
+     * @param scanMode One of SCAN_MODE_*
+     */
+    public void setScanMode(int scanMode) {
+        try {
+            mService.setScanMode(scanMode);
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+    }
+
+    public int getDiscoverableTimeout() {
+        try {
+            return mService.getDiscoverableTimeout();
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return -1;
+    }
+
+    public void setDiscoverableTimeout(int timeout) {
+        try {
+            mService.setDiscoverableTimeout(timeout);
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+    }
+
+    public boolean startDiscovery() {
+        try {
+            return mService.startDiscovery();
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return false;
+    }
+
+    public void cancelDiscovery() {
+        try {
+            mService.cancelDiscovery();
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+    }
+
+    public boolean isDiscovering() {
+        try {
+            return mService.isDiscovering();
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return false;
+    }
+
+    /**
+     * List remote devices that are bonded (paired) to the local adapter.
+     *
+     * Bonding (pairing) is the process by which the user enters a pin code for
+     * the device, which generates a shared link key, allowing for
+     * authentication and encryption of future connections. In Android we
+     * require bonding before RFCOMM or SCO connections can be made to a remote
+     * device.
+     *
+     * This function lists which remote devices we have a link key for. It does
+     * not cause any RF transmission, and does not check if the remote device
+     * still has it's link key with us. If the other side no longer has its
+     * link key then the RFCOMM or SCO connection attempt will result in an
+     * error.
+     *
+     * This function does not check if the remote device is in range.
+     *
+     * Remote devices that have an in-progress bonding attempt are not
+     * returned.
+     *
+     * @return unmodifiable set of bonded devices, or null on error
+     */
+    public Set<BluetoothDevice> getBondedDevices() {
+        try {
+            return toDeviceSet(mService.listBonds());
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return null;
+    }
+
+    /**
+     * Construct a listening, secure RFCOMM server socket.
+     * The remote device connecting to this socket will be authenticated and
+     * communication on this socket will be encrypted.
+     * Call #accept to retrieve connections to this socket.
+     * @return An RFCOMM BluetoothServerSocket
+     * @throws IOException On error, for example Bluetooth not available, or
+     *                     insufficient permissions.
+     */
+    public BluetoothServerSocket listenUsingRfcommOn(int port) throws IOException {
+        BluetoothServerSocket socket = new BluetoothServerSocket(
+                BluetoothSocket.TYPE_RFCOMM, true, true, port);
+        try {
+            socket.mSocket.bindListenNative();
+        } catch (IOException e) {
+            try {
+                socket.close();
+            } catch (IOException e2) { }
+            throw e;
+        }
+        return socket;
+    }
+
+    /**
+     * Construct an unencrypted, unauthenticated, RFCOMM server socket.
+     * Call #accept to retrieve connections to this socket.
+     * @return An RFCOMM BluetoothServerSocket
+     * @throws IOException On error, for example Bluetooth not available, or
+     *                     insufficient permissions.
+     */
+    public BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException {
+        BluetoothServerSocket socket = new BluetoothServerSocket(
+                BluetoothSocket.TYPE_RFCOMM, false, false, port);
+        try {
+            socket.mSocket.bindListenNative();
+        } catch (IOException e) {
+            try {
+                socket.close();
+            } catch (IOException e2) { }
+            throw e;
+        }
+        return socket;
+    }
+
+    /**
+     * Construct a SCO server socket.
+     * Call #accept to retrieve connections to this socket.
+     * @return A SCO BluetoothServerSocket
+     * @throws IOException On error, for example Bluetooth not available, or
+     *                     insufficient permissions.
+     */
+    public static BluetoothServerSocket listenUsingScoOn() throws IOException {
+        BluetoothServerSocket socket = new BluetoothServerSocket(
+                BluetoothSocket.TYPE_SCO, false, false, -1);
+        try {
+            socket.mSocket.bindListenNative();
+        } catch (IOException e) {
+            try {
+                socket.close();
+            } catch (IOException e2) { }
+            throw e;
+        }
+        return socket;
+    }
+
+    private Set<BluetoothDevice> toDeviceSet(String[] addresses) {
+        Set<BluetoothDevice> devices = new HashSet<BluetoothDevice>(addresses.length);
+        for (int i = 0; i < addresses.length; i++) {
+            devices.add(getRemoteDevice(addresses[i]));
+        }
+        return Collections.unmodifiableSet(devices);
+    }
+}
diff --git a/core/java/android/bluetooth/BluetoothAudioGateway.java b/core/java/android/bluetooth/BluetoothAudioGateway.java
index f3afd2a..abd7723 100644
--- a/core/java/android/bluetooth/BluetoothAudioGateway.java
+++ b/core/java/android/bluetooth/BluetoothAudioGateway.java
@@ -9,24 +9,22 @@
 /**
  * Listen's for incoming RFCOMM connection for the headset / handsfree service.
  *
- * This class is planned for deletion, in favor of a generic Rfcomm class.
+ * TODO: Use the new generic BluetoothSocket class instead of this legacy code
  *
  * @hide
  */
-public class BluetoothAudioGateway {
+public final class BluetoothAudioGateway {
     private static final String TAG = "BT Audio Gateway";
     private static final boolean DBG = false;
 
     private int mNativeData;
     static { classInitNative(); }
 
-    private BluetoothDevice mBluetooth;
-
     /* in */
     private int mHandsfreeAgRfcommChannel = -1;
     private int mHeadsetAgRfcommChannel   = -1;
 
-    /* out */
+    /* out - written by native code */
     private String mConnectingHeadsetAddress;
     private int mConnectingHeadsetRfcommChannel; /* -1 when not connected */
     private int mConnectingHeadsetSocketFd;
@@ -35,17 +33,18 @@
     private int mConnectingHandsfreeSocketFd;
     private int mTimeoutRemainingMs; /* in/out */
 
+    private final BluetoothAdapter mAdapter;
+
     public static final int DEFAULT_HF_AG_CHANNEL = 10;
     public static final int DEFAULT_HS_AG_CHANNEL = 11;
 
-    public BluetoothAudioGateway(BluetoothDevice bluetooth) {
-        this(bluetooth, DEFAULT_HF_AG_CHANNEL, DEFAULT_HS_AG_CHANNEL);
+    public BluetoothAudioGateway(BluetoothAdapter adapter) {
+        this(adapter, DEFAULT_HF_AG_CHANNEL, DEFAULT_HS_AG_CHANNEL);
     }
 
-    public BluetoothAudioGateway(BluetoothDevice bluetooth,
-                                 int handsfreeAgRfcommChannel,
-                                 int headsetAgRfcommChannel) {
-        mBluetooth = bluetooth;
+    public BluetoothAudioGateway(BluetoothAdapter adapter, int handsfreeAgRfcommChannel,
+                int headsetAgRfcommChannel) {
+        mAdapter = adapter;
         mHandsfreeAgRfcommChannel = handsfreeAgRfcommChannel;
         mHeadsetAgRfcommChannel = headsetAgRfcommChannel;
         initializeNativeDataNative();
@@ -58,18 +57,17 @@
     private Handler mCallback;
 
     public class IncomingConnectionInfo {
-        IncomingConnectionInfo(BluetoothDevice bluetooth, String address, int socketFd,
-                               int rfcommChan) {
-            mBluetooth = bluetooth;
-            mAddress = address;
+        public BluetoothAdapter mAdapter;
+        public BluetoothDevice mRemoteDevice;
+        public int mSocketFd;
+        public int mRfcommChan;
+        IncomingConnectionInfo(BluetoothAdapter adapter, BluetoothDevice remoteDevice,
+                int socketFd, int rfcommChan) {
+            mAdapter = adapter;
+            mRemoteDevice = remoteDevice;
             mSocketFd = socketFd;
             mRfcommChan = rfcommChan;
         }
-
-        public BluetoothDevice mBluetooth;
-        public String mAddress;
-        public int mSocketFd;
-        public int mRfcommChan;
     }
 
     public static final int MSG_INCOMING_HEADSET_CONNECTION   = 100;
@@ -111,12 +109,11 @@
                                           mConnectingHeadsetRfcommChannel);
                                     Message msg = Message.obtain(mCallback);
                                     msg.what = MSG_INCOMING_HEADSET_CONNECTION;
-                                    msg.obj = 
-                                        new IncomingConnectionInfo(
-                                            mBluetooth, 
-                                            mConnectingHeadsetAddress,
-                                            mConnectingHeadsetSocketFd,
-                                            mConnectingHeadsetRfcommChannel);
+                                    msg.obj = new IncomingConnectionInfo(
+                                        mAdapter,
+                                        mAdapter.getRemoteDevice(mConnectingHeadsetAddress),
+                                        mConnectingHeadsetSocketFd,
+                                        mConnectingHeadsetRfcommChannel);
                                     msg.sendToTarget();
                                 }
                                 if (mConnectingHandsfreeRfcommChannel >= 0) {
@@ -126,12 +123,11 @@
                                     Message msg = Message.obtain();
                                     msg.setTarget(mCallback);
                                     msg.what = MSG_INCOMING_HANDSFREE_CONNECTION;
-                                    msg.obj = 
-                                        new IncomingConnectionInfo(
-                                            mBluetooth,
-                                            mConnectingHandsfreeAddress,
-                                            mConnectingHandsfreeSocketFd,
-                                            mConnectingHandsfreeRfcommChannel);
+                                    msg.obj = new IncomingConnectionInfo(
+                                        mAdapter,
+                                        mAdapter.getRemoteDevice(mConnectingHandsfreeAddress),
+                                        mConnectingHandsfreeSocketFd,
+                                        mConnectingHandsfreeRfcommChannel);
                                     msg.sendToTarget();
                                 }
                             }
diff --git a/core/java/android/bluetooth/BluetoothDevice.aidl b/core/java/android/bluetooth/BluetoothDevice.aidl
new file mode 100644
index 0000000..daae74d
--- /dev/null
+++ b/core/java/android/bluetooth/BluetoothDevice.aidl
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.bluetooth;
+
+parcelable BluetoothDevice;
diff --git a/core/java/android/bluetooth/BluetoothDevice.java b/core/java/android/bluetooth/BluetoothDevice.java
index a64c6d7..27b2849 100644
--- a/core/java/android/bluetooth/BluetoothDevice.java
+++ b/core/java/android/bluetooth/BluetoothDevice.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2009 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,39 +16,25 @@
 
 package android.bluetooth;
 
+import android.content.Context;
+import android.os.IBinder;
+import android.os.Parcel;
+import android.os.Parcelable;
 import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.util.Log;
 
+import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 
 /**
- * The Android Bluetooth API is not finalized, and *will* change. Use at your
- * own risk.
+ * Represents a remote Bluetooth device.
  *
- * Manages the local Bluetooth device. Scan for devices, create bondings,
- * power up and down the adapter.
- *
+ * TODO: unhide
  * @hide
  */
-public class BluetoothDevice {
-
-    public static final int BLUETOOTH_STATE_OFF = 0;
-    public static final int BLUETOOTH_STATE_TURNING_ON = 1;
-    public static final int BLUETOOTH_STATE_ON = 2;
-    public static final int BLUETOOTH_STATE_TURNING_OFF = 3;
-
-    /** Inquiry scan and page scan are both off.
-     *  Device is neither discoverable nor connectable */
-    public static final int SCAN_MODE_NONE = 0;
-    /** Page scan is on, inquiry scan is off.
-     *  Device is connectable, but not discoverable */
-    public static final int SCAN_MODE_CONNECTABLE = 1;
-    /** Page scan and inquiry scan are on.
-     *  Device is connectable and discoverable */
-    public static final int SCAN_MODE_CONNECTABLE_DISCOVERABLE = 3;
-
-    public static final int RESULT_FAILURE = -1;
-    public static final int RESULT_SUCCESS = 0;
+public final class BluetoothDevice implements Parcelable {
+    private static final String TAG = "BluetoothDevice";
 
     /** We do not have a link key for the remote device, and are therefore not
      * bonded */
@@ -81,84 +67,81 @@
     /* The user will be prompted to confirm the passkey displayed on the screen */
     public static final int PAIRING_VARIANT_CONFIRMATION = 2;
 
+    private static final int ADDRESS_LENGTH = 17;
 
-    private static final String TAG = "BluetoothDevice";
+    private static IBluetooth sService;  /* Guarenteed constant after first object constructed */
 
-    private final IBluetoothDevice mService;
+    private final String mAddress;
+
     /**
-     * @hide - hide this because it takes a parameter of type
-     * IBluetoothDevice, which is a System private class.
-     * Also note that Context.getSystemService is a factory that
-     * returns a BlueToothDevice. That is the right way to get
-     * a BluetoothDevice.
+     * Create a new BluetoothDevice
+     * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB",
+     * and is validated in this constructor.
+     * @param address valid Bluetooth MAC address
+     * @throws RuntimeException Bluetooth is not available on this platform
+     * @throws IllegalArgumentException address is invalid
+     * @hide
      */
-    public BluetoothDevice(IBluetoothDevice service) {
-        mService = service;
+    /*package*/ BluetoothDevice(String address) {
+        synchronized (BluetoothDevice.class) {
+            if (sService == null) {
+                IBinder b = ServiceManager.getService(Context.BLUETOOTH_SERVICE);
+                if (b == null) {
+                    throw new RuntimeException("Bluetooth service not available");
+                }
+                sService = IBluetooth.Stub.asInterface(b);
+            }
+        }
+
+        if (!checkBluetoothAddress(address)) {
+            throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
+        }
+
+        mAddress = address;
     }
 
-    /**
-     * Is Bluetooth currently turned on.
-     *
-     * @return true if Bluetooth enabled, false otherwise.
-     */
-    public boolean isEnabled() {
-        try {
-            return mService.isEnabled();
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
+    @Override
+    public boolean equals(Object o) {
+        if (o instanceof BluetoothDevice) {
+            return mAddress.equals(((BluetoothDevice)o).getAddress());
+        }
         return false;
     }
 
-    /**
-     * Get the current state of Bluetooth.
-     *
-     * @return One of BLUETOOTH_STATE_ or BluetoothError.ERROR.
-     */
-    public int getBluetoothState() {
-        try {
-            return mService.getBluetoothState();
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return BluetoothError.ERROR;
+    @Override
+    public int hashCode() {
+        return mAddress.hashCode();
     }
 
-    /**
-     * Enable the Bluetooth device.
-     * Turn on the underlying hardware.
-     * This is an asynchronous call,
-     * BluetoothIntent.BLUETOOTH_STATE_CHANGED_ACTION can be used to check if
-     * and when the device is sucessfully enabled.
-     * @return false if we cannot enable the Bluetooth device. True does not
-     * imply the device was enabled, it only implies that so far there were no
-     * problems.
-     */
-    public boolean enable() {
-        try {
-            return mService.enable();
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return false;
+    @Override
+    public String toString() {
+        return mAddress;
     }
 
-    /**
-     * Disable the Bluetooth device.
-     * This turns off the underlying hardware.
-     *
-     * @return true if successful, false otherwise.
-     */
-    public boolean disable() {
-        try {
-            return mService.disable(true);
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return false;
+    public int describeContents() {
+        return 0;
+    }
+
+    public static final Parcelable.Creator<BluetoothDevice> CREATOR =
+            new Parcelable.Creator<BluetoothDevice>() {
+        public BluetoothDevice createFromParcel(Parcel in) {
+            return new BluetoothDevice(in.readString());
+        }
+        public BluetoothDevice[] newArray(int size) {
+            return new BluetoothDevice[size];
+        }
+    };
+
+    public void writeToParcel(Parcel out, int flags) {
+        out.writeString(mAddress);
     }
 
     public String getAddress() {
-        try {
-            return mService.getAddress();
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return null;
+        return mAddress;
     }
 
     /**
-     * Get the friendly Bluetooth name of this device.
+     * Get the friendly Bluetooth name of this remote device.
      *
      * This name is visible to remote Bluetooth devices. Currently it is only
      * possible to retrieve the Bluetooth name when Bluetooth is enabled.
@@ -167,98 +150,12 @@
      */
     public String getName() {
         try {
-            return mService.getName();
+            return sService.getRemoteName(mAddress);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return null;
     }
 
     /**
-     * Set the friendly Bluetooth name of this device.
-     *
-     * This name is visible to remote Bluetooth devices. The Bluetooth Service
-     * is responsible for persisting this name.
-     *
-     * @param name the name to set
-     * @return     true, if the name was successfully set. False otherwise.
-     */
-    public boolean setName(String name) {
-        try {
-            return mService.setName(name);
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return false;
-    }
-
-    /**
-     * Get the current scan mode.
-     * Used to determine if the local device is connectable and/or discoverable
-     * @return Scan mode, one of SCAN_MODE_* or an error code
-     */
-    public int getScanMode() {
-        try {
-            return mService.getScanMode();
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return BluetoothError.ERROR_IPC;
-    }
-
-    /**
-     * Set the current scan mode.
-     * Used to make the local device connectable and/or discoverable
-     * @param scanMode One of SCAN_MODE_*
-     */
-    public void setScanMode(int scanMode) {
-        try {
-            mService.setScanMode(scanMode);
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-    }
-
-    public int getDiscoverableTimeout() {
-        try {
-            return mService.getDiscoverableTimeout();
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return -1;
-    }
-    public void setDiscoverableTimeout(int timeout) {
-        try {
-            mService.setDiscoverableTimeout(timeout);
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-    }
-
-    public boolean startDiscovery() {
-        try {
-            return mService.startDiscovery();
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return false;
-    }
-
-    public void cancelDiscovery() {
-        try {
-            mService.cancelDiscovery();
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-    }
-
-    public boolean isDiscovering() {
-        try {
-            return mService.isDiscovering();
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return false;
-    }
-
-    /**
-     * Removes the remote device and the pairing information associated
-     * with it.
-     *
-     * @param address the Bluetooth hardware address you want to disconnect.
-     * @return true if the device was disconnected, false otherwise and on
-     *         error.
-     */
-    public boolean removeBond(String address) {
-        try {
-            return mService.removeBond(address);
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return false;
-    }
-
-    /**
      * Create a bonding with a remote bluetooth device.
      *
      * This is an asynchronous call. The result of this bonding attempt can be
@@ -268,9 +165,9 @@
      * @return false If there was an immediate problem creating the bonding,
      *         true otherwise.
      */
-    public boolean createBond(String address) {
+    public boolean createBond() {
         try {
-            return mService.createBond(address);
+            return sService.createBond(mAddress);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return false;
     }
@@ -278,41 +175,25 @@
     /**
      * Cancel an in-progress bonding request started with createBond.
      */
-    public boolean cancelBondProcess(String address) {
+    public boolean cancelBondProcess() {
         try {
-            return mService.cancelBondProcess(address);
+            return sService.cancelBondProcess(mAddress);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return false;
     }
 
     /**
-     * List remote devices that are bonded (paired) to the local device.
+     * Removes the remote device and the pairing information associated
+     * with it.
      *
-     * Bonding (pairing) is the process by which the user enters a pin code for
-     * the device, which generates a shared link key, allowing for
-     * authentication and encryption of future connections. In Android we
-     * require bonding before RFCOMM or SCO connections can be made to a remote
-     * device.
-     *
-     * This function lists which remote devices we have a link key for. It does
-     * not cause any RF transmission, and does not check if the remote device
-     * still has it's link key with us. If the other side no longer has its
-     * link key then the RFCOMM or SCO connection attempt will result in an
-     * error.
-     *
-     * This function does not check if the remote device is in range.
-     *
-     * Remote devices that have an in-progress bonding attempt are not
-     * returned.
-     *
-     * @return bluetooth hardware addresses of remote devices that are
-     *         bonded. Array size is 0 if no devices are bonded. Null on error.
+     * @return true if the device was disconnected, false otherwise and on
+     *         error.
      */
-    public String[] listBonds() {
+    public boolean removeBond() {
         try {
-            return mService.listBonds();
+            return sService.removeBond(mAddress);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return null;
+        return false;
     }
 
     /**
@@ -325,70 +206,103 @@
      * @param address Bluetooth hardware address of the remote device to check.
      * @return Result code
      */
-    public int getBondState(String address) {
+    public int getBondState() {
         try {
-            return mService.getBondState(address);
+            return sService.getBondState(mAddress);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return BluetoothError.ERROR_IPC;
     }
 
-    public String getRemoteName(String address) {
+    public int getBluetoothClass() {
         try {
-            return mService.getRemoteName(address);
-        } catch (RemoteException e) {Log.e(TAG, "", e);}
-        return null;
-    }
-
-    public int getRemoteClass(String address) {
-        try {
-            return mService.getRemoteClass(address);
+            return sService.getRemoteClass(mAddress);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return BluetoothError.ERROR_IPC;
     }
 
-     public String[] getRemoteUuids(String address) {
+     public String[] getUuids() {
         try {
-            return mService.getRemoteUuids(address);
+            return sService.getRemoteUuids(mAddress);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return null;
     }
 
-    public int getRemoteServiceChannel(String address, String uuid) {
+    public int getServiceChannel(String uuid) {
          try {
-             return mService.getRemoteServiceChannel(address, uuid);
+             return sService.getRemoteServiceChannel(mAddress, uuid);
          } catch (RemoteException e) {Log.e(TAG, "", e);}
          return BluetoothError.ERROR_IPC;
     }
 
-    public boolean setPin(String address, byte[] pin) {
+    public boolean setPin(byte[] pin) {
         try {
-            return mService.setPin(address, pin);
+            return sService.setPin(mAddress, pin);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return false;
     }
 
-    public boolean setPasskey(String address, int passkey) {
+    public boolean setPasskey(int passkey) {
         try {
-            return mService.setPasskey(address, passkey);
+            return sService.setPasskey(mAddress, passkey);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return false;
     }
 
-    public boolean setPairingConfirmation(String address, boolean confirm) {
+    public boolean setPairingConfirmation(boolean confirm) {
         try {
-            return mService.setPairingConfirmation(address, confirm);
+            return sService.setPairingConfirmation(mAddress, confirm);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return false;
     }
 
-    public boolean cancelPairingUserInput(String address) {
+    public boolean cancelPairingUserInput() {
         try {
-            return mService.cancelPairingUserInput(address);
+            return sService.cancelPairingUserInput(mAddress);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return false;
     }
 
     /**
+     * Construct a secure RFCOMM socket ready to start an outgoing connection.
+     * Call #connect on the returned #BluetoothSocket to begin the connection.
+     * The remote device will be authenticated and communication on this socket
+     * will be encrypted.
+     * @param port    remote port
+     * @return an RFCOMM BluetoothSocket
+     * @throws IOException on error, for example Bluetooth not available, or
+     *                     insufficient permissions.
+     */
+    public BluetoothSocket createRfcommSocket(int port) throws IOException {
+        return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, true, true, this, port);
+    }
+
+    /**
+     * Construct an insecure RFCOMM socket ready to start an outgoing
+     * connection.
+     * Call #connect on the returned #BluetoothSocket to begin the connection.
+     * The remote device will not be authenticated and communication on this
+     * socket will not be encrypted.
+     * @param port    remote port
+     * @return An RFCOMM BluetoothSocket
+     * @throws IOException On error, for example Bluetooth not available, or
+     *                     insufficient permissions.
+     */
+    public BluetoothSocket createInsecureRfcommSocket(int port) throws IOException {
+        return new BluetoothSocket(BluetoothSocket.TYPE_RFCOMM, -1, false, false, this, port);
+    }
+
+    /**
+     * Construct a SCO socket ready to start an outgoing connection.
+     * Call #connect on the returned #BluetoothSocket to begin the connection.
+     * @return a SCO BluetoothSocket
+     * @throws IOException on error, for example Bluetooth not available, or
+     *                     insufficient permissions.
+     */
+    public BluetoothSocket createScoSocket() throws IOException {
+        return new BluetoothSocket(BluetoothSocket.TYPE_SCO, -1, true, true, this, -1);
+    }
+
+    /**
      * Check that a pin is valid and convert to byte array.
      *
      * Bluetooth pin's are 1 to 16 bytes of UTF8 characters.
@@ -413,7 +327,6 @@
         return pinBytes;
     }
 
-    private static final int ADDRESS_LENGTH = 17;
     /** Sanity check a bluetooth address, such as "00:43:A8:23:10:F0" */
     public static boolean checkBluetoothAddress(String address) {
         if (address == null || address.length() != ADDRESS_LENGTH) {
diff --git a/core/java/android/bluetooth/BluetoothHeadset.java b/core/java/android/bluetooth/BluetoothHeadset.java
index fe1e09a..0e3d2bb 100644
--- a/core/java/android/bluetooth/BluetoothHeadset.java
+++ b/core/java/android/bluetooth/BluetoothHeadset.java
@@ -49,7 +49,7 @@
  *
  * @hide
  */
-public class BluetoothHeadset {
+public final class BluetoothHeadset {
 
     private static final String TAG = "BluetoothHeadset";
     private static final boolean DBG = false;
@@ -163,16 +163,16 @@
     }
 
     /**
-     * Get the Bluetooth address of the current headset.
-     * @return The Bluetooth address, or null if not in connected or connecting
+     * Get the BluetoothDevice for the current headset.
+     * @return current headset, or null if not in connected or connecting
      *         state, or if this proxy object is not connected to the Headset
      *         service.
      */
-    public String getHeadsetAddress() {
-        if (DBG) log("getHeadsetAddress()");
+    public BluetoothDevice getCurrentHeadset() {
+        if (DBG) log("getCurrentHeadset()");
         if (mService != null) {
             try {
-                return mService.getHeadsetAddress();
+                return mService.getCurrentHeadset();
             } catch (RemoteException e) {Log.e(TAG, e.toString());}
         } else {
             Log.w(TAG, "Proxy not attached to service");
@@ -185,19 +185,19 @@
      * Request to initiate a connection to a headset.
      * This call does not block. Fails if a headset is already connecting
      * or connected.
-     * Initiates auto-connection if address is null. Tries to connect to all
+     * Initiates auto-connection if device is null. Tries to connect to all
      * devices with priority greater than PRIORITY_AUTO in descending order.
-     * @param address The Bluetooth Address to connect to, or null to
-     *                auto-connect to the last connected headset.
-     * @return        False if there was a problem initiating the connection
-     *                procedure, and no further HEADSET_STATE_CHANGED intents
-     *                will be expected.
+     * @param device device to connect to, or null to auto-connect last connected
+     *               headset
+     * @return       false if there was a problem initiating the connection
+     *               procedure, and no further HEADSET_STATE_CHANGED intents
+     *               will be expected.
      */
-    public boolean connectHeadset(String address) {
-        if (DBG) log("connectHeadset(" + address + ")");
+    public boolean connectHeadset(BluetoothDevice device) {
+        if (DBG) log("connectHeadset(" + device + ")");
         if (mService != null) {
             try {
-                if (mService.connectHeadset(address)) {
+                if (mService.connectHeadset(device)) {
                     return true;
                 }
             } catch (RemoteException e) {Log.e(TAG, e.toString());}
@@ -213,11 +213,11 @@
      * connecting). Returns false if not connected, or if this proxy object
      * if not currently connected to the headset service.
      */
-    public boolean isConnected(String address) {
-        if (DBG) log("isConnected(" + address + ")");
+    public boolean isConnected(BluetoothDevice device) {
+        if (DBG) log("isConnected(" + device + ")");
         if (mService != null) {
             try {
-                return mService.isConnected(address);
+                return mService.isConnected(device);
             } catch (RemoteException e) {Log.e(TAG, e.toString());}
         } else {
             Log.w(TAG, "Proxy not attached to service");
@@ -295,16 +295,16 @@
      * auto-connected.
      * Incoming connections are ignored regardless of priority if there is
      * already a headset connected.
-     * @param address Paired headset
+     * @param device paired headset
      * @param priority Integer priority, for example PRIORITY_AUTO or
      *                 PRIORITY_NONE
-     * @return True if successful, false if there was some error.
+     * @return true if successful, false if there was some error
      */
-    public boolean setPriority(String address, int priority) {
-        if (DBG) log("setPriority(" + address + ", " + priority + ")");
+    public boolean setPriority(BluetoothDevice device, int priority) {
+        if (DBG) log("setPriority(" + device + ", " + priority + ")");
         if (mService != null) {
             try {
-                return mService.setPriority(address, priority);
+                return mService.setPriority(device, priority);
             } catch (RemoteException e) {Log.e(TAG, e.toString());}
         } else {
             Log.w(TAG, "Proxy not attached to service");
@@ -315,14 +315,14 @@
 
     /**
      * Get priority of headset.
-     * @param address Headset
-     * @return non-negative priority, or negative error code on error.
+     * @param device headset
+     * @return non-negative priority, or negative error code on error
      */
-    public int getPriority(String address) {
-        if (DBG) log("getPriority(" + address + ")");
+    public int getPriority(BluetoothDevice device) {
+        if (DBG) log("getPriority(" + device + ")");
         if (mService != null) {
             try {
-                return mService.getPriority(address);
+                return mService.getPriority(device);
             } catch (RemoteException e) {Log.e(TAG, e.toString());}
         } else {
             Log.w(TAG, "Proxy not attached to service");
diff --git a/core/java/android/bluetooth/BluetoothIntent.java b/core/java/android/bluetooth/BluetoothIntent.java
index d6c79b4..2a0de61 100644
--- a/core/java/android/bluetooth/BluetoothIntent.java
+++ b/core/java/android/bluetooth/BluetoothIntent.java
@@ -31,8 +31,8 @@
 public interface BluetoothIntent {
     public static final String SCAN_MODE =
         "android.bluetooth.intent.SCAN_MODE";
-    public static final String ADDRESS =
-        "android.bluetooth.intent.ADDRESS";
+    public static final String DEVICE =
+        "android.bluetooth.intent.DEVICE";
     public static final String NAME =
         "android.bluetooth.intent.NAME";
     public static final String ALIAS =
diff --git a/core/java/android/bluetooth/BluetoothPbap.java b/core/java/android/bluetooth/BluetoothPbap.java
index 5782644..645e241 100644
--- a/core/java/android/bluetooth/BluetoothPbap.java
+++ b/core/java/android/bluetooth/BluetoothPbap.java
@@ -73,11 +73,11 @@
 
     /** There was an error trying to obtain the state */
     public static final int STATE_ERROR        = -1;
-    /** No Pce currently connected */
+    /** No client currently connected */
     public static final int STATE_DISCONNECTED = 0;
     /** Connection attempt in progress */
     public static final int STATE_CONNECTING   = 1;
-    /** A Pce is currently connected */
+    /** Client is currently connected */
     public static final int STATE_CONNECTED    = 2;
 
     public static final int RESULT_FAILURE = 0;
@@ -159,16 +159,16 @@
     }
 
     /**
-     * Get the Bluetooth address of the current Pce.
-     * @return The Bluetooth address, or null if not in connected or connecting
-     *         state, or if this proxy object is not connected to the Pbap
-     *         service.
+     * Get the currently connected remote Bluetooth device (PCE).
+     * @return The remote Bluetooth device, or null if not in connected or
+     *         connecting state, or if this proxy object is not connected to
+     *         the Pbap service.
      */
-    public String getPceAddress() {
-        if (DBG) log("getPceAddress()");
+    public BluetoothDevice getClient() {
+        if (DBG) log("getClient()");
         if (mService != null) {
             try {
-                return mService.getPceAddress();
+                return mService.getClient();
             } catch (RemoteException e) {Log.e(TAG, e.toString());}
         } else {
             Log.w(TAG, "Proxy not attached to service");
@@ -178,15 +178,15 @@
     }
 
     /**
-     * Returns true if the specified Pcs is connected (does not include
-     * connecting). Returns false if not connected, or if this proxy object
-     * if not currently connected to the Pbap service.
+     * Returns true if the specified Bluetooth device is connected (does not
+     * include connecting). Returns false if not connected, or if this proxy
+     * object is not currently connected to the Pbap service.
      */
-    public boolean isConnected(String address) {
-        if (DBG) log("isConnected(" + address + ")");
+    public boolean isConnected(BluetoothDevice device) {
+        if (DBG) log("isConnected(" + device + ")");
         if (mService != null) {
             try {
-                return mService.isConnected(address);
+                return mService.isConnected(device);
             } catch (RemoteException e) {Log.e(TAG, e.toString());}
         } else {
             Log.w(TAG, "Proxy not attached to service");
@@ -196,15 +196,15 @@
     }
 
     /**
-     * Disconnects the current Pce. Currently this call blocks, it may soon
-     * be made asynchornous. Returns false if this proxy object is
+     * Disconnects the current Pbap client (PCE). Currently this call blocks,
+     * it may soon be made asynchornous. Returns false if this proxy object is
      * not currently connected to the Pbap service.
      */
-    public boolean disconnectPce() {
-        if (DBG) log("disconnectPce()");
+    public boolean disconnect() {
+        if (DBG) log("disconnect()");
         if (mService != null) {
             try {
-                mService.disconnectPce();
+                mService.disconnect();
                 return true;
             } catch (RemoteException e) {Log.e(TAG, e.toString());}
         } else {
diff --git a/core/java/android/bluetooth/BluetoothServerSocket.java b/core/java/android/bluetooth/BluetoothServerSocket.java
index f3baeab..8be300b 100644
--- a/core/java/android/bluetooth/BluetoothServerSocket.java
+++ b/core/java/android/bluetooth/BluetoothServerSocket.java
@@ -33,72 +33,7 @@
  * @hide
  */
 public final class BluetoothServerSocket implements Closeable {
-    private final BluetoothSocket mSocket;
-
-    /**
-     * Construct a listening, secure RFCOMM server socket.
-     * The remote device connecting to this socket will be authenticated and
-     * communication on this socket will be encrypted.
-     * Call #accept to retrieve connections to this socket.
-     * @return An RFCOMM BluetoothServerSocket
-     * @throws IOException On error, for example Bluetooth not available, or
-     *                     insufficient permissions.
-     */
-    public static BluetoothServerSocket listenUsingRfcommOn(int port) throws IOException {
-        BluetoothServerSocket socket = new BluetoothServerSocket(
-                BluetoothSocket.TYPE_RFCOMM, true, true, port);
-        try {
-            socket.mSocket.bindListenNative();
-        } catch (IOException e) {
-            try {
-                socket.close();
-            } catch (IOException e2) { }
-            throw e;
-        }
-        return socket;
-    }
-
-    /**
-     * Construct an unencrypted, unauthenticated, RFCOMM server socket.
-     * Call #accept to retrieve connections to this socket.
-     * @return An RFCOMM BluetoothServerSocket
-     * @throws IOException On error, for example Bluetooth not available, or
-     *                     insufficient permissions.
-     */
-    public static BluetoothServerSocket listenUsingInsecureRfcommOn(int port) throws IOException {
-        BluetoothServerSocket socket = new BluetoothServerSocket(
-                BluetoothSocket.TYPE_RFCOMM, false, false, port);
-        try {
-            socket.mSocket.bindListenNative();
-        } catch (IOException e) {
-            try {
-                socket.close();
-            } catch (IOException e2) { }
-            throw e;
-        }
-        return socket;
-    }
-
-    /**
-     * Construct a SCO server socket.
-     * Call #accept to retrieve connections to this socket.
-     * @return A SCO BluetoothServerSocket
-     * @throws IOException On error, for example Bluetooth not available, or
-     *                     insufficient permissions.
-     */
-    public static BluetoothServerSocket listenUsingScoOn() throws IOException {
-        BluetoothServerSocket socket = new BluetoothServerSocket(
-                BluetoothSocket.TYPE_SCO, false, false, -1);
-        try {
-            socket.mSocket.bindListenNative();
-        } catch (IOException e) {
-            try {
-                socket.close();
-            } catch (IOException e2) { }
-            throw e;
-        }
-        return socket;
-    }
+    /*package*/ final BluetoothSocket mSocket;
 
     /**
      * Construct a socket for incoming connections.
@@ -109,7 +44,7 @@
      * @throws IOException On error, for example Bluetooth not available, or
      *                     insufficient priveleges
      */
-    private BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
+    /*package*/ BluetoothServerSocket(int type, boolean auth, boolean encrypt, int port)
             throws IOException {
         mSocket = new BluetoothSocket(type, -1, auth, encrypt, null, port);
     }
diff --git a/core/java/android/bluetooth/BluetoothSocket.java b/core/java/android/bluetooth/BluetoothSocket.java
index de1f326..dda2cef 100644
--- a/core/java/android/bluetooth/BluetoothSocket.java
+++ b/core/java/android/bluetooth/BluetoothSocket.java
@@ -42,6 +42,7 @@
 
     private final int mType;  /* one of TYPE_RFCOMM etc */
     private final int mPort;  /* RFCOMM channel or L2CAP psm */
+    private final BluetoothDevice mDevice;    /* remote device */
     private final String mAddress;    /* remote address */
     private final boolean mAuth;
     private final boolean mEncrypt;
@@ -51,68 +52,27 @@
     private int mSocketData;    /* used by native code only */
 
     /**
-     * Construct a secure RFCOMM socket ready to start an outgoing connection.
-     * Call #connect on the returned #BluetoothSocket to begin the connection.
-     * The remote device will be authenticated and communication on this socket
-     * will be encrypted.
-     * @param address remote Bluetooth address that this socket can connect to
-     * @param port    remote port
-     * @return an RFCOMM BluetoothSocket
-     * @throws IOException on error, for example Bluetooth not available, or
-     *                     insufficient permissions.
-     */
-    public static BluetoothSocket createRfcommSocket(String address, int port)
-            throws IOException {
-        return new BluetoothSocket(TYPE_RFCOMM, -1, true, true, address, port);
-    }
-
-    /**
-     * Construct an insecure RFCOMM socket ready to start an outgoing
-     * connection.
-     * Call #connect on the returned #BluetoothSocket to begin the connection.
-     * The remote device will not be authenticated and communication on this
-     * socket will not be encrypted.
-     * @param address remote Bluetooth address that this socket can connect to
-     * @param port    remote port
-     * @return An RFCOMM BluetoothSocket
-     * @throws IOException On error, for example Bluetooth not available, or
-     *                     insufficient permissions.
-     */
-    public static BluetoothSocket createInsecureRfcommSocket(String address, int port)
-            throws IOException {
-        return new BluetoothSocket(TYPE_RFCOMM, -1, false, false, address, port);
-    }
-
-    /**
-     * Construct a SCO socket ready to start an outgoing connection.
-     * Call #connect on the returned #BluetoothSocket to begin the connection.
-     * @param address remote Bluetooth address that this socket can connect to
-     * @return a SCO BluetoothSocket
-     * @throws IOException on error, for example Bluetooth not available, or
-     *                     insufficient permissions.
-     */
-    public static BluetoothSocket createScoSocket(String address, int port)
-            throws IOException {
-        return new BluetoothSocket(TYPE_SCO, -1, true, true, address, port);
-    }
-
-    /**
-     * Construct a Bluetooth.
+     * Construct a BluetoothSocket.
      * @param type    type of socket
      * @param fd      fd to use for connected socket, or -1 for a new socket
      * @param auth    require the remote device to be authenticated
      * @param encrypt require the connection to be encrypted
-     * @param address remote Bluetooth address that this socket can connect to
+     * @param device  remote device that this socket can connect to
      * @param port    remote port
      * @throws IOException On error, for example Bluetooth not available, or
      *                     insufficient priveleges
      */
-    /*package*/ BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, String address,
-            int port) throws IOException {
+    /*package*/ BluetoothSocket(int type, int fd, boolean auth, boolean encrypt,
+            BluetoothDevice device, int port) throws IOException {
         mType = type;
         mAuth = auth;
         mEncrypt = encrypt;
-        mAddress = address;
+        mDevice = device;
+        if (device == null) {
+            mAddress = null;
+        } else {
+            mAddress = device.getAddress();
+        }
         mPort = port;
         if (fd == -1) {
             initSocketNative();
@@ -123,6 +83,22 @@
         mOutputStream = new BluetoothOutputStream(this);
     }
 
+    /**
+     * Construct a BluetoothSocket from address.
+     * @param type    type of socket
+     * @param fd      fd to use for connected socket, or -1 for a new socket
+     * @param auth    require the remote device to be authenticated
+     * @param encrypt require the connection to be encrypted
+     * @param address remote device that this socket can connect to
+     * @param port    remote port
+     * @throws IOException On error, for example Bluetooth not available, or
+     *                     insufficient priveleges
+     */
+    private BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, String address,
+            int port) throws IOException {
+        this(type, fd, auth, encrypt, new BluetoothDevice(address), port);
+    }
+
     @Override
     protected void finalize() throws Throwable {
         try {
@@ -154,12 +130,12 @@
     }
 
     /**
-     * Return the address we are connecting, or connected, to.
-     * @return Bluetooth address, or null if this socket has not yet attempted
+     * Return the remote device we are connecting, or connected, to.
+     * @return remote device, or null if this socket has not yet attempted
      *         or established a connection.
      */
-    public String getAddress() {
-        return mAddress;
+    public BluetoothDevice getRemoteDevice() {
+        return mDevice;
     }
 
     /**
diff --git a/core/java/android/bluetooth/HeadsetBase.java b/core/java/android/bluetooth/HeadsetBase.java
index f987ffd..29cf41d 100644
--- a/core/java/android/bluetooth/HeadsetBase.java
+++ b/core/java/android/bluetooth/HeadsetBase.java
@@ -31,7 +31,7 @@
  *
  * @hide
  */
-public class HeadsetBase {
+public final class HeadsetBase {
     private static final String TAG = "Bluetooth HeadsetBase";
     private static final boolean DBG = false;
 
@@ -42,8 +42,9 @@
 
     private static int sAtInputCount = 0;  /* TODO: Consider not using a static variable */
 
-    private final BluetoothDevice mBluetooth;
-    private final String mAddress;
+    private final BluetoothAdapter mAdapter;
+    private final BluetoothDevice mRemoteDevice;
+    private final String mAddress;  // for native code
     private final int mRfcommChannel;
     private int mNativeData;
     private Thread mEventThread;
@@ -73,12 +74,13 @@
 
     private native void cleanupNativeDataNative();
 
-    public HeadsetBase(PowerManager pm, BluetoothDevice bluetooth, String address,
-                       int rfcommChannel) {
+    public HeadsetBase(PowerManager pm, BluetoothAdapter adapter, BluetoothDevice device,
+            int rfcommChannel) {
         mDirection = DIRECTION_OUTGOING;
         mConnectTimestamp = System.currentTimeMillis();
-        mBluetooth = bluetooth;
-        mAddress = address;
+        mAdapter = adapter;
+        mRemoteDevice = device;
+        mAddress = device.getAddress();
         mRfcommChannel = rfcommChannel;
         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "HeadsetBase");
         mWakeLock.setReferenceCounted(false);
@@ -88,12 +90,13 @@
     }
 
     /* Create from an already exisiting rfcomm connection */
-    public HeadsetBase(PowerManager pm, BluetoothDevice bluetooth, String address, int socketFd,
-            int rfcommChannel, Handler handler) {
+    public HeadsetBase(PowerManager pm, BluetoothAdapter adapter, BluetoothDevice device,
+            int socketFd, int rfcommChannel, Handler handler) {
         mDirection = DIRECTION_INCOMING;
         mConnectTimestamp = System.currentTimeMillis();
-        mBluetooth = bluetooth;
-        mAddress = address;
+        mAdapter = adapter;
+        mRemoteDevice = device;
+        mAddress = device.getAddress();
         mRfcommChannel = rfcommChannel;
         mEventThreadHandler = handler;
         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "HeadsetBase");
@@ -252,12 +255,8 @@
         return mEventThread != null;
     }
 
-    public String getAddress() {
-        return mAddress;
-    }
-
-    public String getName() {
-        return mBluetooth.getRemoteName(mAddress);
+    public BluetoothDevice getRemoteDevice() {
+        return mRemoteDevice;
     }
 
     public int getDirection() {
diff --git a/core/java/android/bluetooth/IBluetoothDevice.aidl b/core/java/android/bluetooth/IBluetooth.aidl
similarity index 98%
rename from core/java/android/bluetooth/IBluetoothDevice.aidl
rename to core/java/android/bluetooth/IBluetooth.aidl
index a78752b..9e05a87 100644
--- a/core/java/android/bluetooth/IBluetoothDevice.aidl
+++ b/core/java/android/bluetooth/IBluetooth.aidl
@@ -21,7 +21,7 @@
  *
  * {@hide}
  */
-interface IBluetoothDevice
+interface IBluetooth
 {
     boolean isEnabled();
     int getBluetoothState();
diff --git a/core/java/android/bluetooth/IBluetoothA2dp.aidl b/core/java/android/bluetooth/IBluetoothA2dp.aidl
index 55ff27f..e6c6be2 100644
--- a/core/java/android/bluetooth/IBluetoothA2dp.aidl
+++ b/core/java/android/bluetooth/IBluetoothA2dp.aidl
@@ -16,16 +16,18 @@
 
 package android.bluetooth;
 
+import android.bluetooth.BluetoothDevice;
+
 /**
  * System private API for Bluetooth A2DP service
  *
  * {@hide}
  */
 interface IBluetoothA2dp {
-    int connectSink(in String address);
-    int disconnectSink(in String address);
-    List<String> listConnectedSinks();
-    int getSinkState(in String address);
-    int setSinkPriority(in String address, int priority);
-    int getSinkPriority(in String address);
+    int connectSink(in BluetoothDevice device);
+    int disconnectSink(in BluetoothDevice device);
+    BluetoothDevice[] getConnectedSinks();  // change to Set<> once AIDL supports
+    int getSinkState(in BluetoothDevice device);
+    int setSinkPriority(in BluetoothDevice device, int priority);
+    int getSinkPriority(in BluetoothDevice device);
 }
diff --git a/core/java/android/bluetooth/IBluetoothHeadset.aidl b/core/java/android/bluetooth/IBluetoothHeadset.aidl
index 5f42fd6..6cccd50 100644
--- a/core/java/android/bluetooth/IBluetoothHeadset.aidl
+++ b/core/java/android/bluetooth/IBluetoothHeadset.aidl
@@ -16,6 +16,8 @@
 
 package android.bluetooth;
 
+import android.bluetooth.BluetoothDevice;
+
 /**
  * System private API for Bluetooth Headset service
  *
@@ -23,13 +25,13 @@
  */
 interface IBluetoothHeadset {
     int getState();
-    String getHeadsetAddress();
-    boolean connectHeadset(in String address);
+    BluetoothDevice getCurrentHeadset();
+    boolean connectHeadset(in BluetoothDevice device);
     void disconnectHeadset();
-    boolean isConnected(in String address);
+    boolean isConnected(in BluetoothDevice device);
     boolean startVoiceRecognition();
     boolean stopVoiceRecognition();
-    boolean setPriority(in String address, int priority);
-    int getPriority(in String address);
+    boolean setPriority(in BluetoothDevice device, int priority);
+    int getPriority(in BluetoothDevice device);
     int getBatteryUsageHint();
 }
diff --git a/core/java/android/bluetooth/IBluetoothPbap.aidl b/core/java/android/bluetooth/IBluetoothPbap.aidl
index 06cdb7b..7cc77d1 100644
--- a/core/java/android/bluetooth/IBluetoothPbap.aidl
+++ b/core/java/android/bluetooth/IBluetoothPbap.aidl
@@ -16,6 +16,8 @@
 
 package android.bluetooth;
 
+import android.bluetooth.BluetoothDevice;
+
 /**
  * System private API for Bluetooth pbap service
  *
@@ -23,8 +25,8 @@
  */
 interface IBluetoothPbap {
     int getState();
-    String getPceAddress();
-    boolean connectPce(in String address);
-    void disconnectPce();
-    boolean isConnected(in String address);
+    BluetoothDevice getClient();
+    boolean connect(in BluetoothDevice device);
+    void disconnect();
+    boolean isConnected(in BluetoothDevice device);
 }