From b88401a0bd3043e6ecf5bfdc5a99a10cf36b31d6 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 31 Jan 2020 07:00:18 +0100 Subject: Expose BluetoothDevice async calls: device1_call_connect(..) / device1_call_connect_finish via Java connectAsyncStart() and connectAsyncFinish() --- api/tinyb/BluetoothDevice.hpp | 9 +++++++++ java/BluetoothDevice.java | 10 ++++++++++ java/jni/BluetoothDevice.cxx | 39 +++++++++++++++++++++++++++++++++++++++ src/BluetoothDevice.cpp | 23 +++++++++++++++++++++++ 4 files changed, 81 insertions(+) diff --git a/api/tinyb/BluetoothDevice.hpp b/api/tinyb/BluetoothDevice.hpp index cef30504..e3300b56 100644 --- a/api/tinyb/BluetoothDevice.hpp +++ b/api/tinyb/BluetoothDevice.hpp @@ -106,6 +106,15 @@ public: bool disconnect ( ); + /** An asynchronous connection to this device is initiated, + * connecting each profile flagged as auto-connectable. + */ + void connect_async_start (); + + /** Completion of the initiated asynchronous connection. + */ + bool connect_async_finish (); + /** A connection to this device is established, connecting each profile * flagged as auto-connectable. * @return TRUE if the device connected diff --git a/java/BluetoothDevice.java b/java/BluetoothDevice.java index fa7b1fe0..da077117 100644 --- a/java/BluetoothDevice.java +++ b/java/BluetoothDevice.java @@ -75,6 +75,16 @@ public class BluetoothDevice extends BluetoothObject */ public native boolean disconnect() throws BluetoothException; + /** An asynchronous connection to this device is initiated, + * connecting each profile flagged as auto-connectable. + */ + public native void connectAsyncStart() throws BluetoothException; + + /** Completion of the initiated asynchronous connection. + * @return TRUE if the device connected + */ + public native boolean connectAsyncFinish() throws BluetoothException; + /** A connection to this device is established, connecting each profile * flagged as auto-connectable. * @return TRUE if the device connected diff --git a/java/jni/BluetoothDevice.cxx b/java/jni/BluetoothDevice.cxx index 387c9559..5f9dcf41 100644 --- a/java/jni/BluetoothDevice.cxx +++ b/java/jni/BluetoothDevice.cxx @@ -92,6 +92,45 @@ jboolean Java_tinyb_BluetoothDevice_disconnect(JNIEnv *env, jobject obj) return JNI_FALSE; } +void Java_tinyb_BluetoothDevice_connectAsyncStart(JNIEnv *env, jobject obj) +{ + try { + BluetoothDevice *obj_device = getInstance(env, obj); + + obj_device->connect_async_start(); + } 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); + } +} + +jboolean Java_tinyb_BluetoothDevice_connectAsyncFinish(JNIEnv *env, jobject obj) +{ + try { + BluetoothDevice *obj_device = getInstance(env, obj); + + return obj_device->connect_async_finish() ? JNI_TRUE : JNI_FALSE; + } 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 JNI_FALSE; +} + jboolean Java_tinyb_BluetoothDevice_connect(JNIEnv *env, jobject obj) { try { diff --git a/src/BluetoothDevice.cpp b/src/BluetoothDevice.cpp index d860f8ab..31a9736d 100644 --- a/src/BluetoothDevice.cpp +++ b/src/BluetoothDevice.cpp @@ -242,6 +242,29 @@ bool BluetoothDevice::disconnect () return result; } +void BluetoothDevice::connect_async_start () +{ + device1_call_connect ( + object, // Device1 *proxy, + NULL, // GCancellable *cancellable, + NULL, // GAsyncReadyCallback callback, + NULL // gpointer user_data) + ); +} + +bool BluetoothDevice::connect_async_finish () +{ + GError *error = NULL; + bool result; + result = device1_call_connect_finish ( + object, // Device1 *proxy, + NULL, // GAsyncResult *res, + &error // GError **error) + ); + handle_error(error); + return result; +} + bool BluetoothDevice::connect () { GError *error = NULL; -- cgit v1.2.3