diff options
-rw-r--r-- | api/direct_bt/BTAdapter.hpp | 7 | ||||
-rw-r--r-- | examples/direct_bt_peripheral00/dbt_peripheral00.cpp | 34 | ||||
-rw-r--r-- | examples/direct_bt_scanner10/dbt_scanner10.cpp | 2 | ||||
-rw-r--r-- | examples/java/DBTPeripheral00.java | 34 | ||||
-rw-r--r-- | examples/java/DBTScanner10.java | 2 | ||||
-rw-r--r-- | java/jau/direct_bt/DBTAdapter.java | 3 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTAdapter.cxx | 11 | ||||
-rw-r--r-- | java/org/direct_bt/BTAdapter.java | 7 |
8 files changed, 66 insertions, 34 deletions
diff --git a/api/direct_bt/BTAdapter.hpp b/api/direct_bt/BTAdapter.hpp index ef975016..0247beca 100644 --- a/api/direct_bt/BTAdapter.hpp +++ b/api/direct_bt/BTAdapter.hpp @@ -616,6 +616,13 @@ namespace direct_bt { HCIStatusCode initialize(const BTMode btMode=BTMode::DUAL) noexcept; /** + * Returns true, if this adapter has already been initialize() 'ed. Otherwise false. + * + * This helps avoiding re-initializing, if not so desired. + */ + bool isInitialized() const noexcept { return adapter_initialized.load(); } + + /** * Reset the adapter. * <p> * The semantics are specific to the HCI host implementation, diff --git a/examples/direct_bt_peripheral00/dbt_peripheral00.cpp b/examples/direct_bt_peripheral00/dbt_peripheral00.cpp index 51b14c9c..ca6c0b04 100644 --- a/examples/direct_bt_peripheral00/dbt_peripheral00.cpp +++ b/examples/direct_bt_peripheral00/dbt_peripheral00.cpp @@ -217,24 +217,26 @@ static bool initAdapter(std::shared_ptr<BTAdapter>& adapter) { fprintf_td(stderr, "initAdapter: Adapter not selected: %s\n", adapter->toString().c_str()); return false; } - // setName(..) .. - if( adapter->setPowered(false) ) { - const HCIStatusCode status = adapter->setName(adapter_name, adapter_short_name); - if( HCIStatusCode::SUCCESS == status ) { - fprintf_td(stderr, "initAdapter: setLocalName OK: %s\n", adapter->toString().c_str()); + if( !adapter->isInitialized() ) { + // setName(..) .. + if( adapter->setPowered(false) ) { + const HCIStatusCode status = adapter->setName(adapter_name, adapter_short_name); + if( HCIStatusCode::SUCCESS == status ) { + fprintf_td(stderr, "initAdapter: setLocalName OK: %s\n", adapter->toString().c_str()); + } else { + fprintf_td(stderr, "initAdapter: setLocalName failed: %s\n", adapter->toString().c_str()); + } } else { - fprintf_td(stderr, "initAdapter: setLocalName failed: %s\n", adapter->toString().c_str()); + fprintf_td(stderr, "initAdapter: setPowered failed: %s\n", adapter->toString().c_str()); } - } else { - fprintf_td(stderr, "initAdapter: setPowered failed: %s\n", adapter->toString().c_str()); - } - // Initialize with defaults and power-on - { - const HCIStatusCode status = adapter->initialize( btMode ); - if( HCIStatusCode::SUCCESS != status ) { - fprintf_td(stderr, "initAdapter: Adapter initialization failed: %s: %s\n", - to_string(status).c_str(), adapter->toString().c_str()); - return false; + // Initialize with defaults and power-on + { + const HCIStatusCode status = adapter->initialize( btMode ); + if( HCIStatusCode::SUCCESS != status ) { + fprintf_td(stderr, "initAdapter: Adapter initialization failed: %s: %s\n", + to_string(status).c_str(), adapter->toString().c_str()); + return false; + } } } // Even if adapter is not yet powered, listen to it and act when it gets powered-on diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index c40f3d10..4d3191c5 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -658,7 +658,7 @@ static bool initAdapter(std::shared_ptr<BTAdapter>& adapter) { return false; } // Initialize with defaults and power-on - { + if( !adapter->isInitialized() ) { HCIStatusCode status = adapter->initialize( btMode ); if( HCIStatusCode::SUCCESS != status ) { fprintf_td(stderr, "initAdapter: Adapter initialization failed: %s: %s\n", diff --git a/examples/java/DBTPeripheral00.java b/examples/java/DBTPeripheral00.java index 527b69d5..b0ff329f 100644 --- a/examples/java/DBTPeripheral00.java +++ b/examples/java/DBTPeripheral00.java @@ -234,24 +234,26 @@ public class DBTPeripheral00 { BTUtils.fprintf_td(System.err, "initAdapter: Adapter not selected: %s\n", adapter.toString()); return false; } - // setName(..) .. - if( adapter.setPowered(false) ) { - final HCIStatusCode status = adapter.setName(adapter_name, adapter_short_name); - if( HCIStatusCode.SUCCESS == status ) { - BTUtils.fprintf_td(System.err, "initAdapter: setLocalName OK: %s\n", adapter.toString()); + if( !adapter.isInitialized() ) { + // setName(..) .. + if( adapter.setPowered(false) ) { + final HCIStatusCode status = adapter.setName(adapter_name, adapter_short_name); + if( HCIStatusCode.SUCCESS == status ) { + BTUtils.fprintf_td(System.err, "initAdapter: setLocalName OK: %s\n", adapter.toString()); + } else { + BTUtils.fprintf_td(System.err, "initAdapter: setLocalName failed: %s\n", adapter.toString()); + } } else { - BTUtils.fprintf_td(System.err, "initAdapter: setLocalName failed: %s\n", adapter.toString()); + BTUtils.fprintf_td(System.err, "initAdapter: setPowered failed: %s\n", adapter.toString()); } - } else { - BTUtils.fprintf_td(System.err, "initAdapter: setPowered failed: %s\n", adapter.toString()); - } - // Initialize with defaults and power-on - { - final HCIStatusCode status = adapter.initialize( btMode ); - if( HCIStatusCode.SUCCESS != status ) { - BTUtils.fprintf_td(System.err, "initAdapter: Adapter initialization failed: %s: %s\n", - status.toString(), adapter.toString()); - return false; + // Initialize with defaults and power-on + { + final HCIStatusCode status = adapter.initialize( btMode ); + if( HCIStatusCode.SUCCESS != status ) { + BTUtils.fprintf_td(System.err, "initAdapter: Adapter initialization failed: %s: %s\n", + status.toString(), adapter.toString()); + return false; + } } } // Even if adapter is not yet powered, listen to it and act when it gets powered-on diff --git a/examples/java/DBTScanner10.java b/examples/java/DBTScanner10.java index 184309d1..605a90a3 100644 --- a/examples/java/DBTScanner10.java +++ b/examples/java/DBTScanner10.java @@ -657,7 +657,7 @@ public class DBTScanner10 { return false; } // Initialize with defaults and power-on - { + if( !adapter.isInitialized() ) { final HCIStatusCode status = adapter.initialize( btMode ); if( HCIStatusCode.SUCCESS != status ) { BTUtils.fprintf_td(System.err, "initAdapter: Adapter initialization failed: %s: %s\n", diff --git a/java/jau/direct_bt/DBTAdapter.java b/java/jau/direct_bt/DBTAdapter.java index 56139e14..f9d7183f 100644 --- a/java/jau/direct_bt/DBTAdapter.java +++ b/java/jau/direct_bt/DBTAdapter.java @@ -290,6 +290,9 @@ public class DBTAdapter extends DBTObject implements BTAdapter private native byte initializeImpl(final byte btModeInt); @Override + public final native boolean isInitialized(); + + @Override public final HCIStatusCode reset() { return HCIStatusCode.get( resetImpl() ); } diff --git a/java/jni/direct_bt/DBTAdapter.cxx b/java/jni/direct_bt/DBTAdapter.cxx index b5bd1083..259686c3 100644 --- a/java/jni/direct_bt/DBTAdapter.cxx +++ b/java/jni/direct_bt/DBTAdapter.cxx @@ -890,6 +890,17 @@ jbyte Java_jau_direct_1bt_DBTAdapter_initializeImpl(JNIEnv *env, jobject obj, jb return (jbyte) number(HCIStatusCode::INTERNAL_FAILURE); } +jboolean Java_jau_direct_1bt_DBTAdapter_isInitialized(JNIEnv *env, jobject obj) { + try { + BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); + jau::JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); + return adapter->isInitialized(); + } catch(...) { + rethrow_and_raise_java_exception(env); + } + return JNI_FALSE; +} + jbyte Java_jau_direct_1bt_DBTAdapter_resetImpl(JNIEnv *env, jobject obj) { try { BTAdapter *adapter = jau::getJavaUplinkObject<BTAdapter>(env, obj); diff --git a/java/org/direct_bt/BTAdapter.java b/java/org/direct_bt/BTAdapter.java index eff93a62..bc54586d 100644 --- a/java/org/direct_bt/BTAdapter.java +++ b/java/org/direct_bt/BTAdapter.java @@ -490,6 +490,13 @@ public interface BTAdapter extends BTObject public HCIStatusCode initialize(final BTMode btMode); /** + * Returns true, if this adapter has already been {@link #initialize(BTMode)} 'ed. Otherwise false. + * + * This helps avoiding re-initializing, if not so desired. + */ + boolean isInitialized(); + + /** * Reset the adapter. * <p> * The semantics are specific to the HCI host implementation, |