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

Commit 3d7e334a authored by Wei Wang's avatar Wei Wang
Browse files

Add device capability config for new BLE features(MA, scan filter).(2/3)

Fixes b/15387925.

Change-Id: I7cbcdd340fca7965f2be2c0d2a938e7646dc6849
parent f2ecf982
Loading
Loading
Loading
Loading
+23 −11
Original line number Original line Diff line number Diff line
@@ -25,11 +25,11 @@ import android.bluetooth.IBluetoothGatt;
import android.bluetooth.IBluetoothGattCallback;
import android.bluetooth.IBluetoothGattCallback;
import android.bluetooth.IBluetoothGattServerCallback;
import android.bluetooth.IBluetoothGattServerCallback;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertisementData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.ScanSettings;
import android.bluetooth.le.AdvertisementData;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Intent;
import android.content.Intent;
import android.os.IBinder;
import android.os.IBinder;
import android.os.Message;
import android.os.Message;
@@ -39,6 +39,7 @@ import android.os.SystemClock;
import android.util.Log;
import android.util.Log;


import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.internal.R;


import java.nio.ByteBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ByteOrder;
@@ -88,9 +89,6 @@ public class GattService extends ProfileService {
    static final int SCAN_FILTER_ENABLED = 1;
    static final int SCAN_FILTER_ENABLED = 1;
    static final int SCAN_FILTER_MODIFIED = 2;
    static final int SCAN_FILTER_MODIFIED = 2;


    // TODO: query the number from hardware instead of hard-coded here.
    private static final int MAX_FILTER_SIZE = 1;

    /**
    /**
     * Search queue to serialize remote onbject inspection.
     * Search queue to serialize remote onbject inspection.
     */
     */
@@ -123,6 +121,7 @@ public class GattService extends ProfileService {
    private Integer mAdvertisingState = BluetoothAdapter.STATE_ADVERTISE_STOPPED;
    private Integer mAdvertisingState = BluetoothAdapter.STATE_ADVERTISE_STOPPED;
    private final Object mLock = new Object();
    private final Object mLock = new Object();
    private static int lastConfiguredDutyCycle = 0;
    private static int lastConfiguredDutyCycle = 0;
    private int mMaxScanFilters;


    /**
    /**
     * Pending service declaration queue
     * Pending service declaration queue
@@ -205,6 +204,7 @@ public class GattService extends ProfileService {
        if (DBG) Log.d(TAG, "start()");
        if (DBG) Log.d(TAG, "start()");
        initializeNative();
        initializeNative();
        mStateMachine = GattServiceStateMachine.make(this);
        mStateMachine = GattServiceStateMachine.make(this);
        mMaxScanFilters = getResources().getInteger(R.integer.config_bluetooth_max_scan_filters);
        return true;
        return true;
    }
    }


@@ -1254,12 +1254,17 @@ public class GattService extends ProfileService {
                mScanQueue.add(new ScanClient(appIf, isServer, settings, filters));
                mScanQueue.add(new ScanClient(appIf, isServer, settings, filters));
            }
            }
            Set<ScanFilter> newFilters = configureScanFiltersLocked();
            Set<ScanFilter> newFilters = configureScanFiltersLocked();
            if (isScaning) {
                // Reset scan filters if BLE scan was started and scan filters changed.
                if (!Objects.deepEquals(newFilters, mScanFilters)) {
                if (!Objects.deepEquals(newFilters, mScanFilters)) {
                    mScanFilters = newFilters;
                    mScanFilters = newFilters;
                    // Restart scan using new filters.
                    // Restart scan using new filters.
                if (isScaning) {
                    sendStopScanMessage();
                    sendStopScanMessage();
                    sendStartScanMessage(mScanFilters);
                }
                }
            } else {
                // Always start scanning with new filters if scan not started yet.
                mScanFilters = newFilters;
                sendStartScanMessage(mScanFilters);
                sendStartScanMessage(mScanFilters);
            }
            }
        }
        }
@@ -1322,13 +1327,20 @@ public class GattService extends ProfileService {
            filters.addAll(client.filters);
            filters.addAll(client.filters);
        }
        }
        // TODO: find a better way to handle too many filters.
        // TODO: find a better way to handle too many filters.
        if (filters.size() > MAX_FILTER_SIZE) {
        if (filters.size() > mMaxScanFilters) {
            if (DBG) Log.d(TAG, "filters size > " + MAX_FILTER_SIZE + ", clearing filters");
            if (DBG) Log.d(TAG, "filters size > " + mMaxScanFilters + ", clearing filters");
            filters = new HashSet<ScanFilter>();
            filters = new HashSet<ScanFilter>();
        }
        }
        return filters;
        return filters;
    }
    }


    /**
     * Returns whether scan filter is supported.
     */
    boolean isScanFilterSupported() {
        return mMaxScanFilters > 0;
    }

    private void sendStartScanMessage(Set<ScanFilter> filters) {
    private void sendStartScanMessage(Set<ScanFilter> filters) {
        Message message = mStateMachine.obtainMessage(GattServiceStateMachine.START_BLE_SCAN);
        Message message = mStateMachine.obtainMessage(GattServiceStateMachine.START_BLE_SCAN);
        message.obj = filters;
        message.obj = filters;
+15 −7
Original line number Original line Diff line number Diff line
@@ -2,13 +2,14 @@
package com.android.bluetooth.gatt;
package com.android.bluetooth.gatt;


import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertisementData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.AdvertisementData;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanFilter;
import android.os.Message;
import android.os.Message;
import android.os.ParcelUuid;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.os.RemoteException;


import com.android.internal.R;
import com.android.internal.util.State;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;
import com.android.internal.util.StateMachine;


@@ -54,9 +55,6 @@ final class GattServiceStateMachine extends StateMachine {
    static final int ADD_SCAN_FILTER = 16;
    static final int ADD_SCAN_FILTER = 16;
    static final int ENABLE_SCAN_FILTER = 17;
    static final int ENABLE_SCAN_FILTER = 17;


    // TODO: This should be queried from hardware.
    private final int MAX_ADVERTISERS = 4;

    // TODO: Remove this once stack callback is stable.
    // TODO: Remove this once stack callback is stable.
    private static final int OPERATION_TIMEOUT = 101;
    private static final int OPERATION_TIMEOUT = 101;
    private static final int TIMEOUT_MILLIS = 3000;
    private static final int TIMEOUT_MILLIS = 3000;
@@ -100,6 +98,7 @@ final class GattServiceStateMachine extends StateMachine {
    private final Idle mIdle;
    private final Idle mIdle;
    private final ScanStarting mScanStarting;
    private final ScanStarting mScanStarting;
    private final AdvertiseStarting mAdvertiseStarting;
    private final AdvertiseStarting mAdvertiseStarting;
    private final int mMaxAdvertisers;


    private GattServiceStateMachine(GattService context) {
    private GattServiceStateMachine(GattService context) {
        super(TAG);
        super(TAG);
@@ -118,6 +117,8 @@ final class GattServiceStateMachine extends StateMachine {


        // Initial state is idle.
        // Initial state is idle.
        setInitialState(mIdle);
        setInitialState(mIdle);
        mMaxAdvertisers = mService.getResources().getInteger(
                R.integer.config_bluetooth_max_advertisers);
    }
    }


    /**
    /**
@@ -159,7 +160,12 @@ final class GattServiceStateMachine extends StateMachine {
                case START_BLE_SCAN:
                case START_BLE_SCAN:
                    // TODO: check whether scan is already started for the app.
                    // TODO: check whether scan is already started for the app.
                    // Send the enable scan message to starting state for processing.
                    // Send the enable scan message to starting state for processing.
                    Message newMessage = obtainMessage(CLEAR_SCAN_FILTER);
                    Message newMessage;
                    if (mService.isScanFilterSupported()) {
                        newMessage = obtainMessage(CLEAR_SCAN_FILTER);
                    } else {
                        newMessage = obtainMessage(ENABLE_BLE_SCAN);
                    }
                    newMessage.obj = message.obj;
                    newMessage.obj = message.obj;
                    sendMessage(newMessage);
                    sendMessage(newMessage);
                    transitionTo(mScanStarting);
                    transitionTo(mScanStarting);
@@ -182,7 +188,7 @@ final class GattServiceStateMachine extends StateMachine {
                        transitionTo(mIdle);
                        transitionTo(mIdle);
                        break;
                        break;
                    }
                    }
                    if (mAdvertiseClients.size() >= MAX_ADVERTISERS) {
                    if (mAdvertiseClients.size() >= mMaxAdvertisers) {
                        loge("too many advertisier, current size : " + mAdvertiseClients.size());
                        loge("too many advertisier, current size : " + mAdvertiseClients.size());
                        try {
                        try {
                            mService.onMultipleAdvertiseCallback(client.clientIf,
                            mService.onMultipleAdvertiseCallback(client.clientIf,
@@ -279,7 +285,9 @@ final class GattServiceStateMachine extends StateMachine {
                    break;
                    break;
                case ENABLE_BLE_SCAN:
                case ENABLE_BLE_SCAN:
                    gattClientScanNative(true);
                    gattClientScanNative(true);
                    if (mService.isScanFilterSupported()) {
                        removeMessages(OPERATION_TIMEOUT);
                        removeMessages(OPERATION_TIMEOUT);
                    }
                    transitionTo(mIdle);
                    transitionTo(mIdle);
                    break;
                    break;
                case OPERATION_TIMEOUT:
                case OPERATION_TIMEOUT: