aboutsummaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-02-01 11:14:14 +0100
committerSven Gothel <[email protected]>2020-02-01 11:14:14 +0100
commite32417cdef59de3bdae261e4469186c357599165 (patch)
treeca7f8cc7f1c611288ea7eb9d65e249d103897be0 /java
parent81ff36ed014a34be9194036e26dc9a2e272e5004 (diff)
BluetoothManager: Expose native get_default_adapter() to java as getDefaultAdapter()
Notable: manager.stopDiscovery() doesn't lead to !manager.getDiscovering() (radio silence). Only manager.getDefaultAdapter().stopDiscovery() worked.
Diffstat (limited to 'java')
-rw-r--r--java/BluetoothManager.java8
-rw-r--r--java/jni/BluetoothManager.cxx30
2 files changed, 38 insertions, 0 deletions
diff --git a/java/BluetoothManager.java b/java/BluetoothManager.java
index 0c415d6..489ac3e 100644
--- a/java/BluetoothManager.java
+++ b/java/BluetoothManager.java
@@ -195,6 +195,14 @@ public class BluetoothManager
*/
public native boolean setDefaultAdapter(BluetoothAdapter adapter);
+ /** Gets the default adapter to use for discovery.
+ * <p>
+ * System default is the last detected adapter at initialisation.
+ * </p>
+ * @return the used default adapter
+ */
+ public native BluetoothAdapter getDefaultAdapter();
+
/** Turns on device discovery on the default adapter if it is disabled.
* @return TRUE if discovery was successfully enabled
*/
diff --git a/java/jni/BluetoothManager.cxx b/java/jni/BluetoothManager.cxx
index dac0432..dc6b038 100644
--- a/java/jni/BluetoothManager.cxx
+++ b/java/jni/BluetoothManager.cxx
@@ -332,6 +332,36 @@ jboolean Java_tinyb_BluetoothManager_setDefaultAdapter(JNIEnv *env, jobject obj,
return JNI_FALSE;
}
+jobject Java_tinyb_BluetoothManager_getDefaultAdapter(JNIEnv *env, jobject obj)
+{
+ try {
+ BluetoothManager *manager = getInstance<BluetoothManager>(env, obj);
+
+ std::unique_ptr<tinyb::BluetoothAdapter> b_adapter = manager->get_default_adapter();
+ BluetoothAdapter *b_adapter_naked = b_adapter.release();
+ if (!b_adapter_naked)
+ {
+ return nullptr;
+ }
+ jclass clazz = search_class(env, *b_adapter_naked);
+ jmethodID clazz_ctor = search_method(env, clazz, "<init>", "(J)V", false);
+
+ jobject result = env->NewObject(clazz, clazz_ctor, (jlong)b_adapter_naked);
+ return result;
+ } catch (std::bad_alloc &e) {
+ raise_java_oom_exception(env, e);
+ } catch (BluetoothException &e) {
+ raise_java_bluetooth_exception(env, e);
+ } catch (std::runtime_error &e) {
+ raise_java_runtime_exception(env, e);
+ } catch (std::invalid_argument &e) {
+ raise_java_invalid_arg_exception(env, e);
+ } catch (std::exception &e) {
+ raise_java_exception(env, e);
+ }
+ return NULL;
+}
+
jboolean Java_tinyb_BluetoothManager_startDiscovery(JNIEnv *env, jobject obj)
{
try {