diff options
author | Adam Lesinski <adamlesinski@google.com> | 2016-01-15 18:14:47 -0800 |
---|---|---|
committer | Adam Lesinski <adamlesinski@google.com> | 2016-01-18 18:19:08 -0800 |
commit | d4dc70d2d425b0e98eb2150af1e4f3b5a0afe35e (patch) | |
tree | 6a5bf11905bf51ad1d39f04d02d1c1634998dc14 /framework/java/android/bluetooth/le/BluetoothLeScanner.java | |
parent | 031d983a4d68e80b06898b5ac9daed27c8a61f33 (diff) |
Add WorkSource to BLE scanning API
This will allow apps that do work on behalf of others to correctly blame
those apps for the power implications of BLE scanning.
Bug:22718669
Change-Id: Ib64af0b6a5d37721a6067ac4e5c39c01f921b56b
Diffstat (limited to 'framework/java/android/bluetooth/le/BluetoothLeScanner.java')
-rw-r--r-- | framework/java/android/bluetooth/le/BluetoothLeScanner.java | 64 |
1 files changed, 53 insertions, 11 deletions
diff --git a/framework/java/android/bluetooth/le/BluetoothLeScanner.java b/framework/java/android/bluetooth/le/BluetoothLeScanner.java index 2ba87744d0..03449ccec3 100644 --- a/framework/java/android/bluetooth/le/BluetoothLeScanner.java +++ b/framework/java/android/bluetooth/le/BluetoothLeScanner.java @@ -29,6 +29,7 @@ import android.os.Handler; import android.os.Looper; import android.os.ParcelUuid; import android.os.RemoteException; +import android.os.WorkSource; import android.util.Log; import java.util.ArrayList; @@ -89,9 +90,6 @@ public final class BluetoothLeScanner { */ @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) public void startScan(final ScanCallback callback) { - if (callback == null) { - throw new IllegalArgumentException("callback is null"); - } startScan(null, new ScanSettings.Builder().build(), callback); } @@ -112,14 +110,53 @@ public final class BluetoothLeScanner { @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) public void startScan(List<ScanFilter> filters, ScanSettings settings, final ScanCallback callback) { - startScan(filters, settings, callback, null); + startScan(filters, settings, null, callback, null); + } + + /** + * Start Bluetooth LE scan. Same as {@link #startScan(ScanCallback)} but allows the caller to + * specify on behalf of which application(s) the work is being done. + * + * @param workSource {@link WorkSource} identifying the application(s) for which to blame for + * the scan. + * @param callback Callback used to deliver scan results. + * @hide + */ + @SystemApi + @RequiresPermission(allOf = { + Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.UPDATE_DEVICE_STATS }) + public void startScanFromSource(final WorkSource workSource, final ScanCallback callback) { + startScanFromSource(null, new ScanSettings.Builder().build(), workSource, callback); + } + + /** + * Start Bluetooth LE scan. Same as {@link #startScan(List, ScanSettings, ScanCallback)} but + * allows the caller to specify on behalf of which application(s) the work is being done. + * + * @param filters {@link ScanFilter}s for finding exact BLE devices. + * @param settings Settings for the scan. + * @param workSource {@link WorkSource} identifying the application(s) for which to blame for + * the scan. + * @param callback Callback used to deliver scan results. + * @hide + */ + @SystemApi + @RequiresPermission(allOf = { + Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.UPDATE_DEVICE_STATS }) + public void startScanFromSource(List<ScanFilter> filters, ScanSettings settings, + final WorkSource workSource, final ScanCallback callback) { + startScan(filters, settings, workSource, callback, null); } private void startScan(List<ScanFilter> filters, ScanSettings settings, - final ScanCallback callback, List<List<ResultStorageDescriptor>> resultStorages) { + final WorkSource workSource, final ScanCallback callback, + List<List<ResultStorageDescriptor>> resultStorages) { BluetoothLeUtils.checkAdapterStateOn(mBluetoothAdapter); - if (settings == null || callback == null) { - throw new IllegalArgumentException("settings or callback is null"); + if (callback == null) { + throw new IllegalArgumentException("callback is null"); + } + if (settings == null) { + throw new IllegalArgumentException("settings is null"); } synchronized (mLeScanClients) { if (mLeScanClients.containsKey(callback)) { @@ -152,7 +189,7 @@ public final class BluetoothLeScanner { return; } BleScanCallbackWrapper wrapper = new BleScanCallbackWrapper(gatt, filters, - settings, callback, resultStorages); + settings, workSource, callback, resultStorages); wrapper.startRegisteration(); } } @@ -215,7 +252,7 @@ public final class BluetoothLeScanner { scanFilters.add(filter.getFilter()); scanStorages.add(filter.getStorageDescriptors()); } - startScan(scanFilters, settings, callback, scanStorages); + startScan(scanFilters, settings, null, callback, scanStorages); } /** @@ -235,6 +272,7 @@ public final class BluetoothLeScanner { private final ScanCallback mScanCallback; private final List<ScanFilter> mFilters; + private final WorkSource mWorkSource; private ScanSettings mSettings; private IBluetoothGatt mBluetoothGatt; private List<List<ResultStorageDescriptor>> mResultStorages; @@ -246,10 +284,12 @@ public final class BluetoothLeScanner { public BleScanCallbackWrapper(IBluetoothGatt bluetoothGatt, List<ScanFilter> filters, ScanSettings settings, - ScanCallback scanCallback, List<List<ResultStorageDescriptor>> resultStorages) { + WorkSource workSource, ScanCallback scanCallback, + List<List<ResultStorageDescriptor>> resultStorages) { mBluetoothGatt = bluetoothGatt; mFilters = filters; mSettings = settings; + mWorkSource = workSource; mScanCallback = scanCallback; mClientIf = 0; mResultStorages = resultStorages; @@ -322,7 +362,9 @@ public final class BluetoothLeScanner { mClientIf = clientIf; try { mBluetoothGatt.startScan(mClientIf, false, mSettings, mFilters, - mResultStorages, ActivityThread.currentOpPackageName()); + mWorkSource, mResultStorages, + ActivityThread.currentOpPackageName()); + } catch (RemoteException e) { Log.e(TAG, "fail to start le scan: " + e); mClientIf = -1; |