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

Commit c9ba8c7e authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Change how services are passed up to the stack

Right now we pass all services, characteristics and descriptors one by one.
This patch changes that - now we pass whole GATT database at once.

Bug: 27455533
Change-Id: Ie42cd80072538e411904b9c9b011a978f26158b9
parent f3793ce8
Loading
Loading
Loading
Loading
+27 −93
Original line number Original line Diff line number Diff line
@@ -197,109 +197,43 @@ public final class BluetoothGatt implements BluetoothProfile {
            }
            }


            /**
            /**
             * A new GATT service has been discovered.
             * Remote search has been completed.
             * The service is added to the internal list and the search
             * The internal object structure should now reflect the state
             * continues.
             * of the remote device database. Let the application know that
             * we are done at this point.
             * @hide
             * @hide
             */
             */
            public void onGetService(String address, int srvcType,
            public void onSearchComplete(String address, List<BluetoothGattService> services,
                                     int srvcInstId, ParcelUuid srvcUuid) {
                                         int status) {
                if (VDBG) Log.d(TAG, "onGetService() - Device=" + address + " UUID=" + srvcUuid);
                if (DBG) Log.d(TAG, "onSearchComplete() = Device=" + address + " Status=" + status);
                if (!address.equals(mDevice.getAddress())) {
                if (!address.equals(mDevice.getAddress())) {
                    return;
                    return;
                }
                }
                mServices.add(new BluetoothGattService(mDevice, srvcUuid.getUuid(),
                                                       srvcInstId, srvcType));
            }


            /**
                for (BluetoothGattService s : services) {
             * An included service has been found durig GATT discovery.
                    //services we receive don't have device set properly.
             * The included service is added to the respective parent.
                    s.setDevice(mDevice);
             * @hide
             */
            public void onGetIncludedService(String address, int srvcType,
                                             int srvcInstId, ParcelUuid srvcUuid,
                                             int inclSrvcType, int inclSrvcInstId,
                                             ParcelUuid inclSrvcUuid) {
                if (VDBG) Log.d(TAG, "onGetIncludedService() - Device=" + address
                    + " UUID=" + srvcUuid + " Included=" + inclSrvcUuid);

                if (!address.equals(mDevice.getAddress())) {
                    return;
                }
                }
                BluetoothGattService service = getService(mDevice,
                        srvcUuid.getUuid(), srvcInstId, srvcType);
                BluetoothGattService includedService = getService(mDevice,
                        inclSrvcUuid.getUuid(), inclSrvcInstId, inclSrvcType);


                if (service != null && includedService != null) {
                mServices.addAll(services);
                    service.addIncludedService(includedService);
                }
            }


            /**
                // Fix references to included services, as they doesn't point to right objects.
             * A new GATT characteristic has been discovered.
                for (BluetoothGattService fixedService : mServices) {
             * Add the new characteristic to the relevant service and continue
                    ArrayList<BluetoothGattService> includedServices =
             * the remote device inspection.
                        new ArrayList(fixedService.getIncludedServices());
             * @hide
                    fixedService.getIncludedServices().clear();
             */
            public void onGetCharacteristic(String address, int srvcType,
                             int srvcInstId, ParcelUuid srvcUuid,
                             int charInstId, ParcelUuid charUuid,
                             int charProps) {
                if (VDBG) Log.d(TAG, "onGetCharacteristic() - Device=" + address + " UUID=" +
                               charUuid);


                if (!address.equals(mDevice.getAddress())) {
                    for(BluetoothGattService brokenRef : includedServices) {
                    return;
                        BluetoothGattService includedService = getService(mDevice,
                            brokenRef.getUuid(), brokenRef.getInstanceId(), brokenRef.getType());
                        if (includedService != null) {
                            fixedService.addIncludedService(includedService);
                        } else {
                            Log.e(TAG, "Broken GATT database: can't find included service.");
                        }
                        }
                BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
                                                          srvcInstId, srvcType);
                if (service != null) {
                    service.addCharacteristic(new BluetoothGattCharacteristic(
                           service, charUuid.getUuid(), charInstId, charProps, 0));
                    }
                    }
                }
                }


            /**
             * A new GATT descriptor has been discovered.
             * Finally, add the descriptor to the related characteristic.
             * This should conclude the remote device update.
             * @hide
             */
            public void onGetDescriptor(String address, int srvcType,
                             int srvcInstId, ParcelUuid srvcUuid,
                             int charInstId, ParcelUuid charUuid,
                             int descrInstId, ParcelUuid descUuid) {
                if (VDBG) Log.d(TAG, "onGetDescriptor() - Device=" + address + " UUID=" + descUuid);

                if (!address.equals(mDevice.getAddress())) {
                    return;
                }
                BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
                                                          srvcInstId, srvcType);
                if (service == null) return;

                BluetoothGattCharacteristic characteristic = service.getCharacteristic(
                    charUuid.getUuid(), charInstId);
                if (characteristic == null) return;

                characteristic.addDescriptor(new BluetoothGattDescriptor(
                    characteristic, descUuid.getUuid(), descrInstId, 0));
            }

            /**
             * Remote search has been completed.
             * The internal object structure should now reflect the state
             * of the remote device database. Let the application know that
             * we are done at this point.
             * @hide
             */
            public void onSearchComplete(String address, int status) {
                if (DBG) Log.d(TAG, "onSearchComplete() = Device=" + address + " Status=" + status);
                if (!address.equals(mDevice.getAddress())) {
                    return;
                }
                try {
                try {
                    mCallback.onServicesDiscovered(BluetoothGatt.this, status);
                    mCallback.onServicesDiscovered(BluetoothGatt.this, status);
                } catch (Exception ex) {
                } catch (Exception ex) {
+3 −24
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ package android.bluetooth;


import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanResult;
import android.bluetooth.BluetoothGattService;
import android.os.ParcelUuid;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.os.RemoteException;


@@ -48,30 +49,8 @@ public class BluetoothGattCallbackWrapper extends IBluetoothGattCallback.Stub {
    }
    }


    @Override
    @Override
    public void onGetService(String address, int srvcType, int srvcInstId, ParcelUuid srvcUuid)
    public void onSearchComplete(String address, List<BluetoothGattService> services,
            throws RemoteException {
            int status) 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
    @Override
+19 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2016 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;

parcelable BluetoothGattCharacteristic;
+57 −1
Original line number Original line Diff line number Diff line
@@ -15,6 +15,9 @@
 */
 */
package android.bluetooth;
package android.bluetooth;


import android.os.Parcel;
import android.os.Parcelable;
import android.os.ParcelUuid;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.UUID;
import java.util.UUID;
@@ -26,7 +29,7 @@ import java.util.UUID;
 * {@link BluetoothGattService}. The characteristic contains a value as well as
 * {@link BluetoothGattService}. The characteristic contains a value as well as
 * additional information and optional GATT descriptors, {@link BluetoothGattDescriptor}.
 * additional information and optional GATT descriptors, {@link BluetoothGattDescriptor}.
 */
 */
public class BluetoothGattCharacteristic {
public class BluetoothGattCharacteristic implements Parcelable {


    /**
    /**
     * Characteristic proprty: Characteristic is broadcastable.
     * Characteristic proprty: Characteristic is broadcastable.
@@ -242,6 +245,15 @@ public class BluetoothGattCharacteristic {
        initCharacteristic(service, uuid, instanceId, properties, permissions);
        initCharacteristic(service, uuid, instanceId, properties, permissions);
    }
    }


    /**
     * Create a new BluetoothGattCharacteristic
     * @hide
     */
    public BluetoothGattCharacteristic(UUID uuid, int instanceId,
                                       int properties, int permissions) {
        initCharacteristic(null, uuid, instanceId, properties, permissions);
    }

    private void initCharacteristic(BluetoothGattService service,
    private void initCharacteristic(BluetoothGattService service,
                                    UUID uuid, int instanceId,
                                    UUID uuid, int instanceId,
                                    int properties, int permissions) {
                                    int properties, int permissions) {
@@ -260,6 +272,50 @@ public class BluetoothGattCharacteristic {
        }
        }
    }
    }


    /**
     * @hide
     */
    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeParcelable(new ParcelUuid(mUuid), 0);
        out.writeInt(mInstance);
        out.writeInt(mProperties);
        out.writeInt(mPermissions);
        out.writeTypedList(mDescriptors);
    }

    public static final Parcelable.Creator<BluetoothGattCharacteristic> CREATOR
            = new Parcelable.Creator<BluetoothGattCharacteristic>() {
        public BluetoothGattCharacteristic createFromParcel(Parcel in) {
            return new BluetoothGattCharacteristic(in);
        }

        public BluetoothGattCharacteristic[] newArray(int size) {
            return new BluetoothGattCharacteristic[size];
        }
    };

    private BluetoothGattCharacteristic(Parcel in) {
        mUuid = ((ParcelUuid)in.readParcelable(null)).getUuid();
        mInstance = in.readInt();
        mProperties = in.readInt();
        mPermissions = in.readInt();

        mDescriptors = new ArrayList<BluetoothGattDescriptor>();

        ArrayList<BluetoothGattDescriptor> descs =
                in.createTypedArrayList(BluetoothGattDescriptor.CREATOR);
        if (descs != null) {
            for (BluetoothGattDescriptor desc: descs) {
                desc.setCharacteristic(this);
                mDescriptors.add(desc);
            }
        }
    }

    /**
    /**
     * Returns the deisred key size.
     * Returns the deisred key size.
     * @hide
     * @hide
+19 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2016 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;

parcelable BluetoothGattDescriptor;
Loading