diff options
author | Sven Gothel <[email protected]> | 2020-02-01 11:14:14 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-02-01 11:14:14 +0100 |
commit | c47b88618d287a5e261b476e42b285a2a48b661a (patch) | |
tree | 2cdbc837efb059dbd34e339be5bbcb69c0a71cd4 | |
parent | 9a99e0414a7090e0b2dd0c69fe326a911e182c9c (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.
-rw-r--r-- | java/BluetoothManager.java | 8 | ||||
-rw-r--r-- | java/jni/BluetoothManager.cxx | 30 |
2 files changed, 38 insertions, 0 deletions
diff --git a/java/BluetoothManager.java b/java/BluetoothManager.java index 0c415d68..489ac3eb 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 dac0432c..dc6b038a 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 { |