Add remote device while pairing without inquiry

Change-Id: Ia7d99075b196414b3ded117b323ca5295c2c0b73
diff --git a/src/com/android/bluetooth/btservice/AdapterProperties.java b/src/com/android/bluetooth/btservice/AdapterProperties.java
index 854173d..80f51b3 100755
--- a/src/com/android/bluetooth/btservice/AdapterProperties.java
+++ b/src/com/android/bluetooth/btservice/AdapterProperties.java
@@ -214,48 +214,35 @@
             }
         }
     }
-
-    void addBondedDevice(BluetoothDevice device) {
+    // This function shall be invoked from BondStateMachine whenever the bond
+    // state changes.
+    void onBondStateChanged(BluetoothDevice device, int state)
+    {
         if(device == null)
             return;
-
         try {
             byte[] addrByte = Utils.getByteAddress(device);
             DeviceProperties prop = mRemoteDevices.getDeviceProperties(device);
             if (prop == null)
                 prop = mRemoteDevices.addDeviceProperties(addrByte);
-            prop.setBondState(BluetoothDevice.BOND_BONDED);
+            prop.setBondState(state);
 
-            // add if not already in list
-            if(!mBondedDevices.contains(device)) {
-                debugLog("Adding bonded device:" +  device);
-                mBondedDevices.add(device);
+            if (state == BluetoothDevice.BOND_BONDED) {
+                // add if not already in list
+                if(!mBondedDevices.contains(device)) {
+                    debugLog("Adding bonded device:" +  device);
+                    mBondedDevices.add(device);
+                }
+            } else if (state == BluetoothDevice.BOND_NONE) {
+                // remove device from list
+                if (mBondedDevices.remove(device))
+                    debugLog("Removing bonded device:" +  device);
+                else
+                    debugLog("Failed to remove device: " + device);
             }
         }
         catch(Exception ee) {
-            Log.e(TAG, "Exception in addBondedDevice : ", ee);
-        }
-    }
-
-    void removeBondedDevice(BluetoothDevice device) {
-        if(device == null)
-            return;
-
-        try {
-            byte[] addrByte = device.getAddress().getBytes();
-            DeviceProperties prop = mRemoteDevices.getDeviceProperties(device);
-            if (prop == null)
-                prop = mRemoteDevices.addDeviceProperties(addrByte);
-            prop.setBondState(BluetoothDevice.BOND_NONE);
-
-            // remove device from list
-            if (mBondedDevices.remove(device))
-                debugLog("Removing bonded device:" +  device);
-            else
-                debugLog("Failed to remove device: " + device);
-        }
-        catch(Exception ee) {
-            Log.e(TAG, "Exception in removeBondedDevice : ", ee);
+            Log.e(TAG, "Exception in onBondStateChanged : ", ee);
         }
     }
 
@@ -476,7 +463,9 @@
                         byte[] addrByte = new byte[BD_ADDR_LEN];
                         for (int j = 0; j < number; j++) {
                             System.arraycopy(val, j * BD_ADDR_LEN, addrByte, 0, BD_ADDR_LEN);
-                            addBondedDevice(mAdapter.getRemoteDevice(Utils.getAddressStringFromByte(addrByte)));
+                            onBondStateChanged(mAdapter.getRemoteDevice(
+                                               Utils.getAddressStringFromByte(addrByte)),
+                                               BluetoothDevice.BOND_BONDED);
                         }
                         break;
                     case AbstractionLayer.BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT:
diff --git a/src/com/android/bluetooth/btservice/BondStateMachine.java b/src/com/android/bluetooth/btservice/BondStateMachine.java
index 0b30ea9..400d49c 100755
--- a/src/com/android/bluetooth/btservice/BondStateMachine.java
+++ b/src/com/android/bluetooth/btservice/BondStateMachine.java
@@ -42,6 +42,7 @@
     private AdapterService mAdapterService;
     private AdapterProperties mAdapterProperties;
     private RemoteDevices mRemoteDevices;
+    private BluetoothAdapter mAdapter;
 
     private PendingCommandState mPendingCommandState = new PendingCommandState();
     private StableState mStableState = new StableState();
@@ -54,6 +55,7 @@
         mRemoteDevices = remoteDevices;
         mAdapterService = service;
         mAdapterProperties = prop;
+        mAdapter = BluetoothAdapter.getDefaultAdapter();
         setInitialState(mStableState);
     }
 
@@ -218,15 +220,12 @@
 
     private void sendIntent(BluetoothDevice device, int newState) {
         DeviceProperties devProp = mRemoteDevices.getDeviceProperties(device);
-        int oldState = devProp.getBondState();
+        int oldState = BluetoothDevice.BOND_NONE;
+        if (devProp != null) {
+            oldState = devProp.getBondState();
+        }
         if (oldState == newState) return;
-
-        devProp.setBondState(newState);
-
-        if (newState == BluetoothDevice.BOND_NONE)
-            mAdapterProperties.removeBondedDevice(device);
-        else if (newState == BluetoothDevice.BOND_BONDED)
-            mAdapterProperties.addBondedDevice(device);
+        mAdapterProperties.onBondStateChanged(device, newState);
 
         Intent intent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
@@ -241,8 +240,10 @@
         BluetoothDevice device = mRemoteDevices.getDevice(address);
 
         if (device == null) {
-            errorLog("No record of the device:" + device);
-            return;
+            infoLog("No record of the device:" + device);
+            // This device will be added as part of the BONDING_STATE_CHANGE intent processing
+            // in sendIntent above
+            device = mAdapter.getRemoteDevice(Utils.getAddressStringFromByte(address));
         }
 
         infoLog("bondStateChangeCallback: Status: " + status + " Address: " + device