use of tinyb.BluetoothGattCharacteristic in project smarthome by eclipse.
the class BlueZBluetoothDevice method writeCharacteristic.
@Override
public boolean writeCharacteristic(BluetoothCharacteristic characteristic) {
if (device == null) {
throw new IllegalStateException("TinyB device is not yet set");
}
BluetoothGattCharacteristic c = getTinybCharacteristicByUUID(characteristic.getUuid().toString());
scheduler.submit(() -> {
try {
BluetoothCompletionStatus successStatus = c.writeValue(characteristic.getByteValue()) ? BluetoothCompletionStatus.SUCCESS : BluetoothCompletionStatus.ERROR;
notifyListeners(BluetoothEventType.CHARACTERISTIC_WRITE_COMPLETE, characteristic, successStatus);
} catch (BluetoothException e) {
logger.debug("Exception occurred when trying to read characteristic '{}': {}", characteristic.getUuid(), e.getMessage());
notifyListeners(BluetoothEventType.CHARACTERISTIC_WRITE_COMPLETE, characteristic, BluetoothCompletionStatus.ERROR);
}
});
return true;
}
use of tinyb.BluetoothGattCharacteristic in project smarthome by eclipse.
the class BlueZBluetoothDevice method refreshServices.
protected void refreshServices() {
if (device.getServices().size() > getServices().size()) {
for (BluetoothGattService tinybService : device.getServices()) {
BluetoothService service = new BluetoothService(UUID.fromString(tinybService.getUUID()), tinybService.getPrimary());
for (BluetoothGattCharacteristic tinybCharacteristic : tinybService.getCharacteristics()) {
BluetoothCharacteristic characteristic = new BluetoothCharacteristic(UUID.fromString(tinybCharacteristic.getUUID()), 0);
for (BluetoothGattDescriptor tinybDescriptor : tinybCharacteristic.getDescriptors()) {
BluetoothDescriptor descriptor = new BluetoothDescriptor(characteristic, UUID.fromString(tinybDescriptor.getUUID()));
characteristic.addDescriptor(descriptor);
}
service.addCharacteristic(characteristic);
}
addService(service);
}
notifyListeners(BluetoothEventType.SERVICES_DISCOVERED);
}
}
use of tinyb.BluetoothGattCharacteristic in project smarthome by eclipse.
the class BlueZBluetoothDevice method enableNotifications.
@Override
public boolean enableNotifications(BluetoothCharacteristic characteristic) {
if (device == null || !device.getConnected()) {
throw new IllegalStateException("TinyB device is not set or not connected");
}
BluetoothGattCharacteristic c = getTinybCharacteristicByUUID(characteristic.getUuid().toString());
if (c != null) {
try {
c.enableValueNotifications(value -> {
logger.debug("Received new value '{}' for characteristic '{}' of device '{}'", value, characteristic.getUuid(), address);
characteristic.setValue(value);
notifyListeners(BluetoothEventType.CHARACTERISTIC_UPDATED, characteristic);
});
} catch (BluetoothException e) {
if (e.getMessage().contains("Already notifying")) {
return false;
} else if (e.getMessage().contains("In Progress")) {
// let's retry in 10 seconds
scheduler.schedule(() -> enableNotifications(characteristic), 10, TimeUnit.SECONDS);
} else {
logger.warn("Exception occurred while activating notifications on '{}'", address, e);
}
}
return true;
} else {
logger.warn("Characteristic '{}' is missing on device '{}'.", characteristic.getUuid(), address);
return false;
}
}
use of tinyb.BluetoothGattCharacteristic in project smarthome by eclipse.
the class BlueZBluetoothDevice method readCharacteristic.
@Override
public boolean readCharacteristic(BluetoothCharacteristic characteristic) {
if (device == null) {
throw new IllegalStateException("TinyB device is not yet set");
}
BluetoothGattCharacteristic c = getTinybCharacteristicByUUID(characteristic.getUuid().toString());
scheduler.submit(() -> {
try {
byte[] value = c.readValue();
characteristic.setValue(value);
notifyListeners(BluetoothEventType.CHARACTERISTIC_READ_COMPLETE, characteristic, BluetoothCompletionStatus.SUCCESS);
} catch (BluetoothException e) {
logger.debug("Exception occurred when trying to read characteristic '{}': {}", characteristic.getUuid(), e.getMessage());
notifyListeners(BluetoothEventType.CHARACTERISTIC_READ_COMPLETE, characteristic, BluetoothCompletionStatus.ERROR);
}
});
return true;
}
Aggregations