From 768169322ff9652cbb9b080a8049fe2db10a8adc Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 14 May 2020 14:15:01 +0200 Subject: GATT Service and Characteristic Java <-> JNI binding (incomplete, TODO Descriptor) --- java/direct_bt/tinyb/DBTGattCharacteristic.java | 35 ++++++++++++++------- java/direct_bt/tinyb/DBTGattService.java | 15 ++++++--- java/direct_bt/tinyb/DBTManager.java | 41 ++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 17 deletions(-) (limited to 'java/direct_bt/tinyb') diff --git a/java/direct_bt/tinyb/DBTGattCharacteristic.java b/java/direct_bt/tinyb/DBTGattCharacteristic.java index 6c79e188..9776e400 100644 --- a/java/direct_bt/tinyb/DBTGattCharacteristic.java +++ b/java/direct_bt/tinyb/DBTGattCharacteristic.java @@ -27,7 +27,6 @@ package direct_bt.tinyb; import java.util.List; -import org.tinyb.BluetoothDevice; import org.tinyb.BluetoothException; import org.tinyb.BluetoothGattCharacteristic; import org.tinyb.BluetoothGattDescriptor; @@ -38,11 +37,15 @@ import org.tinyb.BluetoothType; public class DBTGattCharacteristic extends DBTObject implements BluetoothGattCharacteristic { + private final BluetoothGattService service; + private final String[] properties; private final String uuid; - /* pp */ DBTGattCharacteristic(final long nativeInstance, final String uuid) + /* pp */ DBTGattCharacteristic(final long nativeInstance, final BluetoothGattService service, final String[] properties, final String uuid) { super(nativeInstance, uuid.hashCode()); + this.service = service; + this.properties = properties; this.uuid = uuid; } @@ -80,6 +83,24 @@ public class DBTGattCharacteristic extends DBTObject implements BluetoothGattCha return find(UUID, 0); } + @Override + public final BluetoothGattService getService() { return service; } + + /** + * {@inheritDoc} + *

+ * Actually the BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.1.1 Characteristic Properties + *

+ *

+ * Returns string values as defined in + *

+     * org.bluez.GattCharacteristic1 :: array{string} Flags [read-only]
+     * 
+ *

+ */ + @Override + public final String[] getFlags() { return properties; } + /* D-Bus method calls: */ @Override @@ -94,10 +115,7 @@ public class DBTGattCharacteristic extends DBTObject implements BluetoothGattCha @Override public native boolean writeValue(byte[] argValue) throws BluetoothException; - /* D-Bus property accessors: */ - - @Override - public native BluetoothGattService getService(); + /* Native accessors: */ @Override public native byte[] getValue(); @@ -105,14 +123,9 @@ public class DBTGattCharacteristic extends DBTObject implements BluetoothGattCha @Override public native boolean getNotifying(); - @Override - public native String[] getFlags(); - @Override public native List getDescriptors(); - private native void init(DBTGattCharacteristic obj); - @Override protected native void deleteImpl(); diff --git a/java/direct_bt/tinyb/DBTGattService.java b/java/direct_bt/tinyb/DBTGattService.java index 17fd1a7c..c7fffba2 100644 --- a/java/direct_bt/tinyb/DBTGattService.java +++ b/java/direct_bt/tinyb/DBTGattService.java @@ -27,6 +27,7 @@ package direct_bt.tinyb; import java.util.List; +import org.tinyb.BluetoothDevice; import org.tinyb.BluetoothGattCharacteristic; import org.tinyb.BluetoothGattService; import org.tinyb.BluetoothManager; @@ -34,11 +35,15 @@ import org.tinyb.BluetoothType; public class DBTGattService extends DBTObject implements BluetoothGattService { + private final BluetoothDevice device; + private final boolean isPrimary; private final String uuid; - /* pp */ DBTGattService(final long nativeInstance, final String uuid) + /* pp */ DBTGattService(final long nativeInstance, final BluetoothDevice device, final boolean isPrimary, final String uuid) { super(nativeInstance, uuid.hashCode()); + this.device = device; + this.isPrimary = isPrimary; this.uuid = uuid; } @@ -76,13 +81,13 @@ public class DBTGattService extends DBTObject implements BluetoothGattService return find(UUID, 0); } - /* D-Bus property accessors: */ - @Override - public native DBTDevice getDevice(); + public final BluetoothDevice getDevice() { return device; } @Override - public native boolean getPrimary(); + public final boolean getPrimary() { return isPrimary; } + + /* Native accessors: */ @Override public native List getCharacteristics(); diff --git a/java/direct_bt/tinyb/DBTManager.java b/java/direct_bt/tinyb/DBTManager.java index e18f56e4..ec02bbc3 100644 --- a/java/direct_bt/tinyb/DBTManager.java +++ b/java/direct_bt/tinyb/DBTManager.java @@ -176,8 +176,47 @@ public class DBTManager implements BluetoothManager @Override public List getDevices() { return getDefaultAdapter().getDevices(); } + /** + * {@inheritDoc} + *

+ * This call is a quite expensive service query, see below. + *

+ *

+ * This implementation only fetches all {@link BluetoothGattService} from all {@link BluetoothDevice}s + * from the {@link #getDefaultAdapter()}. + *

+ *

+ * This implementation does not {@link BluetoothAdapter#startDiscovery() start} an explicit discovery, + * but previous {@link BluetoothAdapter#getDevices() discovered devices} are being queried. + *

+ *

+ * In case a {@link BluetoothDevice} hasn't detected any service yet, + * i.e. {@link BluetoothDevice#getServicesResolved()} and {@link BluetoothDevice#getConnected()} is false, + * a new {@link BluetoothDevice#connect() connect/disconnect} cycle is performed. + *

+ */ @Override - public List getServices() { throw new UnsupportedOperationException(); } // FIXME + public List getServices() { + final List res = new ArrayList(); + for(final Iterator iterA=adapters.iterator(); iterA.hasNext(); ) { + final BluetoothAdapter adapter = iterA.next(); + final List devices = adapter.getDevices(); + for(final Iterator iterD=devices.iterator(); iterD.hasNext(); ) { + final BluetoothDevice device = iterD.next(); + if( !device.getServicesResolved() && !device.getConnected() ) { + if( device.connect() ) { + if( !device.getServicesResolved() ) { + throw new InternalError("Device connected but services not resolved"); + } + device.disconnect(); + } + } + final List devServices = device.getServices(); + res.addAll(devServices); + } + } + return res; + } @Override public boolean setDefaultAdapter(final BluetoothAdapter adapter) { -- cgit v1.2.3