From 6fdcdb13db5e13f11c44f0f9bba088252d9ffa18 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 20 Apr 2020 07:21:35 +0200 Subject: Initial working Java binding for the direct_bt C++ module Example ScannerTinyB01 demonstrates efficient scanning devices using the new BluetoothDeviceDiscoveryListener interface. The C++ HCIObject extends JavaUplink, which handles the java object references via 'std::shared_ptr', where JavaAnonObj relies on later polymorph specialization. JavaAnonObj gets derived in the java/jni of direct_bt, where the JNI header + libraries are available. +++ The java inplementing NativeDownlink implementations store the nativeInstance to the C++ instances as well as handle their java references within the native instances. The C++ JavaUplink and Java NativeDownlink interfaces are complete the cross referencing java <-> native. +++ Native libraries are now split into pairs: - tinyb + javatinyb - direct_bt + javadirect_bt TODO: BluetoothFactory must chose the proper bundle! +++ The Java Adapter received a BluetoothDeviceDiscoveryListener hook, matching C++ Adapter's HCIDeviceDiscoveryListener. Since the Java Adapter implements its own discovery thread, using the BluetoothDeviceDiscoveryListener is more efficient then polling over a list of Devices fetched. ++++ TODO: Align Java and C++ class names, foremost in the C++ direct_bt space. TODO: Bind the whole C++ GATT functionality More testing. --- java/org/tinyb/BluetoothAdapter.java | 6 ++++ .../tinyb/BluetoothDeviceDiscoveryListener.java | 42 ++++++++++++++++++++++ java/org/tinyb/BluetoothFactory.java | 10 +++--- java/org/tinyb/BluetoothManager.java | 9 +++++ java/org/tinyb/BluetoothUtils.java | 34 ++++++++++++++++++ 5 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 java/org/tinyb/BluetoothDeviceDiscoveryListener.java create mode 100644 java/org/tinyb/BluetoothUtils.java (limited to 'java/org') diff --git a/java/org/tinyb/BluetoothAdapter.java b/java/org/tinyb/BluetoothAdapter.java index 95d2e6d0..2b55cf75 100644 --- a/java/org/tinyb/BluetoothAdapter.java +++ b/java/org/tinyb/BluetoothAdapter.java @@ -241,6 +241,12 @@ public interface BluetoothAdapter extends BluetoothObject */ public boolean getDiscovering(); + /** + * Sets the {@link BluetoothDeviceDiscoveryListener} for the respective device discovery events. + * @param listener A {@link BluetoothDeviceDiscoveryListener} instance, or {@code null} to disable notifications. + */ + public void setDeviceDiscoveryListener(final BluetoothDeviceDiscoveryListener listener); + /** * Enables notifications for the discovering property and calls run function of the * BluetoothNotification object. diff --git a/java/org/tinyb/BluetoothDeviceDiscoveryListener.java b/java/org/tinyb/BluetoothDeviceDiscoveryListener.java new file mode 100644 index 00000000..737f47b9 --- /dev/null +++ b/java/org/tinyb/BluetoothDeviceDiscoveryListener.java @@ -0,0 +1,42 @@ +/** + * Author: Sven Gothel + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package org.tinyb; + +/** + * {@link BluetoothDevice} listener for the respective {@link BluetoothDevice} discovery events: Added, updated and removed. + *

+ * A listener instance may be attached to a {@link BluetoothAdapter} via + * {@link BluetoothAdapter#setDeviceDiscoveryListener(BluetoothDeviceDiscoveryListener)}. + *

+ */ +public interface BluetoothDeviceDiscoveryListener { + /** A {@link BluetoothDevice} has been newly discovered. */ + public void deviceAdded(final BluetoothAdapter adapter, final BluetoothDevice device); + /** An already discovered {@link BluetoothDevice} has been updated. */ + public void deviceUpdated(final BluetoothAdapter adapter, final BluetoothDevice device); + /** An already discovered {@link BluetoothDevice} has been removed or lost. */ + public void deviceRemoved(final BluetoothAdapter adapter, final BluetoothDevice device); +}; diff --git a/java/org/tinyb/BluetoothFactory.java b/java/org/tinyb/BluetoothFactory.java index 8f01db15..38306a16 100644 --- a/java/org/tinyb/BluetoothFactory.java +++ b/java/org/tinyb/BluetoothFactory.java @@ -39,12 +39,12 @@ public class BluetoothFactory { /** * Name of the native implementation native library basename: {@value} */ - public static final String ImplNativeLibBasename = "tinyb"; + public static final String ImplNativeLibBasename = "direct_bt"; // "tinyb"; /** * Name of the Jave native library basename: {@value} */ - public static final String JavaNativeLibBasename = "javatinyb"; + public static final String JavaNativeLibBasename = "javadirect_bt"; // "javatinyb"; /** * Manifest's {@link Attributes.Name#SPECIFICATION_VERSION} or {@code null} if not available. @@ -138,12 +138,12 @@ public class BluetoothFactory { public static final String DBusFactoryImplClassName = "tinyb.dbus.DBusManager"; /** - * Fully qualified factory class name for native HCI implementation: {@value} + * Fully qualified factory class name for direct_bt implementation: {@value} *

* This value is exposed for convenience, user implementations are welcome. *

*/ - public static final String HCIFactoryImplClassName = "tinyb.hci.HCIManager"; + public static final String DirectBTFactoryImplClassName = "direct_bt.tinyb.Manager"; /** * Returns an initialized BluetoothManager instance using the given {@code factoryImplClass}. @@ -187,7 +187,7 @@ public class BluetoothFactory { * @throws InvocationTargetException * @throws ClassNotFoundException * @see {@link #DBusFactoryImplClassName} - * @see {@link #HCIFactoryImplClassName} + * @see {@link #DirectBTFactoryImplClassName} */ public static synchronized BluetoothManager getBluetoothManager(final String factoryImplClassName) throws BluetoothException, NoSuchMethodException, SecurityException, diff --git a/java/org/tinyb/BluetoothManager.java b/java/org/tinyb/BluetoothManager.java index ec1de2d7..b7bec3cb 100644 --- a/java/org/tinyb/BluetoothManager.java +++ b/java/org/tinyb/BluetoothManager.java @@ -181,4 +181,13 @@ public interface BluetoothManager * @return TRUE if discovery is running */ public boolean getDiscovering() throws BluetoothException; + + /** + * Release the native memory associated with this object and all related Bluetooth resources. + * The object should not be used following a call to close + *

+ * Shutdown method is intended to allow a clean Bluetooth state at program exist. + *

+ */ + public void shutdown(); } diff --git a/java/org/tinyb/BluetoothUtils.java b/java/org/tinyb/BluetoothUtils.java new file mode 100644 index 00000000..e7a0276b --- /dev/null +++ b/java/org/tinyb/BluetoothUtils.java @@ -0,0 +1,34 @@ +/** + * Author: Sven Gothel + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package org.tinyb; + +public class BluetoothUtils { + + /** + * Returns current monotonic time in milliseconds. + */ + public static native long getCurrentMilliseconds(); + +} -- cgit v1.2.3