Last active
November 22, 2021 23:47
-
-
Save geoaxis/a2ec0e479e587eabea918ed73bfd9800 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Slf4j | |
@AllArgsConstructor | |
public class PeripheralCallback extends BluetoothPeripheralCallback { | |
private final StringProperty irTemperatureString; | |
private final StringProperty ambientTemperatureString; | |
@Override | |
public void onServicesDiscovered(@NotNull BluetoothPeripheral peripheral, | |
@NotNull List<BluetoothGattService> services) { | |
if (peripheral.getService(UUID_TEMPERATURE_SERVICE) != null) { | |
peripheral.writeCharacteristic(UUID_TEMPERATURE_SERVICE, | |
UUID_TEMPERATURE_CONFIG, | |
ENABLE_COMMAND, | |
WriteType.WITH_RESPONSE); | |
BluetoothGattCharacteristic temperatureDataChar = peripheral.getCharacteristic( | |
UUID_TEMPERATURE_SERVICE, | |
UUID_TEMPERATURE_DATA); | |
peripheral.setNotify(temperatureDataChar, true); | |
} | |
} | |
@Override | |
public void onCharacteristicWrite(@NotNull BluetoothPeripheral peripheral, | |
byte[] value, | |
@NotNull BluetoothGattCharacteristic characteristicUUID, | |
@NotNull BluetoothCommandStatus status) { | |
// Deal with errors | |
if (status != COMMAND_SUCCESS) { | |
log.error("command failed with status {}", status); | |
return; | |
} | |
if (characteristicUUID.getUuid().equals(UUID_TEMPERATURE_CONFIG)) { | |
log.info("temperature notifications configured"); | |
BluetoothGattCharacteristic temperatureDataChar = peripheral.getCharacteristic( | |
UUID_TEMPERATURE_SERVICE, | |
UUID_TEMPERATURE_DATA); | |
peripheral.setNotify(temperatureDataChar, true); | |
} | |
} | |
@Override | |
public void onCharacteristicUpdate(@NotNull BluetoothPeripheral peripheral, | |
byte[] value, | |
@NotNull BluetoothGattCharacteristic characteristic, | |
@NotNull BluetoothCommandStatus status) { | |
final UUID characteristicUUID = characteristic.getUuid(); | |
// Deal with errors | |
if (status != COMMAND_SUCCESS) { | |
log.error("command failed with status {}", status); | |
return; | |
} | |
if (characteristicUUID.equals((UUID_TEMPERATURE_DATA))) { | |
log.info("receiving temperature data update"); | |
var result = calculateTemperature(value); | |
if (result.length >= 2) { | |
Platform.runLater(() -> { | |
irTemperatureString.setValue(String.format("%.2f", result[0])); | |
ambientTemperatureString.setValue(String.format("%.2f", result[1])); | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment