BluetoothCentral { public void scanForPeripheralsWithNames(@NotNull final String[] peripheralNames) { Objects.requireNonNull(peripheralNames, "No peripheral names supplied"); if (peripheralNames.length == 0) { throw new IllegalArgumentException("at least one peripheral name must be supplied"); } scanPeripheralNames = peripheralNames; startScan(null, scanSettings, scanByNameCallback); } BluetoothCentral(@NotNull Context context, @NotNull BluetoothCentralCallback bluetoothCentralCallback, @NotNull Handler handler); void close(); boolean setScanMode(int scanMode); void scanForPeripheralsWithServices(@NotNull final UUID[] serviceUUIDs); void scanForPeripheralsWithNames(@NotNull final String[] peripheralNames); void scanForPeripheralsWithAddresses(@NotNull final String[] peripheralAddresses); void scanForPeripheralsUsingFilters(@NotNull List<ScanFilter> filters); void scanForPeripherals(); void stopScan(); boolean isScanning(); void connectPeripheral(@NotNull BluetoothPeripheral peripheral, @NotNull BluetoothPeripheralCallback peripheralCallback); void autoConnectPeripheral(@NotNull BluetoothPeripheral peripheral, @NotNull BluetoothPeripheralCallback peripheralCallback); void cancelConnection(@NotNull final BluetoothPeripheral peripheral); void autoConnectPeripheralsBatch(@NotNull Map<BluetoothPeripheral, BluetoothPeripheralCallback> batch); @NotNull BluetoothPeripheral getPeripheral(@NotNull String peripheralAddress); @NotNull List<BluetoothPeripheral> getConnectedPeripherals(); boolean isBluetoothEnabled(); boolean setPinCodeForPeripheral(@NotNull String peripheralAddress, @NotNull String pin); boolean removeBond(@NotNull String peripheralAddress); void startPairingPopupHack(); static final int SCAN_FAILED_ALREADY_STARTED; static final int SCAN_FAILED_APPLICATION_REGISTRATION_FAILED; static final int SCAN_FAILED_INTERNAL_ERROR; static final int SCAN_FAILED_FEATURE_UNSUPPORTED; static final int SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES; static final int SCAN_FAILED_SCANNING_TOO_FREQUENTLY; }
@Test (expected = IllegalArgumentException.class) public void scanForPeripheralsWithNamesEmptyTest() { application.grantPermissions(Manifest.permission.ACCESS_COARSE_LOCATION); central.scanForPeripheralsWithNames(new String[0]); }
@Test public void scanForNamesFailedTest() throws Exception { application.grantPermissions(Manifest.permission.ACCESS_COARSE_LOCATION); String myName = "Polar"; central.scanForPeripheralsWithNames(new String[]{myName}); Field field = BluetoothCentral.class.getDeclaredField("scanByNameCallback"); field.setAccessible(true); ScanCallback scanCallback = (ScanCallback) field.get(central); scanCallback.onScanFailed(SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES); verify(callback).onScanFailed(SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES); }
@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}); 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()); List<ScanFilter> filters = scanFiltersCaptor.getValue(); assertNull(filters); Field field = BluetoothCentral.class.getDeclaredField("scanByNameCallback"); field.setAccessible(true); ScanCallback scanCallback = (ScanCallback) field.get(central); 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); 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"); }
@Test (expected = NullPointerException.class) public void scanForPeripheralsWithNamesTestNullTest() { application.grantPermissions(Manifest.permission.ACCESS_COARSE_LOCATION); central.scanForPeripheralsWithNames(null); }