From e32417cdef59de3bdae261e4469186c357599165 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 1 Feb 2020 11:14:14 +0100 Subject: 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. --- java/BluetoothManager.java | 8 ++++++++ java/jni/BluetoothManager.cxx | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'java') 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. + *

+ * System default is the last detected adapter at initialisation. + *

+ * @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(env, obj); + + std::unique_ptr 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, "", "(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 { -- cgit v1.2.3