@Test
public void scanForPeripheralsWithNamesTest() throws Exception {

    application.grantPermissions(Manifest.permission.ACCESS_COARSE_LOCATION);
    String myAddress = "12:23:34:98:76:54";
    String myName = "Polar";
    central.scanForPeripheralsWithNames(new String[]{myName});
    // 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 no filter set
    List<ScanFilter> filters = scanFiltersCaptor.getValue();
    assertNull(filters);
    // Grab the scan callback that is used
    Field field = BluetoothCentral.class.getDeclaredField("scanByNameCallback");
    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(myAddress);
    when(device.getName()).thenReturn("Polar H7");
    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().getName(), "Polar H7");
}