OSDN Git Service

DO NOT MERGE: Move advetise clean up to callback code. fixes b/13289050
authorWei Wang <weiwa@google.com>
Wed, 19 Mar 2014 22:47:12 +0000 (15:47 -0700)
committerWei Wang <weiwa@google.com>
Thu, 20 Mar 2014 03:31:36 +0000 (20:31 -0700)
Cherrypick from master to fix b/13289050

Change-Id: Ibf3c772561125821817c947730cf21defafd4cb2

core/java/android/bluetooth/BluetoothAdapter.java

index f973d84..75b007c 100644 (file)
@@ -593,7 +593,6 @@ public final class BluetoothAdapter {
                 return false;
             }
             mAdvertisingGattCallback.stopAdvertising();
-            mAdvertisingGattCallback = null;
             return true;
         } catch (RemoteException e) {
             Log.e(TAG, "", e);
@@ -1797,15 +1796,12 @@ public final class BluetoothAdapter {
                     try {
                         IBluetoothGatt iGatt = adapter.getBluetoothManager().getBluetoothGatt();
                         iGatt.stopAdvertising();
-                        Log.d(TAG, "unregistering client " + mLeHandle);
-                        iGatt.unregisterClient(mLeHandle);
                     } catch (RemoteException e) {
-                        Log.e(TAG, "Failed to stop advertising and unregister" + e);
+                        Log.e(TAG, "Failed to stop advertising" + e);
                     }
                 } else {
                     Log.e(TAG, "stopAdvertising, BluetoothAdapter is null");
                 }
-                mLeHandle = -1;
                 notifyAll();
             }
         }
@@ -1995,6 +1991,26 @@ public final class BluetoothAdapter {
             if (advertiseState == STATE_ADVERTISE_STARTED) {
                 mAdvertiseCallback.onAdvertiseStart(status);
             } else {
+                synchronized (this) {
+                    if (status == ADVERTISE_CALLBACK_SUCCESS) {
+                        BluetoothAdapter adapter = mBluetoothAdapter.get();
+                        if (adapter != null) {
+                            try {
+                                IBluetoothGatt iGatt =
+                                        adapter.getBluetoothManager().getBluetoothGatt();
+                                Log.d(TAG, "unregistering client " + mLeHandle);
+                                iGatt.unregisterClient(mLeHandle);
+                                // Reset advertise app handle.
+                                mLeHandle = -1;
+                                adapter.mAdvertisingGattCallback = null;
+                            } catch (RemoteException e) {
+                                Log.e(TAG, "Failed to unregister client" + e);
+                            }
+                        } else {
+                            Log.e(TAG, "cannot unregister client, BluetoothAdapter is null");
+                        }
+                    }
+                }
                 mAdvertiseCallback.onAdvertiseStop(status);
             }
         }