BluetoothCentral { public @NotNull BluetoothPeripheral getPeripheral(@NotNull String peripheralAddress) { Objects.requireNonNull(peripheralAddress, "no valid peripheral address provided"); if (!BluetoothAdapter.checkBluetoothAddress(peripheralAddress)) { final String message = String.format("%s is not a valid bluetooth address. Make sure all alphabetic characters are uppercase.", peripheralAddress); throw new IllegalArgumentException(message); } if (connectedPeripherals.containsKey(peripheralAddress)) { return Objects.requireNonNull(connectedPeripherals.get(peripheralAddress)); } else if (unconnectedPeripherals.containsKey(peripheralAddress)) { return Objects.requireNonNull(unconnectedPeripherals.get(peripheralAddress)); } else if (scannedPeripherals.containsKey(peripheralAddress)) { return Objects.requireNonNull(scannedPeripherals.get(peripheralAddress)); } else { BluetoothPeripheral peripheral = new BluetoothPeripheral(context, bluetoothAdapter.getRemoteDevice(peripheralAddress), internalCallback, null, callBackHandler); scannedPeripherals.put(peripheralAddress, peripheral); return peripheral; } } 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 getPeripheralWrongMacAddressTest() { BluetoothPeripheral peripheral = central.getPeripheral("ac:de:ef:12:34:56"); }
@Test public void getPeripheralValidMacAddressTest() { BluetoothDevice device = mock(BluetoothDevice.class); when(device.getAddress()).thenReturn("AC:DE:EF:12:34:56"); bluetoothAdapter.addDevice(device); BluetoothPeripheral peripheral = central.getPeripheral("AC:DE:EF:12:34:56"); assertNotNull(peripheral); }