use of android.bluetooth.le.ScanCallback in project xDrip-plus by jamorham.
the class BluetoothScan method initializeScannerCallback.
@TargetApi(21)
private void initializeScannerCallback() {
Log.d(TAG, "initializeScannerCallback");
mScanCallback = new ScanCallback() {
@Override
public void onBatchScanResults(final List<ScanResult> results) {
runOnUiThread(new Runnable() {
@Override
public void run() {
for (ScanResult result : results) {
BluetoothDevice device = result.getDevice();
if (device.getName() != null && device.getName().length() > 0) {
mLeDeviceListAdapter.addDevice(device);
try {
if (result.getScanRecord() != null)
adverts.put(device.getAddress(), result.getScanRecord().getBytes());
} catch (NullPointerException e) {
//
}
}
}
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
@Override
public void onScanResult(int callbackType, final ScanResult result) {
final BluetoothDevice device = result.getDevice();
runOnUiThread(new Runnable() {
@Override
public void run() {
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0) {
mLeDeviceListAdapter.addDevice(device);
try {
if (result.getScanRecord() != null)
adverts.put(device.getAddress(), result.getScanRecord().getBytes());
} catch (NullPointerException e) {
//
}
mLeDeviceListAdapter.notifyDataSetChanged();
}
}
});
}
};
}
use of android.bluetooth.le.ScanCallback in project android_packages_apps_Settings by DirtyUnicorns.
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.ScanCallback in project platform_packages_apps_Settings by BlissRoms.
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.ScanCallback in project Resurrection_packages_apps_Settings by ResurrectionRemix.
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.ScanCallback in project android-beacon-library by AltBeacon.
the class CycledLeScannerForLollipop method postStopLeScan.
private void postStopLeScan() {
if (!isBluetoothOn()) {
LogManager.d(TAG, "Not stopping scan because bluetooth is off");
return;
}
final BluetoothLeScanner scanner = getScanner();
if (scanner == null) {
return;
}
final ScanCallback scanCallback = getNewLeScanCallback();
mScanHandler.removeCallbacksAndMessages(null);
mScanHandler.post(new Runnable() {
@WorkerThread
@Override
public void run() {
try {
LogManager.d(TAG, "Stopping LE scan on scan handler");
scanner.stopScan(scanCallback);
} catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot stop scan. Bluetooth may be turned off.");
} catch (NullPointerException npe) {
// Necessary because of https://code.google.com/p/android/issues/detail?id=160503
LogManager.e(npe, TAG, "Cannot stop scan. Unexpected NPE.");
} catch (SecurityException e) {
// Thrown by Samsung Knox devices if bluetooth access denied for an app
LogManager.e(TAG, "Cannot stop scan. Security Exception", e);
}
}
});
}
Aggregations