use of android.bluetooth.le.ScanSettings in project android_frameworks_base by ResurrectionRemix.
the class KeyboardUI method startScanning.
private void startScanning() {
BluetoothLeScanner scanner = mLocalBluetoothAdapter.getBluetoothLeScanner();
ScanFilter filter = (new ScanFilter.Builder()).setDeviceName(mKeyboardName).build();
ScanSettings settings = (new ScanSettings.Builder()).setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT).setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(0).build();
mScanCallback = new KeyboardScanCallback();
scanner.startScan(Arrays.asList(filter), settings, mScanCallback);
Message abortMsg = mHandler.obtainMessage(MSG_BLE_ABORT_SCAN, ++mScanAttempt, 0);
mHandler.sendMessageDelayed(abortMsg, BLUETOOTH_SCAN_TIMEOUT_MILLIS);
}
use of android.bluetooth.le.ScanSettings in project android_frameworks_base by crdroidandroid.
the class BluetoothAdapter method startLeScan.
/**
* Starts a scan for Bluetooth LE devices, looking for devices that
* advertise given services.
*
* <p>Devices which advertise all specified services are reported using the
* {@link LeScanCallback#onLeScan} callback.
*
* <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
*
* @param serviceUuids Array of services to look for
* @param callback the callback LE scan results are delivered
* @return true, if the scan was started successfully
* @deprecated use {@link BluetoothLeScanner#startScan(List, ScanSettings, ScanCallback)}
* instead.
*/
@Deprecated
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean startLeScan(final UUID[] serviceUuids, final LeScanCallback callback) {
if (DBG)
Log.d(TAG, "startLeScan(): " + Arrays.toString(serviceUuids));
if (callback == null) {
if (DBG)
Log.e(TAG, "startLeScan: null callback");
return false;
}
BluetoothLeScanner scanner = getBluetoothLeScanner();
if (scanner == null) {
if (DBG)
Log.e(TAG, "startLeScan: cannot get BluetoothLeScanner");
return false;
}
synchronized (mLeScanClients) {
if (mLeScanClients.containsKey(callback)) {
if (DBG)
Log.e(TAG, "LE Scan has already started");
return false;
}
try {
IBluetoothGatt iGatt = mManagerService.getBluetoothGatt();
if (iGatt == null) {
// BLE is not supported
return false;
}
ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
if (callbackType != ScanSettings.CALLBACK_TYPE_ALL_MATCHES) {
// Should not happen.
Log.e(TAG, "LE Scan has already started");
return;
}
ScanRecord scanRecord = result.getScanRecord();
if (scanRecord == null) {
return;
}
if (serviceUuids != null) {
List<ParcelUuid> uuids = new ArrayList<ParcelUuid>();
for (UUID uuid : serviceUuids) {
uuids.add(new ParcelUuid(uuid));
}
List<ParcelUuid> scanServiceUuids = scanRecord.getServiceUuids();
if (scanServiceUuids == null || !scanServiceUuids.containsAll(uuids)) {
if (DBG)
Log.d(TAG, "uuids does not match");
return;
}
}
callback.onLeScan(result.getDevice(), result.getRssi(), scanRecord.getBytes());
}
};
ScanSettings settings = new ScanSettings.Builder().setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
List<ScanFilter> filters = new ArrayList<ScanFilter>();
if (serviceUuids != null && serviceUuids.length > 0) {
// Note scan filter does not support matching an UUID array so we put one
// UUID to hardware and match the whole array in callback.
ScanFilter filter = new ScanFilter.Builder().setServiceUuid(new ParcelUuid(serviceUuids[0])).build();
filters.add(filter);
}
scanner.startScan(filters, settings, scanCallback);
mLeScanClients.put(callback, scanCallback);
return true;
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
}
return false;
}
use of android.bluetooth.le.ScanSettings in project android_packages_apps_Settings by LineageOS.
the class AnomalyActions method doUnoptimizedBleScan.
private static void doUnoptimizedBleScan(Context ctx, long durationMs) {
ScanSettings scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
// perform ble scanning
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Log.e(TAG, "Device does not support Bluetooth or Bluetooth not enabled");
return;
}
BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
if (bleScanner == null) {
Log.e(TAG, "Cannot access BLE scanner");
return;
}
ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.v(TAG, "called onScanResult");
}
@Override
public void onScanFailed(int errorCode) {
Log.v(TAG, "called onScanFailed");
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
Log.v(TAG, "called onBatchScanResults");
}
};
bleScanner.startScan(null, scanSettings, scanCallback);
try {
Thread.sleep(durationMs);
} catch (InterruptedException e) {
Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
}
bleScanner.stopScan(scanCallback);
}
use of android.bluetooth.le.ScanSettings in project PhoneProfilesPlus by henrichg.
the class BluetoothScanJob method startLEScan.
@SuppressLint("NewApi")
static void startLEScan(Context context) {
if (WifiBluetoothScanner.bluetoothLESupported(context)) {
synchronized (PPApplication.bluetoothLEScanResultsMutex) {
if (bluetooth == null)
bluetooth = getBluetoothAdapter(context);
if (bluetooth != null) {
if (Permissions.checkLocation(context)) {
boolean startScan = false;
if ((android.os.Build.VERSION.SDK_INT >= 21)) {
if (WifiBluetoothScanner.bluetoothLEScanner == null)
WifiBluetoothScanner.bluetoothLEScanner = bluetooth.getBluetoothLeScanner();
// if (WifiBluetoothScanner.bluetoothLEScanCallback21 == null)
// WifiBluetoothScanner.bluetoothLEScanCallback21 = new BluetoothLEScanCallback21(context);
// WifiBluetoothScanner.leScanner.stopScan(WifiBluetoothScanner.leScanCallback21);
ScanSettings.Builder builder = new ScanSettings.Builder();
tmpScanLEResults = null;
int forceScan = WifiBluetoothScanner.getForceOneBluetoothScan(context);
if (forceScan == WifiBluetoothScanner.FORCE_ONE_SCAN_FROM_PREF_DIALOG)
builder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
else
builder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
if (bluetooth.isOffloadedScanBatchingSupported())
builder.setReportDelay(ApplicationPreferences.applicationEventBluetoothLEScanDuration(context) * 1000);
ScanSettings settings = builder.build();
List<ScanFilter> filters = new ArrayList<>();
try {
WifiBluetoothScanner.bluetoothLEScanner.startScan(filters, settings, new BluetoothLEScanCallback21(context));
startScan = true;
} catch (Exception ignored) {
}
} else {
// if (WifiBluetoothScanner.bluetoothLEScanCallback18 == null)
// WifiBluetoothScanner.bluetoothLEScanCallback18 = new BluetoothLEScanCallback18(context);
// bluetooth.stopLeScan(WifiBluetoothScanner.leScanCallback18);
tmpScanLEResults = null;
startScan = bluetooth.startLeScan(new BluetoothLEScanCallback18(context));
if (!startScan) {
if (getBluetoothEnabledForScan(context)) {
bluetooth.disable();
}
}
}
setWaitForLEResults(context, startScan);
}
setLEScanRequest(context, false);
}
}
}
}
use of android.bluetooth.le.ScanSettings in project xDrip by NightscoutFoundation.
the class BluetoothScan method scanLeDeviceLollipop.
@TargetApi(21)
private synchronized void scanLeDeviceLollipop(final boolean enable) {
if (enable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
lollipopScanner = bluetooth_adapter.getBluetoothLeScanner();
}
if (lollipopScanner != null) {
Log.d(TAG, "Starting scanner 21");
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
is_scanning = false;
if (bluetooth_adapter != null && bluetooth_adapter.isEnabled()) {
lollipopScanner.stopScan(mScanCallback);
}
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
is_scanning = true;
if (bluetooth_adapter != null && bluetooth_adapter.isEnabled()) {
lollipopScanner.startScan(null, settings, mScanCallback);
}
} else {
try {
scanLeDevice(true);
} catch (Exception e) {
Log.e(TAG, "Failed to scan for ble device", e);
}
}
} else {
is_scanning = false;
if (bluetooth_adapter != null && bluetooth_adapter.isEnabled()) {
lollipopScanner.stopScan(mScanCallback);
}
}
invalidateOptionsMenu();
}
Aggregations