Skip to content

Commit

Permalink
[bluetooth] temporarily connect the device for Service discovery
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Rosenberg <[email protected]>
  • Loading branch information
PRosenb committed Aug 21, 2022
1 parent 09167cb commit e304f38
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,21 @@ public void initialize() {
idleDisconnectDelay = ((Number) idleDisconnectDelayRaw).intValue();
}

if (alwaysConnected) {
// Start the recurrent job if the device is always connected
// or if the Services where not yet discovered.
// If the device is not always connected, the job will be terminated
// after successful connection and the device disconnected after Service
// discovery in `onServicesDiscovered()`.
if (alwaysConnected || !device.isServicesDiscovered()) {
reconnectJob = connectionTaskExecutor.scheduleWithFixedDelay(() -> {
try {
if (device.getConnectionState() != ConnectionState.CONNECTED) {
if (!device.connect()) {
if (device.connect()) {
if (!alwaysConnected) {
cancel(reconnectJob, false);
reconnectJob = null;
}
} else {
logger.debug("Failed to connect to {}", address);
}
// we do not set the Thing status here, because we will anyhow receive a call to
Expand Down Expand Up @@ -326,4 +336,14 @@ public void onDescriptorUpdate(BluetoothDescriptor descriptor, byte[] value) {
descriptor.getUuid(), address);
}
}

@Override
public void onServicesDiscovered() {
super.onServicesDiscovered();

if (!alwaysConnected && device.getConnectionState() == ConnectionState.CONNECTED) {
// disconnect when the device was only connected to discover the Services.
disconnect();
}
}
}

0 comments on commit e304f38

Please sign in to comment.