use of android.bluetooth.le.ScanCallback in project android-beacon-library by AltBeacon.
the class CycledLeScannerForLollipop method postStartLeScan.
private void postStartLeScan(final List<ScanFilter> filters, final ScanSettings settings) {
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 {
scanner.startScan(filters, settings, scanCallback);
} catch (IllegalStateException e) {
LogManager.w(TAG, "Cannot start 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 start scan. Unexpected NPE.");
} catch (SecurityException e) {
// Thrown by Samsung Knox devices if bluetooth access denied for an app
LogManager.e(TAG, "Cannot start scan. Security Exception: " + e.getMessage(), e);
}
}
});
}
use of android.bluetooth.le.ScanCallback in project platform_frameworks_base by android.
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.ScanCallback in project platform_frameworks_base by android.
the class BluetoothAdapter method stopLeScan.
/**
* Stops an ongoing Bluetooth LE device scan.
*
* <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
*
* @param callback used to identify which scan to stop
* must be the same handle used to start the scan
* @deprecated Use {@link BluetoothLeScanner#stopScan(ScanCallback)} instead.
*/
@Deprecated
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public void stopLeScan(LeScanCallback callback) {
if (DBG)
Log.d(TAG, "stopLeScan()");
BluetoothLeScanner scanner = getBluetoothLeScanner();
if (scanner == null) {
return;
}
synchronized (mLeScanClients) {
ScanCallback scanCallback = mLeScanClients.remove(callback);
if (scanCallback == null) {
if (DBG)
Log.d(TAG, "scan not started yet");
return;
}
scanner.stopScan(scanCallback);
}
}
use of android.bluetooth.le.ScanCallback in project android_frameworks_base by DirtyUnicorns.
the class BluetoothAdapter method stopLeScan.
/**
* Stops an ongoing Bluetooth LE device scan.
*
* <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
*
* @param callback used to identify which scan to stop
* must be the same handle used to start the scan
* @deprecated Use {@link BluetoothLeScanner#stopScan(ScanCallback)} instead.
*/
@Deprecated
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public void stopLeScan(LeScanCallback callback) {
if (DBG)
Log.d(TAG, "stopLeScan()");
BluetoothLeScanner scanner = getBluetoothLeScanner();
if (scanner == null) {
return;
}
synchronized (mLeScanClients) {
ScanCallback scanCallback = mLeScanClients.remove(callback);
if (scanCallback == null) {
if (DBG)
Log.d(TAG, "scan not started yet");
return;
}
scanner.stopScan(scanCallback);
}
}
use of android.bluetooth.le.ScanCallback in project android_frameworks_base by ResurrectionRemix.
the class BluetoothAdapter method stopLeScan.
/**
* Stops an ongoing Bluetooth LE device scan.
*
* <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission.
*
* @param callback used to identify which scan to stop
* must be the same handle used to start the scan
* @deprecated Use {@link BluetoothLeScanner#stopScan(ScanCallback)} instead.
*/
@Deprecated
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public void stopLeScan(LeScanCallback callback) {
if (DBG)
Log.d(TAG, "stopLeScan()");
BluetoothLeScanner scanner = getBluetoothLeScanner();
if (scanner == null) {
return;
}
synchronized (mLeScanClients) {
ScanCallback scanCallback = mLeScanClients.remove(callback);
if (scanCallback == null) {
if (DBG)
Log.d(TAG, "scan not started yet");
return;
}
scanner.stopScan(scanCallback);
}
}
Aggregations