All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.hypfvieh.bluetooth.wrapper.BluetoothGattService Maven / Gradle / Ivy

The newest version!
package com.github.hypfvieh.bluetooth.wrapper;

import com.github.hypfvieh.DbusHelper;
import org.bluez.GattCharacteristic1;
import org.bluez.GattService1;
import org.bluez.exceptions.BluezNotImplementedException;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.interfaces.DBusInterface;

import java.util.*;
import java.util.Map.Entry;

/**
 * Wrapper class which represents a GATT service provided by a remote device.
 * @author hypfvieh
 *
 */
public class BluetoothGattService extends AbstractBluetoothObject {

    private final GattService1 service;
    private final BluetoothDevice device;

    private final Map characteristicByUuid = new LinkedHashMap<>();

    public BluetoothGattService(GattService1 _service, BluetoothDevice _device, String _dbusPath, DBusConnection _dbusConnection) {
        super(BluetoothDeviceType.GATT_SERVICE, _dbusConnection, _dbusPath);
        service = _service;
        device = _device;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Class getInterfaceClass() {
        return GattService1.class;
    }

    /**
     * Re-queries the GattCharacteristics from the device.
     */
    public void refreshGattCharacteristics() {
        characteristicByUuid.clear();

        Set findNodes = DbusHelper.findNodes(getDbusConnection(), getDbusPath());
        Map remoteObjects = getRemoteObjects(findNodes, getDbusPath(), GattCharacteristic1.class);
        for (Entry entry : remoteObjects.entrySet()) {
            BluetoothGattCharacteristic bluetoothGattCharacteristics = new BluetoothGattCharacteristic(entry.getValue(), this, entry.getKey(), getDbusConnection());
            characteristicByUuid.put(bluetoothGattCharacteristics.getUuid(), bluetoothGattCharacteristics);
        }
    }

    /**
     * Get the currently available GATT characteristics.
* Will issue a query if {@link #refreshGattCharacteristics()} wasn't called before. * @return List, maybe empty but never null */ public List getGattCharacteristics() { if (characteristicByUuid.isEmpty()) { refreshGattCharacteristics(); } return new ArrayList<>(characteristicByUuid.values()); } /** * Return the {@link BluetoothGattCharacteristic} object for the given UUID. * @param _uuid uuid * @return maybe null if not found */ public BluetoothGattCharacteristic getGattCharacteristicByUuid(String _uuid) { if (characteristicByUuid.isEmpty()) { refreshGattCharacteristics(); } return characteristicByUuid.get(_uuid); } /** * From bluez Documentation: *

* 128-bit service UUID. *

* @return uuid, maybe null */ public String getUuid() { return getTyped("UUID", String.class); } /** * From bluez Documentation: *

* Indicates whether or not this GATT service is a
* primary service. If false, the service is secondary. *

* @return maybe null if feature is not supported */ public Boolean isPrimary() { return getTyped("Primary", Boolean.class); } /** * From bluez Documentation: *

* Not implemented
* Array of object paths representing the included
* services of this service. *

* @return object array, maybe null * @throws BluezNotImplementedException always - because currently not supported */ public Object[] getIncludes() throws BluezNotImplementedException { throw new BluezNotImplementedException("Feature not yet implemented"); } /** * Get the raw {@link GattService1} object. * @return {@link GattService1}, maybe null */ public GattService1 getService() { return service; } /** * Get the {@link BluetoothDevice} instance which is providing this {@link BluetoothGattService}. * @return {@link BluetoothDevice}, maybe null */ public BluetoothDevice getDevice() { return device; } @Override public String toString() { return getClass().getSimpleName() + " [service=" + service + ", device=" + device.getDbusPath() + ", getBluetoothType()=" + getBluetoothType().name() + ", getDbusPath()=" + getDbusPath() + "]"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy