summaryrefslogtreecommitdiffstats
path: root/java/direct_bt/tinyb
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-05-14 14:15:01 +0200
committerSven Gothel <[email protected]>2020-05-14 14:15:01 +0200
commit768169322ff9652cbb9b080a8049fe2db10a8adc (patch)
treed1e8e6cfcb44e35773d4646d9b84f4cf1414dc03 /java/direct_bt/tinyb
parentbececd5a61af0ac86e764a4bba7d3a0234a1d350 (diff)
GATT Service and Characteristic Java <-> JNI binding (incomplete, TODO Descriptor)
Diffstat (limited to 'java/direct_bt/tinyb')
-rw-r--r--java/direct_bt/tinyb/DBTGattCharacteristic.java35
-rw-r--r--java/direct_bt/tinyb/DBTGattService.java15
-rw-r--r--java/direct_bt/tinyb/DBTManager.java41
3 files changed, 74 insertions, 17 deletions
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}
+ * <p>
+ * Actually the BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.1.1 Characteristic Properties
+ * </p>
+ * <p>
+ * Returns string values as defined in <https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/gatt-api.txt>
+ * <pre>
+ * org.bluez.GattCharacteristic1 :: array{string} Flags [read-only]
+ * </pre>
+ * </p>
+ */
+ @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();
@@ -106,13 +124,8 @@ public class DBTGattCharacteristic extends DBTObject implements BluetoothGattCha
public native boolean getNotifying();
@Override
- public native String[] getFlags();
-
- @Override
public native List<BluetoothGattDescriptor> 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<BluetoothGattCharacteristic> 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<BluetoothDevice> getDevices() { return getDefaultAdapter().getDevices(); }
+ /**
+ * {@inheritDoc}
+ * <p>
+ * This call is a quite expensive service query, see below.
+ * </p>
+ * <p>
+ * This implementation only fetches all {@link BluetoothGattService} from all {@link BluetoothDevice}s
+ * from the {@link #getDefaultAdapter()}.
+ * </p>
+ * <p>
+ * This implementation does not {@link BluetoothAdapter#startDiscovery() start} an explicit discovery,
+ * but previous {@link BluetoothAdapter#getDevices() discovered devices} are being queried.
+ * </p>
+ * <p>
+ * 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.
+ * </p>
+ */
@Override
- public List<BluetoothGattService> getServices() { throw new UnsupportedOperationException(); } // FIXME
+ public List<BluetoothGattService> getServices() {
+ final List<BluetoothGattService> res = new ArrayList<BluetoothGattService>();
+ for(final Iterator<BluetoothAdapter> iterA=adapters.iterator(); iterA.hasNext(); ) {
+ final BluetoothAdapter adapter = iterA.next();
+ final List<BluetoothDevice> devices = adapter.getDevices();
+ for(final Iterator<BluetoothDevice> 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<BluetoothGattService> devServices = device.getServices();
+ res.addAll(devServices);
+ }
+ }
+ return res;
+ }
@Override
public boolean setDefaultAdapter(final BluetoothAdapter adapter) {