BluetoothCentral { public void autoConnectPeripheral(@NotNull BluetoothPeripheral peripheral, @NotNull BluetoothPeripheralCallback peripheralCallback) { synchronized (connectLock) { Objects.requireNonNull(peripheral, "no valid peripheral specified"); Objects.requireNonNull(peripheralCallback, "no valid peripheral callback specified"); if (connectedPeripherals.containsKey(peripheral.getAddress())) { Timber.w("already connected to %s'", peripheral.getAddress()); return; } if (unconnectedPeripherals.get(peripheral.getAddress()) != null) { Timber.w("already issued autoconnect for '%s' ", peripheral.getAddress()); return; } int deviceType = peripheral.getType(); if (deviceType == BluetoothDevice.DEVICE_TYPE_UNKNOWN) { Timber.d("peripheral with address '%s' not in Bluetooth cache, autoconnecting by scanning", peripheral.getAddress()); scannedPeripherals.remove(peripheral.getAddress()); unconnectedPeripherals.put(peripheral.getAddress(), peripheral); autoConnectPeripheralByScan(peripheral.getAddress(), peripheralCallback); return; } if (!(deviceType == BluetoothDevice.DEVICE_TYPE_LE || deviceType == BluetoothDevice.DEVICE_TYPE_DUAL)) { Timber.e("peripheral does not support Bluetooth LE"); return; } peripheral.setPeripheralCallback(peripheralCallback); scannedPeripherals.remove(peripheral.getAddress()); unconnectedPeripherals.put(peripheral.getAddress(), peripheral); peripheral.autoConnect(); } } 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 public void autoconnectTestCached() throws Exception { application.grantPermissions(Manifest.permission.ACCESS_COARSE_LOCATION); BluetoothPeripheral peripheral = mock(BluetoothPeripheral.class); when(peripheral.getAddress()).thenReturn("12:23:34:98:76:54"); when(peripheral.getType()).thenReturn(BluetoothDevice.DEVICE_TYPE_LE); central.autoConnectPeripheral(peripheral, peripheralCallback); verify(peripheral).autoConnect(); Field field = BluetoothCentral.class.getDeclaredField("internalCallback"); field.setAccessible(true); BluetoothPeripheral.InternalCallback internalCallback = (BluetoothPeripheral.InternalCallback) field.get(central); internalCallback.connected(peripheral); verify(callback).onConnectedPeripheral(peripheral); }