diff options
author | Sven Gothel <[email protected]> | 2020-05-17 09:58:12 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-05-17 09:58:12 +0200 |
commit | 525cb093d2ead1be30cf860d096129a8ec7146bf (patch) | |
tree | 0bf0e063fc9dfbe7b97cf45301dfe1b859b1e5cf /java/org/tinyb/BluetoothGattService.java | |
parent | b12a3e3adf8159a0c252cee67beae35e1b5b879f (diff) |
Working GATT Java Side; GATT Types made fully functional for user to avoid 'technical' GATTHandler
GATT Types made fully functional for user to avoid 'technical' GATTHandler (C++)
> GATTService, GATTCharacteristic, GATTDescriptor
-- Reside in their own respective files
-- Added semantic methods (readValue(), ..) implemented using DBTDevice -> GATTHandler
-- GATTDescriptor owns its value
-- GATTHandler setSendIndicationConfirmation(..) defaults to true
-- Allow user to cirvumvent using GATTHandler manually completely,
device 1--*> services 1--*> characteristics 1--*> descriptor
-- C++ GATT types aligned 1:1 to TinyB (Java)
> Merged GATTIndicationListener + GATTNotificationListener -> GATTCharacteristicListener
-- Simplifying usage, unifying notification and indication
-- Now using a list of shared_ptr<GATTCharacteristicListener> allowing multiple actors
instead of just a one shot entry. Similar to AdapterStatusListener,
we utilize this also on the Java side to implement the TinyB notifications.
See dbt_scanner00.cpp: Simplified high-level usage.
See dbt_scanner01.cpp: Lower-level technical usage w/ GATTHandler.
+++
> Simplified more names
> Removed redundancy in listener callbacks,
-- don't pass adapter when device is already given.
device <*--1> adapter
-- don't pass GATT handle explicitly when characteristic is passed
> Comparison of all GATT types are done by their respective unique handle
Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle)
> GATTHandler: Own L2CAPComm instance directly, instead of shared_ptr.
> JNIMem: Added JNICriticalArray class for RAII style release
++++
++++
Working GATT Java Side
> All toString() methods return the C++ toString() via JNI for better representation.
> DBTDevice.java/JNI: Resolved the odd 'adapter' reference issue:
-- Was not passing the jobject of DBTAdapter, but its shared container refeference ;-)
> All GATT types receive their GATT handler for equal test and identity @ ctor
> GATT read/write Value update the cached value as well as issue the notifyValue on change,
including GATTCharacteristic notification/indication listener
Diffstat (limited to 'java/org/tinyb/BluetoothGattService.java')
-rw-r--r-- | java/org/tinyb/BluetoothGattService.java | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/java/org/tinyb/BluetoothGattService.java b/java/org/tinyb/BluetoothGattService.java index 38b91d26..0379568b 100644 --- a/java/org/tinyb/BluetoothGattService.java +++ b/java/org/tinyb/BluetoothGattService.java @@ -28,6 +28,7 @@ package org.tinyb; +import java.util.Iterator; import java.util.List; /** @@ -84,4 +85,57 @@ public interface BluetoothGattService extends BluetoothObject * @return A list of BluetoothGattCharacteristics exposed by this service */ public List<BluetoothGattCharacteristic> getCharacteristics(); + + /** + * Adds the given {@link GATTCharacteristicListener} to the {@link BluetoothDevice} for all {@link BluetoothGattCharacteristic}s. + * @param listener {@link GATTCharacteristicListener} to add to the {@link BluetoothDevice}. + * @return true if successful, otherwise false + */ + public static boolean addCharacteristicListenerToAll(final BluetoothDevice device, final List<BluetoothGattService> services, final GATTCharacteristicListener listener) { + final boolean res = device.addCharacteristicListener(listener, null /* for all */); + for(final Iterator<BluetoothGattService> is = services.iterator(); is.hasNext(); ) { + final BluetoothGattService s = is.next(); + final List<BluetoothGattCharacteristic> characteristics = s.getCharacteristics(); + for(final Iterator<BluetoothGattCharacteristic> ic = characteristics.iterator(); ic.hasNext(); ) { + final BluetoothGattCharacteristic c = ic.next(); + c.enableValueNotifications(null); + } + } + return res; + } + + /** + * Removes the given {@link GATTCharacteristicListener} from the {@link BluetoothDevice}. + * @param listener {@link GATTCharacteristicListener} to remove from the {@link BluetoothDevice}. + * @return true if successful, otherwise false + */ + public static boolean removeCharacteristicListenerFromAll(final BluetoothDevice device, final List<BluetoothGattService> services, final GATTCharacteristicListener listener) { + for(final Iterator<BluetoothGattService> is = services.iterator(); is.hasNext(); ) { + final BluetoothGattService s = is.next(); + final List<BluetoothGattCharacteristic> characteristics = s.getCharacteristics(); + for(final Iterator<BluetoothGattCharacteristic> ic = characteristics.iterator(); ic.hasNext(); ) { + final BluetoothGattCharacteristic c = ic.next(); + c.disableValueNotifications(); + } + } + return device.removeCharacteristicListener(listener); + } + + /** + * Removes all {@link GATTCharacteristicListener} from the {@link BluetoothDevice}. + * @return count of removed {@link GATTCharacteristicListener} + */ + public static int removeAllCharacteristicListener(final BluetoothDevice device, final List<BluetoothGattService> services) { + for(final Iterator<BluetoothGattService> is = services.iterator(); is.hasNext(); ) { + final BluetoothGattService s = is.next(); + final List<BluetoothGattCharacteristic> characteristics = s.getCharacteristics(); + for(final Iterator<BluetoothGattCharacteristic> ic = characteristics.iterator(); ic.hasNext(); ) { + final BluetoothGattCharacteristic c = ic.next(); + c.disableValueNotifications(); + } + } + return device.removeAllCharacteristicListener(); + } + + } |