26 #include "jau_direct_bt_DBTDevice.h"
61 const BTGattChar * associatedCharacteristicRef;
64 jmethodID mNotificationReceived =
nullptr;
65 jmethodID mIndicationReceived =
nullptr;
70 : associatedCharacteristicRef(associatedCharacteristicRef_),
75 if(
nullptr == listenerClazz ) {
79 if(
nullptr != associatedCharacteristicRef_ ) {
81 associatedCharacteristicObj = JavaGlobalObj::GetJavaObject(associatedCharacteristicRef_->
getJavaObject());
86 if(
nullptr == mNotificationReceived ) {
91 if(
nullptr == mNotificationReceived ) {
97 if(
nullptr == associatedCharacteristicRef ) {
100 return characteristic == *associatedCharacteristicRef;
105 std::shared_ptr<jau::JavaAnon> jCharDeclRef = charDecl->getJavaObject();
112 const size_t value_size =
charValue.getSize();
113 jbyteArray jval = env->NewByteArray((jsize)value_size);
114 env->SetByteArrayRegion(jval, 0, (jsize)value_size, (
const jbyte *)
charValue.get_ptr());
117 env->CallVoidMethod(listenerObj.
getObject(), mNotificationReceived,
118 jCharDecl, jval, (jlong)timestamp);
120 env->DeleteLocalRef(jval);
125 const bool confirmationSent)
override {
126 std::shared_ptr<jau::JavaAnon> jCharDeclRef = charDecl->getJavaObject();
133 const size_t value_size =
charValue.getSize();
134 jbyteArray jval = env->NewByteArray((jsize)value_size);
135 env->SetByteArrayRegion(jval, 0, (jsize)value_size, (
const jbyte *)
charValue.get_ptr());
138 env->CallVoidMethod(listenerObj.
getObject(), mIndicationReceived,
139 jCharDecl, jval, (jlong)timestamp, (jboolean)confirmationSent);
141 env->DeleteLocalRef(jval);
149 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
158 BTDevice *nativePtr = getJavaUplinkObject<BTDevice>(env, obj);
169 BTDevice *nativePtr = getJavaUplinkObject<BTDevice>(env, obj);
180 if(
nullptr == listener ) {
185 getObjectRef<JNIGattCharListener>(env, listener,
"nativeInstance");
186 if(
nullptr != pre ) {
191 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
194 if(
nullptr == gatt ) {
198 BTGattChar * associatedCharacteristicRef =
nullptr;
199 if(
nullptr != jAssociatedCharacteristic ) {
200 associatedCharacteristicRef = getJavaUplinkObject<BTGattChar>(env, jAssociatedCharacteristic);
203 std::shared_ptr<BTGattCharListener> l =
204 std::shared_ptr<BTGattCharListener>(
new JNIGattCharListener(env, device, listener, associatedCharacteristicRef) );
218 if(
nullptr == jlistener ) {
222 getObjectRef<JNIGattCharListener>(env, jlistener,
"nativeInstance");
223 if(
nullptr == pre ) {
224 WARN_PRINT(
"BTGattCharListener's nativeInstance is null, not in use");
227 setObjectRef<JNIGattCharListener>(env, jlistener,
nullptr,
"nativeInstance");
229 BTDevice *device = getJavaUplinkObjectUnchecked<BTDevice>(env, obj);
230 if(
nullptr == device ) {
236 if(
nullptr == gatt ) {
238 DBG_PRINT(
"BTGattChar's device GATTHandle not connected: %s", device->
toString().c_str());
243 WARN_PRINT(
"Failed to remove BTGattCharListener with nativeInstance: %p at %s", pre, device->
toString().c_str());
255 if(
nullptr == jAssociatedCharacteristic ) {
258 BTDevice *device = getJavaUplinkObjectUnchecked<BTDevice>(env, obj);
259 if(
nullptr == device ) {
265 if(
nullptr == gatt ) {
267 DBG_PRINT(
"BTGattChar's device GATTHandle not connected: %s", device->
toString().c_str());
271 BTGattChar * associatedCharacteristicRef = getJavaUplinkObject<BTGattChar>(env, jAssociatedCharacteristic);
283 BTDevice *device = getJavaUplinkObjectUnchecked<BTDevice>(env, obj);
284 if(
nullptr == device ) {
290 if(
nullptr == gatt ) {
292 DBG_PRINT(
"BTGattChar's device GATTHandle not connected: %s", device->
toString().c_str());
306 BTDevice *device = castInstance<BTDevice>(nativeInstance);
307 DBG_PRINT(
"Java_jau_direct_1bt_DBTDevice_deleteImpl (remove only) %s", device->
toString().c_str());
319 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
331 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
343 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
355 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
358 return (jbyte)
number(res);
366 jshort interval, jshort window,
367 jshort min_interval, jshort max_interval,
368 jshort latency, jshort timeout)
371 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
374 return (jbyte)
number(res);
383 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
395 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
398 if(
nullptr == jsink ) {
401 const size_t sink_size = env->GetArrayLength(jsink);
406 uint8_t * sink_ptr = criticalArray.
get(jsink, criticalArray.Mode::UPDATE_AND_RELEASE);
407 if( NULL == sink_ptr ) {
419 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
422 if(
nullptr == jsource ) {
425 const size_t source_size = env->GetArrayLength(jsource);
430 uint8_t * source_ptr = criticalArray.
get(jsource, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
431 if( NULL == source_ptr ) {
437 return (jbyte)
number(res);
446 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
449 if(
nullptr == jsink ) {
452 const size_t sink_size = env->GetArrayLength(jsink);
457 uint8_t * sink_ptr = criticalArray.
get(jsink, criticalArray.Mode::UPDATE_AND_RELEASE);
458 if( NULL == sink_ptr ) {
470 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
473 return (jbyte)
number(res);
482 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
494 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
506 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
518 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
531 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
543 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
555 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
567 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
579 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
591 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
603 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
615 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
633 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
637 if( services.
size() > 0 ) {
639 if(
nullptr != ga ) {
649 std::function<jobject(JNIEnv*, jclass, jmethodID,
BTGattService*)> ctor_service =
650 [](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor,
BTGattService *service)->jobject {
652 std::shared_ptr<BTDevice> _device = service->getDeviceChecked();
654 jobject jdevice = JavaGlobalObj::GetObject(_device->
getJavaObject());
655 const jboolean isPrimary = service->isPrimary;
658 service->type->toString());
661 jobject jservice = env_->NewObject(clazz, clazz_ctor, (jlong)service, jdevice, isPrimary,
662 juuid, service->startHandle, service->endHandle);
665 std::shared_ptr<JavaAnon> jServiceRef = service->getJavaObject();
667 env_->DeleteLocalRef(juuid);
668 env_->DeleteLocalRef(jservice);
669 return JavaGlobalObj::GetObject(jServiceRef);
671 return convert_vector_sharedptr_to_jarraylist<jau::darray<BTGattServiceRef>,
BTGattService>(
682 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
685 return device->
pingGATT() ? JNI_TRUE : JNI_FALSE;
695 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
697 return (jshort) device->
getRSSI();
707 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
711 jclass map_cls =
search_class(env,
"java/util/HashMap");
712 jmethodID map_ctor =
search_method(env, map_cls,
"<init>",
"(I)V",
false);
713 jmethodID map_put =
search_method(env, map_cls,
"put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
false);
715 jclass short_cls =
search_class(env,
"java/lang/Short");
716 jmethodID short_ctor =
search_method(env, short_cls,
"<init>",
"(S)V",
false);
717 jobject result =
nullptr;
719 if(
nullptr != mdata ) {
720 result = env->NewObject(map_cls, map_ctor, 1);
721 jbyteArray arr = env->NewByteArray(mdata->
data.
getSize());
723 jobject key = env->NewObject(short_cls, short_ctor, mdata->
company);
724 env->CallObjectMethod(result, map_put, key, arr);
726 env->DeleteLocalRef(arr);
727 env->DeleteLocalRef(key);
729 result = env->NewObject(map_cls, map_ctor, 0);
731 if (
nullptr == result) {
744 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
761 struct BooleanDeviceCBContext {
766 jmethodID boolean_ctor;
768 BooleanDeviceCBContext(
773 jmethodID boolean_ctor_)
774 : addressAndType(addressAndType_), javaCallback_ref(javaCallback_ref_),
775 mRun(mRun_), boolean_cls_ref(boolean_cls_ref_), boolean_ctor(boolean_ctor_)
779 bool operator==(
const BooleanDeviceCBContext& rhs)
const
784 return rhs.addressAndType == addressAndType &&
785 rhs.javaCallback_ref == javaCallback_ref;
788 bool operator!=(
const BooleanDeviceCBContext& rhs)
const
789 {
return !( *
this == rhs ); }
792 typedef std::shared_ptr<BooleanDeviceCBContext> BooleanDeviceCBContextRef;
798 static void disablePairedNotifications(JNIEnv *env, jobject obj,
BTManager &mgmt)
801 getObjectRef<InvocationFunc<bool, const MgmtEvent&>>(env, obj,
"pairedNotificationRef");
802 if(
nullptr != funcptr ) {
805 setObjectRef(env, obj, funcptr,
"pairedNotificationRef");
812 void Java_jau_direct_1bt_DBTDevice_disablePairedNotificationsImpl(JNIEnv *env, jobject obj)
815 BTDevice *device = getJavaUplinkObject<BTDevice>(env, obj);
819 disablePairedNotifications(env, obj, mgmt);
824 void Java_jau_direct_1bt_DBTDevice_enablePairedNotificationsImpl(JNIEnv *env, jobject obj, jobject javaCallback)
827 BTDevice *device= getJavaUplinkObject<BTDevice>(env, obj);
832 disablePairedNotifications(env, obj, mgmt);
834 bool(*nativeCallback)(BooleanDeviceCBContextRef&,
const MgmtEvent&) =
835 [](BooleanDeviceCBContextRef& ctx_ref,
const MgmtEvent& e)->bool {
837 if( event.getAddress() != ctx_ref->addressAndType.address ||
event.getAddressType() != ctx_ref->addressAndType.type ) {
840 jobject result =
jni_env->NewObject(ctx_ref->boolean_cls_ref.getClass(), ctx_ref->boolean_ctor, JNI_FALSE);
841 jni_env->CallVoidMethod(*(ctx_ref->javaCallback_ref), ctx_ref->mRun, result);
842 jni_env->DeleteLocalRef(result);
848 jni_env->DeleteLocalRef(notification);
854 BooleanDeviceCBContext * ctx =
new BooleanDeviceCBContext{
856 jni_env->DeleteLocalRef(boolean_cls);