OSDN Git Service

Add a default impelementation of IBluetoothGattCallback(1/2).
authorWei Wang <weiwa@google.com>
Wed, 30 Jul 2014 04:34:25 +0000 (21:34 -0700)
committerWei Wang <weiwa@google.com>
Fri, 1 Aug 2014 21:34:25 +0000 (14:34 -0700)
Also cleaned up a few TODOs and fixed bug.
b/16410260 AdvertiseCallback null onSuccess callback

Change-Id: I75a582c03be1e2ef6964c2cbeba42aaaf9f9e17c

core/java/android/bluetooth/BluetoothGatt.java
core/java/android/bluetooth/BluetoothGattCallbackWrapper.java [new file with mode: 0644]
core/java/android/bluetooth/IBluetoothGattCallback.aidl
core/java/android/bluetooth/le/BluetoothLeAdvertiser.java
core/java/android/bluetooth/le/BluetoothLeScanner.java

index 1fe43ec..59d7956 100644 (file)
@@ -16,7 +16,6 @@
 
 package android.bluetooth;
 
-import android.bluetooth.le.ScanResult;
 import android.content.Context;
 import android.os.ParcelUuid;
 import android.os.RemoteException;
@@ -130,10 +129,10 @@ public final class BluetoothGatt implements BluetoothProfile {
     /*package*/ static final int AUTHENTICATION_MITM = 2;
 
     /**
-     * Bluetooth GATT interface callbacks
+     * Bluetooth GATT callbacks. Overrides the default BluetoothGattCallback implementation.
      */
     private final IBluetoothGattCallback mBluetoothGattCallback =
-        new IBluetoothGattCallback.Stub() {
+        new BluetoothGattCallbackWrapper() {
             /**
              * Application interface registered - app is ready to go
              * @hide
@@ -198,14 +197,6 @@ public final class BluetoothGatt implements BluetoothProfile {
             }
 
             /**
-             * Callback reporting an LE scan result.
-             * @hide
-             */
-            public void onScanResult(String address, int rssi, byte[] advData) {
-                // no op
-            }
-
-            /**
              * A new GATT service has been discovered.
              * The service is added to the internal list and the search
              * continues.
@@ -600,23 +591,6 @@ public final class BluetoothGatt implements BluetoothProfile {
             }
 
             /**
-             * Advertise state change callback
-             * @hide
-             */
-            public void onAdvertiseStateChange(int state, int status) {
-                if (DBG) Log.d(TAG, "onAdvertiseStateChange() - state = "
-                        + state + " status=" + status);
-            }
-
-            /**
-             * @hide
-             */
-            @Override
-            public void onMultiAdvertiseCallback(int status) {
-                // no op.
-            }
-
-            /**
              * Callback invoked when the MTU for a given connection changes
              * @hide
              */
@@ -647,20 +621,6 @@ public final class BluetoothGatt implements BluetoothProfile {
                     Log.w(TAG, "Unhandled exception in callback", ex);
                 }
             }
-
-            @Override
-            public void onBatchScanResults(List<ScanResult> results) {
-                // no op
-            }
-
-            /**
-             * @hide
-             */
-            @Override
-            public void onFoundOrLost(boolean onFound, String address, int rssi,
-                    byte[] advData) {
-                // no op.
-            }
         };
 
     /*package*/ BluetoothGatt(Context context, IBluetoothGatt iGatt, BluetoothDevice device,
diff --git a/core/java/android/bluetooth/BluetoothGattCallbackWrapper.java b/core/java/android/bluetooth/BluetoothGattCallbackWrapper.java
new file mode 100644 (file)
index 0000000..0eb9d21
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * Copyright (C) 2014 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.bluetooth.le.AdvertiseSettings;
+import android.bluetooth.le.ScanResult;
+import android.os.ParcelUuid;
+import android.os.RemoteException;
+
+import java.util.List;
+
+/**
+ * Wrapper class for default implementation of IBluetoothGattCallback.
+ *
+ * @hide
+ */
+public class BluetoothGattCallbackWrapper extends IBluetoothGattCallback.Stub {
+
+    @Override
+    public void onClientRegistered(int status, int clientIf) throws RemoteException {
+    }
+
+    @Override
+    public void onClientConnectionState(int status, int clientIf, boolean connected, String address)
+            throws RemoteException {
+    }
+
+    @Override
+    public void onScanResult(ScanResult scanResult) throws RemoteException {
+    }
+
+    @Override
+    public void onBatchScanResults(List<ScanResult> batchResults) throws RemoteException {
+    }
+
+    @Override
+    public void onGetService(String address, int srvcType, int srvcInstId, ParcelUuid srvcUuid)
+            throws RemoteException {
+    }
+
+    @Override
+    public void onGetIncludedService(String address, int srvcType, int srvcInstId,
+            ParcelUuid srvcUuid, int inclSrvcType, int inclSrvcInstId, ParcelUuid inclSrvcUuid)
+            throws RemoteException {
+    }
+
+    @Override
+    public void onGetCharacteristic(String address, int srvcType, int srvcInstId,
+            ParcelUuid srvcUuid, int charInstId, ParcelUuid charUuid, int charProps)
+            throws RemoteException {
+    }
+
+    @Override
+    public void onGetDescriptor(String address, int srvcType, int srvcInstId, ParcelUuid srvcUuid,
+            int charInstId, ParcelUuid charUuid, int descrInstId, ParcelUuid descrUuid)
+            throws RemoteException {
+    }
+
+    @Override
+    public void onSearchComplete(String address, int status) throws RemoteException {
+    }
+
+    @Override
+    public void onCharacteristicRead(String address, int status, int srvcType, int srvcInstId,
+            ParcelUuid srvcUuid, int charInstId, ParcelUuid charUuid, byte[] value)
+            throws RemoteException {
+    }
+
+    @Override
+    public void onCharacteristicWrite(String address, int status, int srvcType, int srvcInstId,
+            ParcelUuid srvcUuid, int charInstId, ParcelUuid charUuid) throws RemoteException {
+    }
+
+    @Override
+    public void onExecuteWrite(String address, int status) throws RemoteException {
+    }
+
+    @Override
+    public void onDescriptorRead(String address, int status, int srvcType, int srvcInstId,
+            ParcelUuid srvcUuid, int charInstId, ParcelUuid charUuid, int descrInstId,
+            ParcelUuid descrUuid, byte[] value) throws RemoteException {
+    }
+
+    @Override
+    public void onDescriptorWrite(String address, int status, int srvcType, int srvcInstId,
+            ParcelUuid srvcUuid, int charInstId, ParcelUuid charUuid, int descrInstId,
+            ParcelUuid descrUuid) throws RemoteException {
+    }
+
+    @Override
+    public void onNotify(String address, int srvcType, int srvcInstId, ParcelUuid srvcUuid,
+            int charInstId, ParcelUuid charUuid, byte[] value) throws RemoteException {
+    }
+
+    @Override
+    public void onReadRemoteRssi(String address, int rssi, int status) throws RemoteException {
+    }
+
+    @Override
+    public void onMultiAdvertiseCallback(int status, boolean isStart,
+            AdvertiseSettings advertiseSettings) throws RemoteException {
+    }
+
+    @Override
+    public void onConfigureMTU(String address, int mtu, int status) throws RemoteException {
+    }
+
+    @Override
+    public void onConnectionCongested(String address, boolean congested) throws RemoteException {
+    }
+
+    @Override
+    public void onFoundOrLost(boolean onFound, String address, int rssi, byte[] advData)
+            throws RemoteException {
+    }
+
+}
index 18e3f54..f14cce0 100644 (file)
@@ -16,6 +16,7 @@
 package android.bluetooth;
 
 import android.os.ParcelUuid;
+import android.bluetooth.le.AdvertiseSettings;
 import android.bluetooth.le.ScanResult;
 
 /**
@@ -26,7 +27,7 @@ oneway interface IBluetoothGattCallback {
     void onClientRegistered(in int status, in int clientIf);
     void onClientConnectionState(in int status, in int clientIf,
                                  in boolean connected, in String address);
-    void onScanResult(in String address, in int rssi, in byte[] advData);
+    void onScanResult(in ScanResult scanResult);
     void onBatchScanResults(in List<ScanResult> batchResults);
     void onGetService(in String address, in int srvcType, in int srvcInstId,
                       in ParcelUuid srvcUuid);
@@ -64,7 +65,8 @@ oneway interface IBluetoothGattCallback {
                              in int charInstId, in ParcelUuid charUuid,
                              in byte[] value);
     void onReadRemoteRssi(in String address, in int rssi, in int status);
-    void onMultiAdvertiseCallback(in int status);
+    void onMultiAdvertiseCallback(in int status, boolean isStart,
+                                  in AdvertiseSettings advertiseSettings);
     void onConfigureMTU(in String address, in int mtu, in int status);
     void onConnectionCongested(in String address, in boolean congested);
     void onFoundOrLost(in boolean onFound, in String address, in int rssi,
index 8879da7..331ebfc 100644 (file)
@@ -18,6 +18,7 @@ package android.bluetooth.le;
 
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothGatt;
+import android.bluetooth.BluetoothGattCallbackWrapper;
 import android.bluetooth.BluetoothUuid;
 import android.bluetooth.IBluetoothGatt;
 import android.bluetooth.IBluetoothGattCallback;
@@ -233,7 +234,7 @@ public final class BluetoothLeAdvertiser {
     /**
      * Bluetooth GATT interface callbacks for advertising.
      */
-    private static class AdvertiseCallbackWrapper extends IBluetoothGattCallback.Stub {
+    private static class AdvertiseCallbackWrapper extends BluetoothGattCallbackWrapper {
         private static final int LE_CALLBACK_TIMEOUT_MILLIS = 2000;
         private final AdvertiseCallback mAdvertiseCallback;
         private final AdvertiseData mAdvertisement;
@@ -245,7 +246,7 @@ public final class BluetoothLeAdvertiser {
         // -1: scan stopped
         // >0: registered and scan started
         private int mClientIf;
-        private boolean isAdvertising = false;
+        private boolean mIsAdvertising = false;
 
         public AdvertiseCallbackWrapper(AdvertiseCallback advertiseCallback,
                 AdvertiseData advertiseData, AdvertiseData scanResponse,
@@ -270,7 +271,7 @@ public final class BluetoothLeAdvertiser {
                 } catch (InterruptedException e) {
                     Log.e(TAG, "Callback reg wait interrupted: ", e);
                 }
-                started = (mClientIf > 0 && isAdvertising);
+                started = (mClientIf > 0 && mIsAdvertising);
             }
             return started;
         }
@@ -282,7 +283,7 @@ public final class BluetoothLeAdvertiser {
                 } catch (InterruptedException e) {
                     Log.e(TAG, "Callback reg wait interrupted: " + e);
                 }
-                return !isAdvertising;
+                return !mIsAdvertising;
             }
         }
 
@@ -312,155 +313,35 @@ public final class BluetoothLeAdvertiser {
         }
 
         @Override
-        public void onClientConnectionState(int status, int clientIf,
-                boolean connected, String address) {
-            // no op
-        }
-
-        @Override
-        public void onScanResult(String address, int rssi, byte[] advData) {
-            // no op
-        }
-
-        @Override
-        public void onGetService(String address, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid) {
-            // no op
-        }
-
-        @Override
-        public void onGetIncludedService(String address, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int inclSrvcType, int inclSrvcInstId,
-                ParcelUuid inclSrvcUuid) {
-            // no op
-        }
-
-        @Override
-        public void onGetCharacteristic(String address, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid,
-                int charProps) {
-            // no op
-        }
-
-        @Override
-        public void onGetDescriptor(String address, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid,
-                int descInstId, ParcelUuid descUuid) {
-            // no op
-        }
-
-        @Override
-        public void onSearchComplete(String address, int status) {
-            // no op
-        }
-
-        @Override
-        public void onCharacteristicRead(String address, int status, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid, byte[] value) {
-            // no op
-        }
-
-        @Override
-        public void onCharacteristicWrite(String address, int status, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid) {
-            // no op
-        }
-
-        @Override
-        public void onNotify(String address, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid,
-                byte[] value) {
-            // no op
-        }
-
-        @Override
-        public void onDescriptorRead(String address, int status, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid,
-                int descInstId, ParcelUuid descrUuid, byte[] value) {
-            // no op
-        }
-
-        @Override
-        public void onDescriptorWrite(String address, int status, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid,
-                int descInstId, ParcelUuid descrUuid) {
-            // no op
-        }
-
-        @Override
-        public void onExecuteWrite(String address, int status) {
-            // no op
-        }
-
-        @Override
-        public void onReadRemoteRssi(String address, int rssi, int status) {
-            // no op
-        }
-
-        @Override
-        public void onMultiAdvertiseCallback(int status) {
-            // TODO: This logic needs to be re-visited to account
-            // for whether the scan has actually been started
-            // or not. Toggling the isAdvertising does not seem
-            // correct.
+        public void onMultiAdvertiseCallback(int status, boolean isStart,
+                AdvertiseSettings settings) {
             synchronized (this) {
-                if (status == AdvertiseCallback.ADVERTISE_SUCCESS) {
-                    isAdvertising = !isAdvertising;
-                    if (!isAdvertising) {
-                        try {
-                            mBluetoothGatt.unregisterClient(mClientIf);
-                            mClientIf = -1;
-                        } catch (RemoteException e) {
-                            Log.e(TAG, "remote exception when unregistering", e);
-                        }
+                if (isStart) {
+                    if (status == AdvertiseCallback.ADVERTISE_SUCCESS) {
+                        // Start success
+                        mAdvertiseCallback.onStartSuccess(settings);
+                        mIsAdvertising = true;
                     } else {
-                        mAdvertiseCallback.onStartSuccess(null);
+                        // Start failure.
+                        mAdvertiseCallback.onStartFailure(status);
                     }
                 } else {
-                    if (!isAdvertising)
-                        mAdvertiseCallback.onStartFailure(status);
+                    // unregister client for stop.
+                    try {
+                        mBluetoothGatt.unregisterClient(mClientIf);
+                        mClientIf = -1;
+                        mIsAdvertising = false;
+                    } catch (RemoteException e) {
+                        Log.e(TAG, "remote exception when unregistering", e);
+                    }
                 }
                 notifyAll();
             }
 
         }
-
-        /**
-         * Callback reporting LE ATT MTU.
-         *
-         * @hide
-         */
-        @Override
-        public void onConfigureMTU(String address, int mtu, int status) {
-            // no op
-        }
-
-        @Override
-        public void onConnectionCongested(String address, boolean congested) {
-            // no op
-        }
-
-        @Override
-        public void onBatchScanResults(List<ScanResult> results) {
-            // no op
-        }
-
-        @Override
-        public void onFoundOrLost(boolean onFound, String address, int rssi,
-                byte[] advData) {
-            // no op
-        }
     }
 
-    //TODO: move this api to a common util class.
+    // TODO: move this api to a common util class.
     private void checkAdapterState() {
         if (mBluetoothAdapter.getState() != mBluetoothAdapter.STATE_ON) {
             throw new IllegalStateException("BT Adapter is not turned ON");
index f100ddc..45e466f 100644 (file)
@@ -19,6 +19,7 @@ package android.bluetooth.le;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothGatt;
+import android.bluetooth.BluetoothGattCallbackWrapper;
 import android.bluetooth.IBluetoothGatt;
 import android.bluetooth.IBluetoothGattCallback;
 import android.bluetooth.IBluetoothManager;
@@ -186,7 +187,7 @@ public final class BluetoothLeScanner {
     /**
      * Bluetooth GATT interface callbacks
      */
-    private static class BleScanCallbackWrapper extends IBluetoothGattCallback.Stub {
+    private static class BleScanCallbackWrapper extends BluetoothGattCallbackWrapper {
         private static final int REGISTRATION_CALLBACK_TIMEOUT_SECONDS = 5;
 
         private final ScanCallback mScanCallback;
@@ -284,37 +285,26 @@ public final class BluetoothLeScanner {
             }
         }
 
-        @Override
-        public void onClientConnectionState(int status, int clientIf,
-                boolean connected, String address) {
-            // no op
-        }
-
         /**
          * Callback reporting an LE scan result.
          *
          * @hide
          */
         @Override
-        public void onScanResult(String address, int rssi, byte[] advData) {
+        public void onScanResult(final ScanResult scanResult) {
             if (DBG)
-                Log.d(TAG, "onScanResult() - Device=" + address + " RSSI=" + rssi);
+                Log.d(TAG, "onScanResult() - " + scanResult.toString());
 
             // Check null in case the scan has been stopped
             synchronized (this) {
                 if (mClientIf <= 0)
                     return;
             }
-            BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(
-                    address);
-            long scanNanos = SystemClock.elapsedRealtimeNanos();
-            final ScanResult result = new ScanResult(device, ScanRecord.parseFromBytes(advData),
-                    rssi, scanNanos);
             Handler handler = new Handler(Looper.getMainLooper());
             handler.post(new Runnable() {
                     @Override
                 public void run() {
-                    mScanCallback.onScanResult(ScanSettings.CALLBACK_TYPE_ALL_MATCHES, result);
+                    mScanCallback.onScanResult(ScanSettings.CALLBACK_TYPE_ALL_MATCHES, scanResult);
                 }
             });
 
@@ -332,104 +322,6 @@ public final class BluetoothLeScanner {
         }
 
         @Override
-        public void onGetService(String address, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid) {
-            // no op
-        }
-
-        @Override
-        public void onGetIncludedService(String address, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int inclSrvcType, int inclSrvcInstId,
-                ParcelUuid inclSrvcUuid) {
-            // no op
-        }
-
-        @Override
-        public void onGetCharacteristic(String address, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid,
-                int charProps) {
-            // no op
-        }
-
-        @Override
-        public void onGetDescriptor(String address, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid,
-                int descInstId, ParcelUuid descUuid) {
-            // no op
-        }
-
-        @Override
-        public void onSearchComplete(String address, int status) {
-            // no op
-        }
-
-        @Override
-        public void onCharacteristicRead(String address, int status, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid, byte[] value) {
-            // no op
-        }
-
-        @Override
-        public void onCharacteristicWrite(String address, int status, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid) {
-            // no op
-        }
-
-        @Override
-        public void onNotify(String address, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid,
-                byte[] value) {
-            // no op
-        }
-
-        @Override
-        public void onDescriptorRead(String address, int status, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid,
-                int descInstId, ParcelUuid descrUuid, byte[] value) {
-            // no op
-        }
-
-        @Override
-        public void onDescriptorWrite(String address, int status, int srvcType,
-                int srvcInstId, ParcelUuid srvcUuid,
-                int charInstId, ParcelUuid charUuid,
-                int descInstId, ParcelUuid descrUuid) {
-            // no op
-        }
-
-        @Override
-        public void onExecuteWrite(String address, int status) {
-            // no op
-        }
-
-        @Override
-        public void onReadRemoteRssi(String address, int rssi, int status) {
-            // no op
-        }
-
-        @Override
-        public void onMultiAdvertiseCallback(int status) {
-            // no op
-        }
-
-        @Override
-        public void onConfigureMTU(String address, int mtu, int status) {
-            // no op
-        }
-
-        @Override
-        public void onConnectionCongested(String address, boolean congested) {
-            // no op
-        }
-
-        @Override
         public void onFoundOrLost(boolean onFound, String address, int rssi,
                 byte[] advData) {
             if (DBG) {