@Test
public void scanForPeripheralsWithServicesTest() throws Exception {

    application.grantPermissions(Manifest.permission.ACCESS_COARSE_LOCATION);
    UUID BLP_SERVICE_UUID = UUID.fromString("00001810-0000-1000-8000-00805f9b34fb");
    central.scanForPeripheralsWithServices(new UUID[]{BLP_SERVICE_UUID});
    // Make sure startScan is called
    ArgumentCaptor<List> scanFiltersCaptor = ArgumentCaptor.forClass(List.class);
    ArgumentCaptor<ScanSettings> scanSettingsCaptor = ArgumentCaptor.forClass(ScanSettings.class);
    ArgumentCaptor<ScanCallback> scanCallbackCaptor = ArgumentCaptor.forClass(ScanCallback.class);
    verify(scanner).startScan(scanFiltersCaptor.capture(), scanSettingsCaptor.capture(), scanCallbackCaptor.capture());
    // Verify there is only 1 filter set
    List<ScanFilter> filters = scanFiltersCaptor.getValue();
    assertEquals(1, filters.size());
    // Verify the filter contains the UUID we added
    ScanFilter uuidFilter = filters.get(0);
    ParcelUuid parcelUuid = uuidFilter.getServiceUuid();
    UUID uuid = parcelUuid.getUuid();
    assertEquals(BLP_SERVICE_UUID.toString(), uuid.toString());
    // Grab the scan callback that is used
    Field field = BluetoothCentral.class.getDeclaredField("defaultScanCallback");
    field.setAccessible(true);
    ScanCallback scanCallback = (ScanCallback) field.get(central);
    // Fake scan result
    ScanResult scanResult = mock(ScanResult.class);
    BluetoothDevice device = mock(BluetoothDevice.class);
    when(device.getAddress()).thenReturn("12:23:34:98:76:54");
    when(scanResult.getDevice()).thenReturn(device);
    bluetoothAdapter.addDevice(device);
    scanCallback.onScanResult(CALLBACK_TYPE_ALL_MATCHES, scanResult);
    // See if we get it back
    ArgumentCaptor<BluetoothPeripheral> bluetoothPeripheralCaptor = ArgumentCaptor.forClass(BluetoothPeripheral.class);
    ArgumentCaptor<ScanResult> scanResultCaptor = ArgumentCaptor.forClass(ScanResult.class);
    verify(callback).onDiscoveredPeripheral(bluetoothPeripheralCaptor.capture(), scanResultCaptor.capture());
    assertEquals(scanResultCaptor.getValue(), scanResult);
    assertEquals(bluetoothPeripheralCaptor.getValue().getAddress(), "12:23:34:98:76:54");
}