OSDN Git Service

Bluetooth: Fix startAdvertisingSet error handling
authorJakub Pawlowski <jpawlowski@google.com>
Thu, 17 Aug 2017 14:19:12 +0000 (07:19 -0700)
committerAndre Eisenbach <eisenbach@google.com>
Thu, 17 Aug 2017 22:34:40 +0000 (22:34 +0000)
Calls to old advertising API (startAdvertising), should never throw
exceptions. Instead, it used to post failure callback. This behaviour
was accidentally modified when implementing new API. Right now, instead
of posting error callback, we throw IllegalArgumentException if we fail
to obtain BluetoothGatt object, or the call to startAdvertisingSet
fails.

This patch brings back the old behaviour to the API. It also makes new
API post callback instead of throwing exception in this error case.

Bug: 63819108
Test: manual
Change-Id: I897b99839f899ca3f3dc609918d665c8c327b777
Merged-In: I897b99839f899ca3f3dc609918d665c8c327b777
(cherry picked from commit f7bd6b26c73b7ea0bbfd00b75c23895eca281aff)

core/java/android/bluetooth/le/BluetoothLeAdvertiser.java

index dfd5996..5830c27 100644 (file)
@@ -415,7 +415,8 @@ public final class BluetoothLeAdvertiser {
           gatt = mBluetoothManager.getBluetoothGatt();
         } catch (RemoteException e) {
           Log.e(TAG, "Failed to get Bluetooth gatt - ", e);
-          throw new IllegalStateException("Failed to get Bluetooth");
+          postStartSetFailure(handler, callback, AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR);
+          return;
         }
 
         IAdvertisingSetCallback wrapped = wrap(callback, handler);
@@ -429,7 +430,8 @@ public final class BluetoothLeAdvertiser {
                                      periodicData, duration, maxExtendedAdvertisingEvents, wrapped);
         } catch (RemoteException e) {
           Log.e(TAG, "Failed to start advertising set - ", e);
-          throw new IllegalStateException("Failed to start advertising set");
+          postStartSetFailure(handler, callback, AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR);
+          return;
         }
     }
 
@@ -648,6 +650,16 @@ public final class BluetoothLeAdvertiser {
         };
     }
 
+    private void postStartSetFailure(Handler handler, final AdvertisingSetCallback callback,
+        final int error) {
+        handler.post(new Runnable() {
+              @Override
+              public void run() {
+                  callback.onAdvertisingSetStarted(null, 0, error);
+              }
+          });
+    }
+
     private void postStartFailure(final AdvertiseCallback callback, final int error) {
         mHandler.post(new Runnable() {
             @Override