Search in sources :

Example 1 with BluetoothGattCharacteristic

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;
}
Also used : BluetoothException(tinyb.BluetoothException) BluetoothCompletionStatus(org.eclipse.smarthome.binding.bluetooth.BluetoothCompletionStatus) BluetoothGattCharacteristic(tinyb.BluetoothGattCharacteristic)

Example 2 with BluetoothGattCharacteristic

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);
    }
}
Also used : BluetoothDescriptor(org.eclipse.smarthome.binding.bluetooth.BluetoothDescriptor) BluetoothService(org.eclipse.smarthome.binding.bluetooth.BluetoothService) BluetoothCharacteristic(org.eclipse.smarthome.binding.bluetooth.BluetoothCharacteristic) BluetoothGattService(tinyb.BluetoothGattService) BluetoothGattDescriptor(tinyb.BluetoothGattDescriptor) BluetoothGattCharacteristic(tinyb.BluetoothGattCharacteristic)

Example 3 with BluetoothGattCharacteristic

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;
    }
}
Also used : BluetoothException(tinyb.BluetoothException) BluetoothGattCharacteristic(tinyb.BluetoothGattCharacteristic)

Example 4 with BluetoothGattCharacteristic

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;
}
Also used : BluetoothException(tinyb.BluetoothException) BluetoothGattCharacteristic(tinyb.BluetoothGattCharacteristic)

Aggregations

BluetoothGattCharacteristic (tinyb.BluetoothGattCharacteristic)4 BluetoothException (tinyb.BluetoothException)3 BluetoothCharacteristic (org.eclipse.smarthome.binding.bluetooth.BluetoothCharacteristic)1 BluetoothCompletionStatus (org.eclipse.smarthome.binding.bluetooth.BluetoothCompletionStatus)1 BluetoothDescriptor (org.eclipse.smarthome.binding.bluetooth.BluetoothDescriptor)1 BluetoothService (org.eclipse.smarthome.binding.bluetooth.BluetoothService)1 BluetoothGattDescriptor (tinyb.BluetoothGattDescriptor)1 BluetoothGattService (tinyb.BluetoothGattService)1