summaryrefslogtreecommitdiffstats
path: root/java/jni/tinyb/DBusAdapter.cxx
blob: fdc401014b684f5c93bbce861d277feb860e7851 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
 * Author: Andrei Vasiliu <andrei.vasiliu@intel.com>
 * Copyright (c) 2016 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "tinyb/BluetoothAdapter.hpp"
#include "tinyb/BluetoothDevice.hpp"
#include "tinyb/BluetoothObject.hpp"

#include "tinyb_dbus_DBusAdapter.h"

#include "JNIMem.hpp"
#include "helper_tinyb.hpp"

using namespace tinyb;

jobject Java_tinyb_dbus_DBusAdapter_getBluetoothType(JNIEnv *env, jobject obj)
{
    try {
        (void)obj;

        return get_bluetooth_type(env, "ADAPTER");
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return nullptr;
}

jobject Java_tinyb_dbus_DBusAdapter_clone(JNIEnv *env, jobject obj)
{
    try {
        return generic_clone<BluetoothAdapter>(env, obj);
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return nullptr;
}

jboolean Java_tinyb_dbus_DBusAdapter_startDiscovery(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        return obj_adapter->start_discovery() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

jboolean Java_tinyb_dbus_DBusAdapter_stopDiscoveryImpl(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        return obj_adapter->stop_discovery() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

jobject Java_tinyb_dbus_DBusAdapter_getDevices(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
        std::vector<std::unique_ptr<BluetoothDevice>> array = obj_adapter->get_devices();
        jobject result = convert_vector_uniqueptr_to_jarraylist<BluetoothDevice>(env, array,
                                                                    "(J)V");

        return result;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return nullptr;
}

jint Java_tinyb_dbus_DBusAdapter_removeDevices(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
        std::vector<std::unique_ptr<tinyb::BluetoothDevice>> array = obj_adapter->get_devices();
        
        for (unsigned int i =0;i<array.size();i++) {
            std::unique_ptr<tinyb::BluetoothDevice> *obj_device = &array.at(i);
            std::string path = obj_device->get()->get_object_path();
            // printf("PATH:%s\n", path.c_str());
            // fflush(stdout);
            obj_adapter->remove_device(path.c_str());
            
        }
        return array.size();
        
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return 0;
}

jstring Java_tinyb_dbus_DBusAdapter_getAddress(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
        std::string address = obj_adapter->get_address();

        return env->NewStringUTF((const char *)address.c_str());
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return nullptr;
}

jstring Java_tinyb_dbus_DBusAdapter_getName(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
        std::string name = obj_adapter->get_name();

        return env->NewStringUTF((const char *)name.c_str());
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return nullptr;
}

jstring Java_tinyb_dbus_DBusAdapter_getAlias(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
        std::string alias = obj_adapter->get_alias();

        return env->NewStringUTF((const char *)alias.c_str());
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return nullptr;
}

void Java_tinyb_dbus_DBusAdapter_setAlias(JNIEnv *env, jobject obj, jstring str)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        const std::string string_to_write = from_jstring_to_string(env, str);

        obj_adapter->set_alias(string_to_write);
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

jlong Java_tinyb_dbus_DBusAdapter_getBluetoothClass(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        return (jlong)obj_adapter->get_class();
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return 0;
}

jboolean Java_tinyb_dbus_DBusAdapter_getPowered(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        return obj_adapter->get_powered() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

jboolean Java_tinyb_dbus_DBusAdapter_setPowered(JNIEnv *env, jobject obj, jboolean val)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        bool val_to_write = from_jboolean_to_bool(val);
        obj_adapter->set_powered(val_to_write);
        return JNI_TRUE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

void Java_tinyb_dbus_DBusAdapter_enablePoweredNotifications(JNIEnv *env, jobject obj, jobject callback)
{
    try {
        BluetoothAdapter *obj_adapter =
                                    getInstance<BluetoothAdapter>(env, obj);
        std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
        obj_adapter->enable_powered_notifications([ callback_ptr ] (bool v)
            {
                jclass notification = search_class(*jni_env, **callback_ptr);
                jmethodID  method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
                jni_env->DeleteLocalRef(notification);

                jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
                jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);

                jobject result = jni_env->NewObject(boolean_cls, constructor, v ? JNI_TRUE : JNI_FALSE);
                jni_env->DeleteLocalRef(boolean_cls);

                jni_env->CallVoidMethod(**callback_ptr, method, result);
                jni_env->DeleteLocalRef(result);

            });
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

void Java_tinyb_dbus_DBusAdapter_disablePoweredNotifications(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter =
                                    getInstance<BluetoothAdapter>(env, obj);
        obj_adapter->disable_powered_notifications();
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

jboolean Java_tinyb_dbus_DBusAdapter_getDiscoverable(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        return obj_adapter->get_discoverable() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

jboolean Java_tinyb_dbus_DBusAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean val)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        bool val_to_write = from_jboolean_to_bool(val);
        obj_adapter->set_discoverable(val_to_write);
        return JNI_TRUE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

void Java_tinyb_dbus_DBusAdapter_enableDiscoverableNotifications(JNIEnv *env, jobject obj, jobject callback)
{
    try {
        BluetoothAdapter *obj_adapter =
                                    getInstance<BluetoothAdapter>(env, obj);
        std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
        obj_adapter->enable_discoverable_notifications([ callback_ptr ] (bool v)
            {
                jclass notification = search_class(*jni_env, **callback_ptr);
                jmethodID  method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
                jni_env->DeleteLocalRef(notification);

                jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
                jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);

                jobject result = jni_env->NewObject(boolean_cls, constructor, v ? JNI_TRUE : JNI_FALSE);
                jni_env->DeleteLocalRef(boolean_cls);

                jni_env->CallVoidMethod(**callback_ptr, method, result);
                jni_env->DeleteLocalRef(result);

            });
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

void Java_tinyb_dbus_DBusAdapter_disableDiscoverableNotifications(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter =
                                    getInstance<BluetoothAdapter>(env, obj);
        obj_adapter->disable_discoverable_notifications();
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

jlong Java_tinyb_dbus_DBusAdapter_getDiscoverableTimeout(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        return (jlong)obj_adapter->get_discoverable_timeout();
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return 0;
}

jboolean Java_tinyb_dbus_DBusAdapter_setDiscoverableTimout(JNIEnv *env, jobject obj, jlong timeout)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        if (timeout < 0)
        {
            throw std::invalid_argument("timeout argument is negative\n");
        }
        obj_adapter->set_discoverable_timeout((unsigned int)timeout);
        return JNI_TRUE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

jboolean Java_tinyb_dbus_DBusAdapter_getPairable(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        return obj_adapter->get_pairable() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

void Java_tinyb_dbus_DBusAdapter_enablePairableNotifications(JNIEnv *env, jobject obj, jobject callback)
{
    try {
        BluetoothAdapter *obj_adapter =
                                    getInstance<BluetoothAdapter>(env, obj);
        std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
        obj_adapter->enable_pairable_notifications([ callback_ptr ] (bool v)
            {
                jclass notification = search_class(*jni_env, **callback_ptr);
                jmethodID  method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
                jni_env->DeleteLocalRef(notification);

                jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
                jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);

                jobject result = jni_env->NewObject(boolean_cls, constructor, v ? JNI_TRUE : JNI_FALSE);
                jni_env->DeleteLocalRef(boolean_cls);

                jni_env->CallVoidMethod(**callback_ptr, method, result);
                jni_env->DeleteLocalRef(result);

            });
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

void Java_tinyb_dbus_DBusAdapter_disablePairableNotifications(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter =
                                    getInstance<BluetoothAdapter>(env, obj);
        obj_adapter->disable_pairable_notifications();
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

jboolean Java_tinyb_dbus_DBusAdapter_setPairable(JNIEnv *env, jobject obj, jboolean val)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        bool val_to_write = from_jboolean_to_bool(val);
        obj_adapter->set_pairable(val_to_write);
        return JNI_TRUE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

jlong Java_tinyb_dbus_DBusAdapter_getPairableTimeout(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        return (jlong)obj_adapter->get_pairable_timeout();
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return 0;
}

jboolean Java_tinyb_dbus_DBusAdapter_setPairableTimeout(JNIEnv *env, jobject obj, jlong timeout)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        if (timeout < 0)
        {
            throw std::invalid_argument("timeout argument is negative\n");
        }
        obj_adapter->set_pairable_timeout((unsigned int)timeout);
        return JNI_TRUE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

jboolean Java_tinyb_dbus_DBusAdapter_getDiscovering(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        return obj_adapter->get_discovering() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return JNI_FALSE;
}

void Java_tinyb_dbus_DBusAdapter_enableDiscoveringNotifications(JNIEnv *env, jobject obj, jobject callback)
{
    try {
        BluetoothAdapter *obj_adapter =
                                    getInstance<BluetoothAdapter>(env, obj);
        std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
        obj_adapter->enable_discovering_notifications([ callback_ptr ] (bool v)
            {
                jclass notification = search_class(*jni_env, **callback_ptr);
                jmethodID  method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
                jni_env->DeleteLocalRef(notification);

                jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
                jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);

                jobject result = jni_env->NewObject(boolean_cls, constructor, v ? JNI_TRUE : JNI_FALSE);
                jni_env->DeleteLocalRef(boolean_cls);

                jni_env->CallVoidMethod(**callback_ptr, method, result);
                jni_env->DeleteLocalRef(result);

            });
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

void Java_tinyb_dbus_DBusAdapter_disableDiscoveringNotifications(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter =
                                    getInstance<BluetoothAdapter>(env, obj);
        obj_adapter->disable_discovering_notifications();
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

jobjectArray Java_tinyb_dbus_DBusAdapter_getUUIDs(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
        std::vector<std::string> uuids = obj_adapter->get_uuids();
        unsigned int uuids_size = uuids.size();

        jclass string_class = search_class(env, "Ljava/lang/String;");
        jobjectArray result = env->NewObjectArray(uuids_size, string_class, 0);
        if (!result)
        {
            throw std::bad_alloc();
        }

        for (unsigned int i = 0; i < uuids_size; ++i)
        {
            std::string str_elem = uuids.at(i);
            jobject elem = env->NewStringUTF((const char *)str_elem.c_str());
            env->SetObjectArrayElement(result, i, elem);
        }

        return result;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return nullptr;
}

jstring Java_tinyb_dbus_DBusAdapter_getModalias(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);
        std::unique_ptr<std::string> modalias = obj_adapter->get_modalias();
        if(modalias == nullptr)
            return nullptr;

        return env->NewStringUTF((const char *)modalias->c_str());
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return nullptr;
}

void Java_tinyb_dbus_DBusAdapter_delete(JNIEnv *env, jobject obj)
{
    try {
        BluetoothAdapter *adapter = getInstance<BluetoothAdapter>(env, obj);
        delete adapter;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

void Java_tinyb_dbus_DBusAdapter_setDiscoveryFilter(JNIEnv *env, jobject obj, jobject uuids, jint rssi, jint pathloss, jint transportType)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        jclass cList = env->FindClass("java/util/List");

        jmethodID mSize = env->GetMethodID(cList, "size", "()I");
        jmethodID mGet = env->GetMethodID(cList, "get", "(I)Ljava/lang/Object;");

        jint size = env->CallIntMethod(uuids, mSize);
        std::vector<BluetoothUUID> native_uuids;

        for (jint i = 0; i < size; i++) {
            jstring strObj = (jstring) env->CallObjectMethod(uuids, mGet, i);
            const char * chr = env->GetStringUTFChars(strObj, NULL);
            BluetoothUUID uuid(chr);
            native_uuids.push_back(uuid);
            env->ReleaseStringUTFChars(strObj, chr);
        }

        TransportType t_type = from_int_to_transport_type((int) transportType);

        obj_adapter->set_discovery_filter(native_uuids, (int16_t) rssi, (uint16_t) pathloss, t_type);
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
}

jobject Java_tinyb_dbus_DBusAdapter_connectDevice(JNIEnv *env, jobject obj, jstring jaddress, jstring jaddressType)
{
    try {
        BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj);

        const std::string address = from_jstring_to_string(env, jaddress);
        const std::string addressType = from_jstring_to_string(env, jaddressType);

        fprintf(stderr, "connectDeviceJ.0\n"); fflush(stderr);
        std::unique_ptr<tinyb::BluetoothDevice> b_device = obj_adapter->connect_device(address, addressType);
        fprintf(stderr, "connectDeviceJ.1\n"); fflush(stderr);

        BluetoothDevice *b_device_naked = b_device.release();
        fprintf(stderr, "connectDeviceJ.2\n"); fflush(stderr);
        if (!b_device_naked)
        {
            return nullptr;
        }
        jclass clazz = search_class(env, *b_device_naked);
        jmethodID clazz_ctor = search_method(env, clazz, "<init>", "(J)V", false);

        jobject result = env->NewObject(clazz, clazz_ctor, (jlong)b_device_naked);
        fprintf(stderr, "connectDeviceJ.X\n"); fflush(stderr);
        return result;
    } catch(...) {
        rethrow_and_raise_java_exception(env);
    }
    return NULL;
}