aboutsummaryrefslogtreecommitdiffstats
path: root/java/jni
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-05-25 01:17:26 +0200
committerSven Gothel <[email protected]>2020-05-25 01:17:26 +0200
commitf82491139dd92ee17dbd6625462fa7f6e59fbf60 (patch)
treea20d2f667f394d0ec2ad34805a129018020e178f /java/jni
parent95989de74073cfa0065300ba4522049898f88adc (diff)
DBTDevice::getGATTServices(): Reuse GATTHandler's list result, retrieve GenericAccess after GATT discovery and update data (name)
As there is no need to discover the GATT services more than once, we shall reuse the already stored GATTService list in GATTHandler. For the auto-connect whitelist use case (dbt_scanner10) w/o _any_ HCI discovery, the connected LE device has no name set. Hence we retrieve the GATT GenericAccess data and update the DBTDevice's name and appearance. Eventually after getGATTServices(), the device name is set!
Diffstat (limited to 'java/jni')
-rw-r--r--java/jni/direct_bt/DBTDevice.cxx77
1 files changed, 34 insertions, 43 deletions
diff --git a/java/jni/direct_bt/DBTDevice.cxx b/java/jni/direct_bt/DBTDevice.cxx
index 726a7ad4..f90aa6bb 100644
--- a/java/jni/direct_bt/DBTDevice.cxx
+++ b/java/jni/direct_bt/DBTDevice.cxx
@@ -298,20 +298,15 @@ jboolean Java_direct_1bt_tinyb_DBTDevice_connectImpl(JNIEnv *env, jobject obj)
std::shared_ptr<GATTHandler> gatt = device->connectGATT();
if( nullptr != gatt ) {
// FIXME: Split up - may offload to other thread
- std::vector<GATTServiceRef> & primServices = gatt->discoverCompletePrimaryServices();
-
- std::shared_ptr<GenericAccess> ga = gatt->getGenericAccess(primServices);
- if( nullptr != ga ) {
- env->SetShortField(obj, getField(env, obj, "appearance", "S"), static_cast<jshort>(ga->category));
- java_exception_check_and_throw(env, E_FILE_LINE);
- DBG_PRINT("GATT connected to GenericAccess: %s", ga->toString().c_str());
- }
-#if 0
- std::shared_ptr<DeviceInformation> di = gatt->getDeviceInformation(primServices);
- if( nullptr != di ) {
- DBG_PRINT("GATT connected to DeviceInformation: %s", di->toString().c_str());
+ std::vector<GATTServiceRef> services = device->getGATTServices(); // implicit GATT connect and discovery if required
+ if( services.size() > 0 ) {
+ std::shared_ptr<GenericAccess> ga = device->getGATTGenericAccess();
+ if( nullptr != ga ) {
+ env->SetShortField(obj, getField(env, obj, "appearance", "S"), static_cast<jshort>(ga->appearance));
+ java_exception_check_and_throw(env, E_FILE_LINE);
+ DBG_PRINT("GATT connected to GenericAccess: %s", ga->toString().c_str());
+ }
}
-#endif
return JNI_TRUE;
}
} catch(...) {
@@ -331,36 +326,32 @@ jobject Java_direct_1bt_tinyb_DBTDevice_getServices(JNIEnv *env, jobject obj) {
DBTDevice *device = getInstance<DBTDevice>(env, obj);
JavaGlobalObj::check(device->getJavaObject(), E_FILE_LINE);
- std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
- if( nullptr != gatt ) {
- std::vector<std::shared_ptr<GATTService>> & services = gatt->getServices();
- // DBTGattService(final long nativeInstance, final DBTDevice device, final boolean isPrimary,
- // final String type_uuid, final short handleStart, final short handleEnd)
-
- std::function<jobject(JNIEnv*, jclass, jmethodID, GATTService*)> ctor_service =
- [](JNIEnv *env, jclass clazz, jmethodID clazz_ctor, GATTService *service)->jobject {
- // prepare adapter ctor
- JavaGlobalObj::check(service->device->getJavaObject(), E_FILE_LINE);
- jobject jdevice = JavaGlobalObj::GetObject(service->device->getJavaObject());
- const jboolean isPrimary = service->isPrimary;
- const jstring uuid = from_string_to_jstring(env,
- directBTJNISettings.getUnifyUUID128Bit() ? service->type->toUUID128String() :
- service->type->toString());
- java_exception_check_and_throw(env, E_FILE_LINE);
-
- jobject jservice = env->NewObject(clazz, clazz_ctor, (jlong)service, jdevice, isPrimary,
- uuid, service->startHandle, service->endHandle);
- java_exception_check_and_throw(env, E_FILE_LINE);
- JNIGlobalRef::check(jservice, E_FILE_LINE);
- std::shared_ptr<JavaAnonObj> jServiceRef = service->getJavaObject();
- JavaGlobalObj::check(jServiceRef, E_FILE_LINE);
-
- return JavaGlobalObj::GetObject(jServiceRef);
- };
- return convert_vector_sharedptr_to_jarraylist<GATTService>(env, services, _serviceClazzCtorArgs.c_str(), ctor_service);
- } else {
- WARN_PRINT("Device GATTHandle not connected: %s", device->toString().c_str());
- }
+ std::vector<GATTServiceRef> services = device->getGATTServices(); // implicit GATT connect and discovery if required
+
+ // DBTGattService(final long nativeInstance, final DBTDevice device, final boolean isPrimary,
+ // final String type_uuid, final short handleStart, final short handleEnd)
+
+ std::function<jobject(JNIEnv*, jclass, jmethodID, GATTService*)> ctor_service =
+ [](JNIEnv *env, jclass clazz, jmethodID clazz_ctor, GATTService *service)->jobject {
+ // prepare adapter ctor
+ JavaGlobalObj::check(service->device->getJavaObject(), E_FILE_LINE);
+ jobject jdevice = JavaGlobalObj::GetObject(service->device->getJavaObject());
+ const jboolean isPrimary = service->isPrimary;
+ const jstring uuid = from_string_to_jstring(env,
+ directBTJNISettings.getUnifyUUID128Bit() ? service->type->toUUID128String() :
+ service->type->toString());
+ java_exception_check_and_throw(env, E_FILE_LINE);
+
+ jobject jservice = env->NewObject(clazz, clazz_ctor, (jlong)service, jdevice, isPrimary,
+ uuid, service->startHandle, service->endHandle);
+ java_exception_check_and_throw(env, E_FILE_LINE);
+ JNIGlobalRef::check(jservice, E_FILE_LINE);
+ std::shared_ptr<JavaAnonObj> jServiceRef = service->getJavaObject();
+ JavaGlobalObj::check(jServiceRef, E_FILE_LINE);
+
+ return JavaGlobalObj::GetObject(jServiceRef);
+ };
+ return convert_vector_sharedptr_to_jarraylist<GATTService>(env, services, _serviceClazzCtorArgs.c_str(), ctor_service);
} catch(...) {
rethrow_and_raise_java_exception(env);
}