aboutsummaryrefslogtreecommitdiffstats
path: root/examples/java/ScannerTinyB01.java
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-05-17 09:58:12 +0200
committerSven Gothel <[email protected]>2020-05-17 09:58:12 +0200
commit525cb093d2ead1be30cf860d096129a8ec7146bf (patch)
tree0bf0e063fc9dfbe7b97cf45301dfe1b859b1e5cf /examples/java/ScannerTinyB01.java
parentb12a3e3adf8159a0c252cee67beae35e1b5b879f (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 'examples/java/ScannerTinyB01.java')
-rw-r--r--examples/java/ScannerTinyB01.java100
1 files changed, 63 insertions, 37 deletions
diff --git a/examples/java/ScannerTinyB01.java b/examples/java/ScannerTinyB01.java
index 01973951..5cbb81a6 100644
--- a/examples/java/ScannerTinyB01.java
+++ b/examples/java/ScannerTinyB01.java
@@ -40,6 +40,7 @@ import org.tinyb.BluetoothManager;
import org.tinyb.BluetoothNotification;
import org.tinyb.BluetoothUtils;
import org.tinyb.EIRDataTypeSet;
+import org.tinyb.GATTCharacteristicListener;
public class ScannerTinyB01 {
static {
@@ -122,11 +123,11 @@ public class ScannerTinyB01 {
}
@Override
- public void deviceFound(final BluetoothAdapter adapter, final BluetoothDevice device, final long timestamp) {
+ public void deviceFound(final BluetoothDevice device, final long timestamp) {
final boolean matches = device.getAddress().equals(mac);
System.err.println("****** FOUND__: "+device.toString()+" - match "+matches);
System.err.println("Status Adapter:");
- System.err.println(adapter.toString());
+ System.err.println(device.getAdapter().toString());
if( matches ) {
synchronized(matchingDiscoveredDeviceBucket) {
@@ -137,30 +138,30 @@ public class ScannerTinyB01 {
}
@Override
- public void deviceUpdated(final BluetoothAdapter a, final BluetoothDevice device, final long timestamp, final EIRDataTypeSet updateMask) {
+ public void deviceUpdated(final BluetoothDevice device, final long timestamp, final EIRDataTypeSet updateMask) {
final boolean matches = device.getAddress().equals(mac);
System.err.println("****** UPDATED: "+updateMask+" of "+device+" - match "+matches);
System.err.println("Status Adapter:");
- System.err.println(adapter.toString());
+ System.err.println(device.getAdapter().toString());
}
@Override
- public void deviceConnected(final BluetoothAdapter adapter, final BluetoothDevice device, final long timestamp) {
+ public void deviceConnected(final BluetoothDevice device, final long timestamp) {
final boolean matches = device.getAddress().equals(mac);
System.err.println("****** CONNECTED: "+device.toString()+" - match "+matches);
System.err.println("Status Adapter:");
- System.err.println(adapter.toString());
+ System.err.println(device.getAdapter().toString());
}
@Override
- public void deviceDisconnected(final BluetoothAdapter adapter, final BluetoothDevice device, final long timestamp) {
+ public void deviceDisconnected(final BluetoothDevice device, final long timestamp) {
final boolean matches = device.getAddress().equals(mac);
System.err.println("****** DISCONNECTED: "+device.toString()+" - match "+matches);
System.err.println("Status Adapter:");
- System.err.println(adapter.toString());
+ System.err.println(device.getAdapter().toString());
}
};
- adapter.addStatusListener(statusListener);
+ adapter.addStatusListener(statusListener, null);
adapter.enableDiscoverableNotifications(new BluetoothNotification<Boolean>() {
@Override
public void run(final Boolean value) {
@@ -186,6 +187,22 @@ public class ScannerTinyB01 {
}
});
+ final GATTCharacteristicListener myCharacteristicListener = new GATTCharacteristicListener() {
+ @Override
+ public void notificationReceived(final BluetoothGattCharacteristic charDecl,
+ final byte[] value, final long timestamp) {
+ System.err.println("****** GATT notificationReceived: "+charDecl+
+ ", value "+BluetoothUtils.bytesHexString(value, true, true));
+ }
+
+ @Override
+ public void indicationReceived(final BluetoothGattCharacteristic charDecl,
+ final byte[] value, final long timestamp, final boolean confirmationSent) {
+ System.err.println("****** GATT indicationReceived: "+charDecl+
+ ", value "+BluetoothUtils.bytesHexString(value, true, true));
+ }
+ };
+
int loop = 0;
try {
do {
@@ -279,9 +296,17 @@ public class ScannerTinyB01 {
if ( null == allBluetoothServices || allBluetoothServices.isEmpty() ) {
System.err.println("No BluetoothGattService found!");
} else {
+ final boolean addedCharacteristicListenerRes =
+ BluetoothGattService.addCharacteristicListenerToAll(sensor, allBluetoothServices, myCharacteristicListener);
+ System.err.println("Added GATTCharacteristicListener: "+addedCharacteristicListenerRes);
+ // DBTGattService dbtService
printAllServiceInfo(allBluetoothServices);
- }
+ Thread.sleep(1000); // FIXME: Wait for notifications
+
+ final boolean remRes = BluetoothGattService.removeCharacteristicListenerFromAll(sensor, allBluetoothServices, myCharacteristicListener);
+ System.err.println("Removed GATTCharacteristicListener: "+remRes);
+ }
sensor.disconnect();
// sensor.remove();
System.err.println("ScannerTinyB01 04 ...: "+adapter);
@@ -313,65 +338,66 @@ public class ScannerTinyB01 {
private static void printAllServiceInfo(final List<BluetoothGattService> allBluetoothServices) {
try {
for (final BluetoothGattService service : allBluetoothServices) {
- System.err.println("Service UUID: " + service.getUUID());
+ System.err.println("Service: " + service.getUUID());
final List<BluetoothGattCharacteristic> v = service.getCharacteristics();
for (final BluetoothGattCharacteristic c : v) {
- System.err.println(" Characteristic UUID: " + c.getUUID());
+ System.err.println(" Characteristic: " + c);
final List<BluetoothGattDescriptor> descriptors = c.getDescriptors();
for (final BluetoothGattDescriptor d : descriptors) {
- System.err.println(" Descriptor UUID: " + d.getUUID());
+ System.err.println(" Descriptor: " + d);
}
- if (c.getUUID().contains("2a29-")) {
+ final String uuid;
+ {
+ final String _uuid = c.getUUID().toLowerCase();
+ if( 4 == _uuid.length() ) {
+ uuid = _uuid + "-"; // FIXME: uuid16_t -> uuid128_t string??
+ } else {
+ uuid = _uuid;
+ }
+ }
+ System.err.println("**** Quering: " + uuid);
+
+ if (uuid.contains("2a29-")) {
final byte[] tempRaw = c.readValue();
- System.err.println(" Manufacturer: " + new String(tempRaw));
+ System.err.println("**** Manufacturer: " + new String(tempRaw));
}
- if (c.getUUID().contains("2a28-")) {
+ if (uuid.contains("2a28-")) {
final byte[] tempRaw = c.readValue();
- System.err.println(" Software: " + new String(tempRaw));
+ System.err.println("**** Software: " + new String(tempRaw));
}
- if (c.getUUID().contains("2a27-")) {
+ if (uuid.contains("2a27-")) {
final byte[] tempRaw = c.readValue();
- System.err.println(" Hardware: " + new String(tempRaw));
+ System.err.println("**** Hardware: " + new String(tempRaw));
}
- if (c.getUUID().contains("2a26-")) {
+ if (uuid.contains("2a26-")) {
final byte[] tempRaw = c.readValue();
- System.err.println(" Firmware: " + new String(tempRaw));
+ System.err.println("**** Firmware: " + new String(tempRaw));
}
- if (c.getUUID().contains("2a25-")) {
+ if (uuid.contains("2a25-")) {
final byte[] tempRaw = c.readValue();
- System.err.println(" Serial: " + new String(tempRaw));
+ System.err.println("**** Serial: " + new String(tempRaw));
}
- if (c.getUUID().contains("2a24-")) {
+ if (uuid.contains("2a24-")) {
final byte[] tempRaw = c.readValue();
- System.err.println(" Model: " + new String(tempRaw));
+ System.err.println("**** Model: " + new String(tempRaw));
}
- if (c.getUUID().contains("2a23-")) {
+ if (uuid.contains("2a23-")) {
final byte[] tempRaw = c.readValue();
- System.err.println(" System ID: " + bytesToHex(tempRaw));
+ System.err.println("**** System ID: " + BluetoothUtils.bytesHexString(tempRaw, true, true));
}
}
}
} catch (final RuntimeException e) {
}
}
- private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
- public static String bytesToHex(final byte[] bytes) {
- final char[] hexChars = new char[bytes.length * 2];
- for (int j = 0; j < bytes.length; j++) {
- final int v = bytes[j] & 0xFF;
- hexChars[j * 2] = HEX_ARRAY[v >>> 4];
- hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
- }
- return new String(hexChars);
- }
static class BooleanNotification implements BluetoothNotification<Boolean> {
private final long t0;
private final String name;