2 * Copyright (C) 2017 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings.anomaly.tester.utils;
19 import android.bluetooth.BluetoothAdapter;
20 import android.bluetooth.le.BluetoothLeScanner;
21 import android.bluetooth.le.ScanCallback;
22 import android.bluetooth.le.ScanResult;
23 import android.bluetooth.le.ScanSettings;
24 import android.content.Context;
25 import android.util.Log;
27 import java.util.List;
30 * Actions to generate anomaly.
32 public class AnomalyActions {
33 private static final String TAG = AnomalyActions.class.getSimpleName();
35 public static final String KEY_ACTION = "action";
36 public static final String KEY_DURATION_MS = "duration_ms";
37 public static final String KEY_RESULT_RECEIVER = "result_receiver";
39 public static final String ACTION_BLE_SCAN_UNOPTIMIZED = "action.ble_scan_unoptimized";
41 public static void doAction(Context ctx, String actionCode, long durationMs) {
42 if (actionCode == null) {
43 Log.e(TAG, "Intent was missing action.");
47 case ACTION_BLE_SCAN_UNOPTIMIZED:
48 doUnoptimizedBleScan(ctx, durationMs);
51 Log.e(TAG, "Intent had invalid action");
55 private static void doUnoptimizedBleScan(Context ctx, long durationMs) {
56 ScanSettings scanSettings = new ScanSettings.Builder()
57 .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
59 // perform ble scanning
60 BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
61 if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled() ) {
62 Log.e(TAG, "Device does not support Bluetooth or Bluetooth not enabled");
65 BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
66 if (bleScanner == null) {
67 Log.e(TAG, "Cannot access BLE scanner");
71 ScanCallback scanCallback = new ScanCallback() {
73 public void onScanResult(int callbackType, ScanResult result) {
74 Log.v(TAG, "called onScanResult");
78 public void onScanFailed(int errorCode) {
79 Log.v(TAG, "called onScanFailed");
83 public void onBatchScanResults(List<ScanResult> results) {
84 Log.v(TAG, "called onBatchScanResults");
88 bleScanner.startScan(null, scanSettings, scanCallback);
90 Thread.sleep(durationMs);
91 } catch (InterruptedException e) {
92 Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
94 bleScanner.stopScan(scanCallback);