[bluetooth] Add support for service data (#10278)

Signed-off-by: Peter Rosenberg <prosenb.dev@gmail.com>
This commit is contained in:
Pete
2022-08-26 05:36:21 +10:00
committed by GitHub
parent d4c472a04c
commit 34bdc21370
8 changed files with 195 additions and 3 deletions

View File

@@ -224,6 +224,11 @@ public class BeaconBluetoothHandler extends BaseThingHandler implements Bluetoot
int rssi = scanNotification.getRssi();
if (rssi != Integer.MIN_VALUE) {
updateRSSI(rssi);
} else {
// we received a scan notification from this device so it is online
// TODO how can we detect if the underlying bluez stack is still receiving advertising packets when there
// are no changes?
updateStatus(ThingStatus.ONLINE);
}
}

View File

@@ -85,11 +85,21 @@ public class ConnectedBluetoothHandler extends BeaconBluetoothHandler {
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
@@ -326,4 +336,14 @@ public class ConnectedBluetoothHandler extends BeaconBluetoothHandler {
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();
}
}
}

View File

@@ -12,10 +12,13 @@
*/
package org.openhab.binding.bluetooth.notification;
import java.util.Map;
/**
* The {@link BluetoothScanNotification} provides a notification of a received scan packet
*
* @author Chris Jackson - Initial contribution
* @author Peter Rosenberg - Add support for ServiceData
*/
public class BluetoothScanNotification extends BluetoothNotification {
/**
@@ -33,6 +36,13 @@ public class BluetoothScanNotification extends BluetoothNotification {
*/
private byte[] manufacturerData = null;
/**
* The service data.
* Key: UUID of the service
* Value: Data of the characteristic
*/
private Map<String, byte[]> serviceData = null;
/**
* The beacon type
*/
@@ -106,6 +116,14 @@ public class BluetoothScanNotification extends BluetoothNotification {
return manufacturerData;
}
public void setServiceData(Map<String, byte[]> serviceData) {
this.serviceData = serviceData;
}
public Map<String, byte[]> getServiceData() {
return serviceData;
}
/**
* Sets the beacon type for this packet
*