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

Commit 875a1cee authored by Ömer Faruk Yılmaz's avatar Ömer Faruk Yılmaz Committed by Gerrit Code Review
Browse files

Merge "Use new BluetoothScan binder instead of BluetoothGatt" into main

parents 9b62dcc3 196c21d6
Loading
Loading
Loading
Loading
+33 −5
Original line number Diff line number Diff line
@@ -2819,12 +2819,21 @@ public final class BluetoothAdapter {
            return false;
        }
        try {
            if (Flags.scanManagerRefactor()) {
                IBluetoothScan scan = getBluetoothScan();
                if (scan == null) {
                    // BLE is not supported
                    return false;
                }
                return scan.numHwTrackFiltersAvailable(mAttributionSource) != 0;
            } else {
                IBluetoothGatt iGatt = getBluetoothGatt();
                if (iGatt == null) {
                    // BLE is not supported
                    return false;
                }
                return iGatt.numHwTrackFiltersAvailable(mAttributionSource) != 0;
            }
        } catch (RemoteException e) {
            Log.e(TAG, "", e);
        }
@@ -4242,6 +4251,25 @@ public final class BluetoothAdapter {
        return null;
    }

    /**
     * Return a binder to BluetoothScan
     *
     * @hide
     */
    public @Nullable IBluetoothScan getBluetoothScan() {
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                return IBluetoothScan.Stub.asInterface(mService.getBluetoothScan());
            }
        } catch (RemoteException e) {
            Log.e(TAG, e + "\n" + Log.getStackTraceString(new Throwable()));
        } finally {
            mServiceLock.readLock().unlock();
        }
        return null;
    }

    /** Return a binder to a Profile service */
    private @Nullable IBinder getProfile(int profile) {
        mServiceLock.readLock().lock();
+74 −20
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.bluetooth.Attributable;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.IBluetoothGatt;
import android.bluetooth.IBluetoothScan;
import android.bluetooth.annotations.RequiresBluetoothLocationPermission;
import android.bluetooth.annotations.RequiresBluetoothScanPermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
@@ -37,6 +38,8 @@ import android.os.RemoteException;
import android.os.WorkSource;
import android.util.Log;

import com.android.bluetooth.flags.Flags;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -253,9 +256,20 @@ public final class BluetoothLeScanner {
                return postCallbackErrorOrReturn(
                        callback, ScanCallback.SCAN_FAILED_ALREADY_STARTED);
            }
            IBluetoothGatt gatt = mBluetoothAdapter.getBluetoothGatt();
            IBluetoothScan scan = null;
            IBluetoothGatt gatt = null;
            if (Flags.scanManagerRefactor()) {
                scan = mBluetoothAdapter.getBluetoothScan();
                if (scan == null) {
                    return postCallbackErrorOrReturn(
                            callback, ScanCallback.SCAN_FAILED_INTERNAL_ERROR);
                }
            } else {
                gatt = mBluetoothAdapter.getBluetoothGatt();
                if (gatt == null) {
                return postCallbackErrorOrReturn(callback, ScanCallback.SCAN_FAILED_INTERNAL_ERROR);
                    return postCallbackErrorOrReturn(
                            callback, ScanCallback.SCAN_FAILED_INTERNAL_ERROR);
                }
            }
            if (!isSettingsConfigAllowedForScan(settings)) {
                return postCallbackErrorOrReturn(
@@ -271,11 +285,18 @@ public final class BluetoothLeScanner {
            }
            if (callback != null) {
                BleScanCallbackWrapper wrapper =
                        new BleScanCallbackWrapper(gatt, filters, settings, workSource, callback);
                        new BleScanCallbackWrapper(
                                gatt, scan, filters, settings, workSource, callback);
                wrapper.startRegistration();
            } else {
                try {
                    gatt.startScanForIntent(callbackIntent, settings, filters, mAttributionSource);
                    if (Flags.scanManagerRefactor()) {
                        scan.startScanForIntent(
                                callbackIntent, settings, filters, mAttributionSource);
                    } else {
                        gatt.startScanForIntent(
                                callbackIntent, settings, filters, mAttributionSource);
                    }
                } catch (RemoteException e) {
                    return ScanCallback.SCAN_FAILED_INTERNAL_ERROR;
                }
@@ -284,9 +305,7 @@ public final class BluetoothLeScanner {
        return ScanCallback.NO_ERROR;
    }

    /**
     * Stops an ongoing Bluetooth LE scan.
     */
    /** Stops an ongoing Bluetooth LE scan. */
    @RequiresLegacyBluetoothAdminPermission
    @RequiresBluetoothScanPermission
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_SCAN)
@@ -316,12 +335,21 @@ public final class BluetoothLeScanner {
    public void stopScan(PendingIntent callbackIntent) {
        BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter);
        try {
            if (Flags.scanManagerRefactor()) {
                IBluetoothScan scan = mBluetoothAdapter.getBluetoothScan();
                if (scan == null) {
                    Log.w(TAG, "stopScan called after bluetooth has been turned off");
                    return;
                }
                scan.stopScanForIntent(callbackIntent, mAttributionSource);
            } else {
                IBluetoothGatt gatt = mBluetoothAdapter.getBluetoothGatt();
                if (gatt == null) {
                    Log.w(TAG, "stopScan called after bluetooth has been turned off");
                    return;
                }
                gatt.stopScanForIntent(callbackIntent, mAttributionSource);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to stop scan", e);
        }
@@ -394,6 +422,7 @@ public final class BluetoothLeScanner {
        private final WorkSource mWorkSource;
        private ScanSettings mSettings;
        private IBluetoothGatt mBluetoothGatt;
        private IBluetoothScan mBluetoothScan;

        // mLeHandle 0: not registered
        // -2: registration failed because app is scanning to frequently
@@ -403,11 +432,13 @@ public final class BluetoothLeScanner {

        public BleScanCallbackWrapper(
                IBluetoothGatt bluetoothGatt,
                IBluetoothScan bluetoothScan,
                List<ScanFilter> filters,
                ScanSettings settings,
                WorkSource workSource,
                ScanCallback scanCallback) {
            mBluetoothGatt = bluetoothGatt;
            mBluetoothScan = bluetoothScan;
            mFilters = filters;
            mSettings = settings;
            mWorkSource = workSource;
@@ -421,7 +452,11 @@ public final class BluetoothLeScanner {
                // Scan stopped.
                if (mScannerId == -1 || mScannerId == -2) return;
                try {
                    if (Flags.scanManagerRefactor()) {
                        mBluetoothScan.registerScanner(this, mWorkSource, mAttributionSource);
                    } else {
                        mBluetoothGatt.registerScanner(this, mWorkSource, mAttributionSource);
                    }
                    wait(REGISTRATION_CALLBACK_TIMEOUT_MILLIS);
                } catch (InterruptedException | RemoteException e) {
                    Log.e(TAG, "application registration exception", e);
@@ -452,9 +487,15 @@ public final class BluetoothLeScanner {
                    return;
                }
                try {
                    if (Flags.scanManagerRefactor()) {
                        mBluetoothScan.stopScan(mScannerId, mAttributionSource);

                        mBluetoothScan.unregisterScanner(mScannerId, mAttributionSource);
                    } else {
                        mBluetoothGatt.stopScan(mScannerId, mAttributionSource);

                        mBluetoothGatt.unregisterScanner(mScannerId, mAttributionSource);
                    }
                } catch (RemoteException e) {
                    Log.e(TAG, "Failed to stop scan and unregister", e);
                }
@@ -470,7 +511,11 @@ public final class BluetoothLeScanner {
                    return;
                }
                try {
                    if (Flags.scanManagerRefactor()) {
                        mBluetoothScan.flushPendingBatchResults(mScannerId, mAttributionSource);
                    } else {
                        mBluetoothGatt.flushPendingBatchResults(mScannerId, mAttributionSource);
                    }
                } catch (RemoteException e) {
                    Log.e(TAG, "Failed to get pending scan results", e);
                }
@@ -493,12 +538,21 @@ public final class BluetoothLeScanner {
                    try {
                        if (mScannerId == -1) {
                            // Registration succeeds after timeout, unregister scanner.
                            if (Flags.scanManagerRefactor()) {
                                mBluetoothScan.unregisterScanner(scannerId, mAttributionSource);
                            } else {
                                mBluetoothGatt.unregisterScanner(scannerId, mAttributionSource);
                            }
                        } else {
                            mScannerId = scannerId;
                            if (Flags.scanManagerRefactor()) {
                                mBluetoothScan.startScan(
                                        mScannerId, mSettings, mFilters, mAttributionSource);
                            } else {
                                mBluetoothGatt.startScan(
                                        mScannerId, mSettings, mFilters, mAttributionSource);
                            }
                        }
                    } catch (RemoteException e) {
                        Log.e(TAG, "fail to start le scan: " + e);
                        mScannerId = -1;