.usePreferredPhy(phy) .useAutoConnect(shouldAutoConnect()) .setManager(this);
/** * Creates a new connect request. This allows to set a callback to the connect event, * just like any other request. * * @param device the device to connect to. * @return The new connect request. */ @NonNull static ConnectRequest connect(@NonNull final BluetoothDevice device) { return new ConnectRequest(Type.CONNECT, device); }
/** * This method is called when Bluetooth Adapter has been enabled. It is also called * after the service was created if Bluetooth Adapter was enabled at that moment. * This method could initialize all Bluetooth related features, for example open the GATT server. * Make sure you call <code>super.onBluetoothEnabled()</code> at this methods reconnects to * devices that were connected before the Bluetooth was turned off. */ protected void onBluetoothEnabled() { for (final BluetoothDevice device : mManagedDevices) { final BleManager<BleManagerCallbacks> manager = mBleManagers.get(device); if (!manager.isConnected()) manager.connect(device).enqueue(); } }
.useAutoConnect(shouldAutoConnect()) .setManager(this);
@Override public int onStartCommand(final Intent intent, final int flags, final int startId) { if (intent == null || !intent.hasExtra(EXTRA_DEVICE_ADDRESS)) throw new UnsupportedOperationException("No device address at EXTRA_DEVICE_ADDRESS key"); final Uri logUri = intent.getParcelableExtra(EXTRA_LOG_URI); mLogSession = Logger.openSession(getApplicationContext(), logUri); mDeviceName = intent.getStringExtra(EXTRA_DEVICE_NAME); Logger.i(mLogSession, "Service started"); final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); final String deviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS); mBluetoothDevice = adapter.getRemoteDevice(deviceAddress); mBleManager.setLogger(mLogSession); onServiceStarted(); mBleManager.connect(mBluetoothDevice) .useAutoConnect(shouldAutoConnect()) .retry(3, 100) .enqueue(); return START_REDELIVER_INTENT; }
final BluetoothDevice currentDevice = mBluetoothDevice; if (bluetoothEnabled && currentDevice != null && currentDevice.equals(device)) { mConnectRequest.notifySuccess(device); } else { mConnectRequest.notifyFail(device, bluetoothEnabled ? FailCallback.REASON_REQUEST_FAILED : final boolean shouldAutoConnect = connectRequest.shouldAutoConnect(); log(Log.VERBOSE, connectRequest.isFirstAttempt() ? "Connecting..." : "Retrying..."); mCallbacks.onDeviceConnecting(device); mConnectionTime = SystemClock.elapsedRealtime(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { final int preferredPhy = connectRequest.getPreferredPhy(); log(Log.DEBUG, "gatt = device.connectGatt(autoConnect = false, TRANSPORT_LE, " + phyMaskToString(preferredPhy) + ")");
/** * Adds the given device to managed and stars connecting to it. If the device is already connected this method does nothing. * @param device target Bluetooth device * @param session log session that has to be used by the device */ @SuppressWarnings("unchecked") public void connect(final BluetoothDevice device, final ILogSession session) { // If a device is in managed devices it means that it's already connected, or was connected // using autoConnect and the link was lost but Android is already trying to connect to it. if (mManagedDevices.contains(device)) return; mManagedDevices.add(device); LoggableBleManager<BleManagerCallbacks> manager = mBleManagers.get(device); if (manager != null) { if (session != null) manager.setLogger(session); manager.connect(device).enqueue(); } else { mBleManagers.put(device, manager = initializeManager()); manager.setGattCallbacks(BleMulticonnectProfileService.this); manager.setLogger(session); manager.connect(device) .fail((d, status) -> { mManagedDevices.remove(device); mBleManagers.remove(device); }) .enqueue(10000); } }
mConnectRequest.notifyFail(mBluetoothDevice, FailCallback.REASON_BLUETOOTH_DISABLED); mConnectRequest = null;
@Override public void onDeviceSelected(final BluetoothDevice device, final String name) { final int titleId = getLoggerProfileTitle(); if (titleId > 0) { mLogSession = Logger.newSession(getApplicationContext(), getString(titleId), device.getAddress(), name); // If nRF Logger is not installed we may want to use local logger if (mLogSession == null && getLocalAuthorityLogger() != null) { mLogSession = LocalLogSession.newSession(getApplicationContext(), getLocalAuthorityLogger(), device.getAddress(), name); } } mDeviceName = name; mBleManager.setLogger(mLogSession); mBleManager.connect(device) .useAutoConnect(shouldAutoConnect()) .retry(3, 100) .enqueue(); }
@Override public void onDeviceSelected(final BluetoothDevice device, final String name) { final int titleId = getLoggerProfileTitle(); if (titleId > 0) { mLogSession = Logger.newSession(getApplicationContext(), getString(titleId), device.getAddress(), name); // If nRF Logger is not installed we may want to use local logger if (mLogSession == null && getLocalAuthorityLogger() != null) { mLogSession = LocalLogSession.newSession(getApplicationContext(), getLocalAuthorityLogger(), device.getAddress(), name); } } mDeviceName = name; mBleManager.setLogger(mLogSession); mBleManager.connect(device) .useAutoConnect(shouldAutoConnect()) .retry(3, 100) .enqueue(); }