use of tinyb.BluetoothException 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.BluetoothException 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.BluetoothException 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