summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-05-14 14:06:24 +0200
committerSven Gothel <[email protected]>2020-05-14 14:06:24 +0200
commit07ee3e5457a5cdf4305ef4e1f404b2c632dfed07 (patch)
tree791e80bd33ff57436e902254992d62002549c1fe
parent9e1dec8ed7c5b71f45c35817101e3f5b03e5d4aa (diff)
DBTManager(java/jni): get/setDefaultAdapter(..) java side implementation, remove JNI code
-rw-r--r--java/direct_bt/tinyb/DBTManager.java18
-rw-r--r--java/jni/direct_bt/DBTManager.cxx37
2 files changed, 11 insertions, 44 deletions
diff --git a/java/direct_bt/tinyb/DBTManager.java b/java/direct_bt/tinyb/DBTManager.java
index e458403d..e18f56e4 100644
--- a/java/direct_bt/tinyb/DBTManager.java
+++ b/java/direct_bt/tinyb/DBTManager.java
@@ -125,6 +125,7 @@ public class DBTManager implements BluetoothManager
private long nativeInstance;
private static DBTManager inst;
private final List<BluetoothAdapter> adapters = new ArrayList<BluetoothAdapter>();
+ private int defaultAdapterIndex = 0;
public BluetoothType getBluetoothType() { return BluetoothType.NONE; }
@@ -179,10 +180,18 @@ public class DBTManager implements BluetoothManager
public List<BluetoothGattService> getServices() { throw new UnsupportedOperationException(); } // FIXME
@Override
- public boolean setDefaultAdapter(final BluetoothAdapter adapter) { throw new UnsupportedOperationException(); } // FIXME
+ public boolean setDefaultAdapter(final BluetoothAdapter adapter) {
+ final int idx = adapters.indexOf(adapter);
+ if( 0 <= idx ) {
+ defaultAdapterIndex = idx;
+ return true;
+ }
+ return false;
+ }
+
@Override
- public BluetoothAdapter getDefaultAdapter() { return adapters.get(0); }
+ public BluetoothAdapter getDefaultAdapter() { return adapters.get(defaultAdapterIndex); }
@Override
public boolean startDiscovery() throws BluetoothException { return getDefaultAdapter().startDiscovery(); }
@@ -193,11 +202,6 @@ public class DBTManager implements BluetoothManager
@Override
public boolean getDiscovering() throws BluetoothException { return getDefaultAdapter().getDiscovering(); }
- /**
- * Returns an opened default adapter instance!
- * @throws BluetoothException in case adapter is invalid or could not have been opened.
- */
- private native DBTAdapter getDefaultAdapterImpl() throws BluetoothException;
private native List<BluetoothAdapter> getAdapterListImpl();
private native void initImpl() throws BluetoothException;
diff --git a/java/jni/direct_bt/DBTManager.cxx b/java/jni/direct_bt/DBTManager.cxx
index c870b6da..6e59b1fb 100644
--- a/java/jni/direct_bt/DBTManager.cxx
+++ b/java/jni/direct_bt/DBTManager.cxx
@@ -66,43 +66,6 @@ void Java_direct_1bt_tinyb_DBTManager_deleteImpl(JNIEnv *env, jobject obj)
static const std::string _adapterClazzCtorArgs("(JLjava/lang/String;Ljava/lang/String;)V");
-jobject Java_direct_1bt_tinyb_DBTManager_getDefaultAdapterImpl(JNIEnv *env, jobject obj)
-{
- try {
- DBTManager *manager = getInstance<DBTManager>(env, obj);
- DBG_PRINT("Java_direct_1bt_tinyb_DBTManager_getDefaultAdapterImpl: Manager %s", manager->toString().c_str());
- (void) manager;
- const int defAdapterIdx = 0;
- DBTAdapter * adapter = new DBTAdapter(defAdapterIdx);
- if( !adapter->isValid() ) {
- delete adapter;
- throw BluetoothException("Invalid default adapter "+std::to_string(defAdapterIdx), E_FILE_LINE);
- }
- if( !adapter->hasDevId() ) {
- delete adapter;
- throw BluetoothException("Invalid default adapter dev-id "+std::to_string(defAdapterIdx), E_FILE_LINE);
- }
-
- // prepare adapter ctor
- const jstring addr = from_string_to_jstring(env, adapter->getAddressString());
- const jstring name = from_string_to_jstring(env, adapter->getName());
- if( java_exception_check(env, E_FILE_LINE) ) { return nullptr; }
- const jclass clazz = search_class(env, *adapter);
- const jmethodID clazz_ctor = search_method(env, clazz, "<init>", _adapterClazzCtorArgs.c_str(), false);
- jobject jAdapter = env->NewObject(clazz, clazz_ctor, (jlong)adapter, addr, name);
- if( java_exception_check(env, E_FILE_LINE) ) { return nullptr; }
- JNIGlobalRef::check(jAdapter, E_FILE_LINE);
- std::shared_ptr<JavaAnonObj> jAdapterRef = adapter->getJavaObject();
- JavaGlobalObj::check(jAdapterRef, E_FILE_LINE);
-
- DBG_PRINT("Java_direct_1bt_tinyb_DBTManager_getDefaultAdapterImpl: New Adapter %p %s", adapter, adapter->toString().c_str());
- return JavaGlobalObj::GetObject(jAdapterRef);
- } catch(...) {
- rethrow_and_raise_java_exception(env);
- }
- return nullptr;
-}
-
jobject Java_direct_1bt_tinyb_DBTManager_getAdapterListImpl(JNIEnv *env, jobject obj)
{
try {