Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e0d4afb2 authored by Wei Wang's avatar Wei Wang
Browse files

Add a default impelementation of IBluetoothGattCallback(1/2).

Also cleaned up a few TODOs and fixed bug.
b/16410260 AdvertiseCallback null onSuccess callback

Change-Id: I75a582c03be1e2ef6964c2cbeba42aaaf9f9e17c
parent bfa96fb3
Loading
Loading
Loading
Loading
+2 −42
Original line number Diff line number Diff line
@@ -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
@@ -197,14 +196,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
@@ -599,23 +590,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,
+131 −0
Original line number Diff line number Diff line
/*
 * 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 {
    }

}
+4 −2
Original line number Diff line number Diff line
@@ -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,
+23 −142
Original line number Diff line number Diff line
@@ -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,152 +313,32 @@ 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 (isStart) {
                    if (status == AdvertiseCallback.ADVERTISE_SUCCESS) {
                    isAdvertising = !isAdvertising;
                    if (!isAdvertising) {
                        // Start success
                        mAdvertiseCallback.onStartSuccess(settings);
                        mIsAdvertising = true;
                    } else {
                        // Start failure.
                        mAdvertiseCallback.onStartFailure(status);
                    }
                } else {
                    // unregister client for stop.
                    try {
                        mBluetoothGatt.unregisterClient(mClientIf);
                        mClientIf = -1;
                        mIsAdvertising = false;
                    } catch (RemoteException e) {
                        Log.e(TAG, "remote exception when unregistering", e);
                    }
                    } else {
                        mAdvertiseCallback.onStartSuccess(null);
                    }
                } else {
                    if (!isAdvertising)
                        mAdvertiseCallback.onStartFailure(status);
                }
                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.
+5 −113
Original line number Diff line number Diff line
@@ -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);
                }
            });

@@ -331,104 +321,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) {