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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2020 Gothel Software e.K.
* Copyright (c) 2020 ZAFENA AB
*
* 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 "direct_bt_tinyb_DBTAdapter.h"
// #define VERBOSE_ON 1
#include <dbt_debug.hpp>
#include "JNIMem.hpp"
#include "helper_base.hpp"
#include "helper_dbt.hpp"
#include "direct_bt/DBTAdapter.hpp"
using namespace direct_bt;
static const std::string _adapterSettingsClassName("org/tinyb/AdapterSettings");
static const std::string _adapterSettingsClazzCtorArgs("(I)V");
static const std::string _eirDataTypeSetClassName("org/tinyb/EIRDataTypeSet");
static const std::string _eirDataTypeSetClazzCtorArgs("(I)V");
static const std::string _deviceClazzCtorArgs("(JLdirect_bt/tinyb/DBTAdapter;Ljava/lang/String;Ljava/lang/String;J)V");
static const std::string _adapterSettingsChangedMethodArgs("(Lorg/tinyb/BluetoothAdapter;Lorg/tinyb/AdapterSettings;Lorg/tinyb/AdapterSettings;Lorg/tinyb/AdapterSettings;J)V");
static const std::string _deviceStatusMethodArgs("(Lorg/tinyb/BluetoothDevice;J)V");
static const std::string _deviceStatusUpdateMethodArgs("(Lorg/tinyb/BluetoothDevice;JLorg/tinyb/EIRDataTypeSet;)V");
class JNIAdapterStatusListener : public AdapterStatusListener {
private:
/**
package org.tinyb;
public interface AdapterStatusListener {
public void adapterSettingsChanged(final BluetoothAdapter adapter,
final AdapterSetting oldmask, final AdapterSetting newmask,
final AdapterSetting changedmask, final long timestamp);
public void deviceFound(final BluetoothDevice device, final long timestamp);
public void deviceUpdated(final BluetoothDevice device, final long timestamp, final EIRDataType updateMask);
public void deviceConnected(final BluetoothDevice device, final long timestamp);
public void deviceDisconnected(final BluetoothDevice device, final long timestamp);
};
*/
const DBTDevice * deviceMatchRef;
std::shared_ptr<JavaAnonObj> adapterObjRef;
std::unique_ptr<JNIGlobalRef> adapterSettingsClazzRef;
jmethodID adapterSettingsClazzCtor;
std::unique_ptr<JNIGlobalRef> eirDataTypeSetClazzRef;
jmethodID eirDataTypeSetClazzCtor;
std::unique_ptr<JNIGlobalRef> deviceClazzRef;
jmethodID deviceClazzCtor;
jfieldID deviceClazzTSUpdateField;
std::unique_ptr<JNIGlobalRef> listenerObjRef;
jmethodID mAdapterSettingsChanged = nullptr;
jmethodID mDeviceFound = nullptr;
jmethodID mDeviceUpdated = nullptr;
jmethodID mDeviceConnected = nullptr;
jmethodID mDeviceDisconnected = nullptr;
public:
JNIAdapterStatusListener(JNIEnv *env, DBTAdapter *adapter, jobject statusListener, const DBTDevice * deviceMatchRef) {
adapterObjRef = adapter->getJavaObject();
JavaGlobalObj::check(adapterObjRef, E_FILE_LINE);
listenerObjRef = std::unique_ptr<JNIGlobalRef>(new JNIGlobalRef(statusListener));
jclass listenerClazz = search_class(env, listenerObjRef->getObject());
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == listenerClazz ) {
throw InternalError("AdapterStatusListener not found", E_FILE_LINE);
}
this->deviceMatchRef = deviceMatchRef;
// adapterSettingsClazzRef, adapterSettingsClazzCtor
{
jclass adapterSettingsClazz = search_class(env, _adapterSettingsClassName.c_str());
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == adapterSettingsClazz ) {
throw InternalError("DBTDevice::java_class not found: "+_adapterSettingsClassName, E_FILE_LINE);
}
adapterSettingsClazzRef = std::unique_ptr<JNIGlobalRef>(new JNIGlobalRef(adapterSettingsClazz));
env->DeleteLocalRef(adapterSettingsClazz);
}
adapterSettingsClazzCtor = search_method(env, adapterSettingsClazzRef->getClass(), "<init>", _adapterSettingsClazzCtorArgs.c_str(), false);
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == adapterSettingsClazzCtor ) {
throw InternalError("AdapterSettings ctor not found: "+_adapterSettingsClassName+".<init>"+_adapterSettingsClazzCtorArgs, E_FILE_LINE);
}
// eirDataTypeSetClazzRef, eirDataTypeSetClazzCtor
{
jclass eirDataTypeSetClazz = search_class(env, _eirDataTypeSetClassName.c_str());
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == eirDataTypeSetClazz ) {
throw InternalError("DBTDevice::java_class not found: "+_eirDataTypeSetClassName, E_FILE_LINE);
}
eirDataTypeSetClazzRef = std::unique_ptr<JNIGlobalRef>(new JNIGlobalRef(eirDataTypeSetClazz));
env->DeleteLocalRef(eirDataTypeSetClazz);
}
eirDataTypeSetClazzCtor = search_method(env, eirDataTypeSetClazzRef->getClass(), "<init>", _eirDataTypeSetClazzCtorArgs.c_str(), false);
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == eirDataTypeSetClazzCtor ) {
throw InternalError("EIRDataType ctor not found: "+_eirDataTypeSetClassName+".<init>"+_eirDataTypeSetClazzCtorArgs, E_FILE_LINE);
}
// deviceClazzRef, deviceClazzCtor
{
jclass deviceClazz = search_class(env, DBTDevice::java_class().c_str());
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == deviceClazz ) {
throw InternalError("DBTDevice::java_class not found: "+DBTDevice::java_class(), E_FILE_LINE);
}
deviceClazzRef = std::unique_ptr<JNIGlobalRef>(new JNIGlobalRef(deviceClazz));
env->DeleteLocalRef(deviceClazz);
}
deviceClazzCtor = search_method(env, deviceClazzRef->getClass(), "<init>", _deviceClazzCtorArgs.c_str(), false);
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == deviceClazzCtor ) {
throw InternalError("DBTDevice::java_class ctor not found: "+DBTDevice::java_class()+".<init>"+_deviceClazzCtorArgs, E_FILE_LINE);
}
deviceClazzTSUpdateField = env->GetFieldID(deviceClazzRef->getClass(), "ts_update", "J");
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == deviceClazzTSUpdateField ) {
throw InternalError("DBTDevice::java_class field not found: "+DBTDevice::java_class()+".ts_update", E_FILE_LINE);
}
mAdapterSettingsChanged = search_method(env, listenerClazz, "adapterSettingsChanged", _adapterSettingsChangedMethodArgs.c_str(), false);
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mAdapterSettingsChanged ) {
throw InternalError("AdapterStatusListener has no adapterSettingsChanged"+_adapterSettingsChangedMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDeviceFound = search_method(env, listenerClazz, "deviceFound", _deviceStatusMethodArgs.c_str(), false);
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDeviceFound ) {
throw InternalError("AdapterStatusListener has no deviceFound"+_deviceStatusMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDeviceUpdated = search_method(env, listenerClazz, "deviceUpdated", _deviceStatusUpdateMethodArgs.c_str(), false);
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDeviceUpdated ) {
throw InternalError("AdapterStatusListener has no deviceUpdated"+_deviceStatusMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDeviceConnected = search_method(env, listenerClazz, "deviceConnected", _deviceStatusMethodArgs.c_str(), false);
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDeviceConnected ) {
throw InternalError("AdapterStatusListener has no deviceConnected"+_deviceStatusMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
mDeviceDisconnected = search_method(env, listenerClazz, "deviceDisconnected", _deviceStatusMethodArgs.c_str(), false);
java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mDeviceDisconnected ) {
throw InternalError("AdapterStatusListener has no deviceDisconnected"+_deviceStatusMethodArgs+" method, for "+adapter->toString(), E_FILE_LINE);
}
}
bool matchDevice(const DBTDevice & device) override {
if( nullptr == deviceMatchRef ) {
return true;
}
return device == *deviceMatchRef;
}
void adapterSettingsChanged(DBTAdapter const &a, const AdapterSetting oldmask, const AdapterSetting newmask,
const AdapterSetting changedmask, const uint64_t timestamp) override {
JNIEnv *env = *jni_env;
try {
#ifdef VERBOSE_ON
fprintf(stderr, "****** Native Adapter SETTINGS_CHANGED: %s -> %s, changed %s\n",
direct_bt::adapterSettingsToString(oldmask).c_str(),
direct_bt::adapterSettingsToString(newmask).c_str(),
direct_bt::adapterSettingsToString(changedmask).c_str());
fprintf(stderr, "Status DBTAdapter:\n");
fprintf(stderr, "%s\n", a.toString().c_str());
#endif
(void)a;
jobject adapterSettingOld = env->NewObject(adapterSettingsClazzRef->getClass(), adapterSettingsClazzCtor, (jint)oldmask);
java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(adapterSettingOld, E_FILE_LINE);
jobject adapterSettingNew = env->NewObject(adapterSettingsClazzRef->getClass(), adapterSettingsClazzCtor, (jint)newmask);
java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(adapterSettingNew, E_FILE_LINE);
jobject adapterSettingChanged = env->NewObject(adapterSettingsClazzRef->getClass(), adapterSettingsClazzCtor, (jint)changedmask);
java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(adapterSettingChanged, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef->getObject(), mAdapterSettingsChanged,
JavaGlobalObj::GetObject(adapterObjRef), adapterSettingOld, adapterSettingNew, adapterSettingChanged, (jlong)timestamp);
java_exception_check_and_throw(env, E_FILE_LINE);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
void deviceFound(std::shared_ptr<DBTDevice> device, const uint64_t timestamp) override {
JNIEnv *env = *jni_env;
try {
#ifdef VERBOSE_ON
fprintf(stderr, "****** Native Adapter Device FOUND__: %s\n", device->toString().c_str());
fprintf(stderr, "Status DBTAdapter:\n");
fprintf(stderr, "%s\n", device->getAdapter().toString().c_str());
#endif
jobject jdevice;
std::shared_ptr<JavaAnonObj> jDeviceRef = device->getJavaObject();
if( JavaGlobalObj::isValid(jDeviceRef) ) {
// Reuse Java instance
jdevice = JavaGlobalObj::GetObject(jDeviceRef);
} else {
// New Java instance
// Device(final long nativeInstance, final Adapter adptr, final String address, final String name)
const jstring addr = from_string_to_jstring(env, device->getAddressString());
const jstring name = from_string_to_jstring(env, device->getName());
java_exception_check_and_throw(env, E_FILE_LINE);
jobject jDevice = env->NewObject(deviceClazzRef->getClass(), deviceClazzCtor,
(jlong)device.get(), JavaGlobalObj::GetObject(adapterObjRef), addr, name, (jlong)timestamp);
java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(jDevice, E_FILE_LINE);
std::shared_ptr<JavaAnonObj> jDeviceRef = device->getJavaObject();
JavaGlobalObj::check(jDeviceRef, E_FILE_LINE);
jdevice = JavaGlobalObj::GetObject(jDeviceRef);
}
env->CallVoidMethod(listenerObjRef->getObject(), mDeviceFound, jdevice, (jlong)timestamp);
java_exception_check_and_throw(env, E_FILE_LINE);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
void deviceUpdated(std::shared_ptr<DBTDevice> device, const uint64_t timestamp, const EIRDataType updateMask) override {
JNIEnv *env = *jni_env;
try {
#ifdef VERBOSE_ON
fprintf(stderr, "****** Native Adapter Device UPDATED: %s of %s\n", direct_bt::eirDataMaskToString(updateMask).c_str(), device->toString().c_str());
fprintf(stderr, "Status DBTAdapter:\n");
fprintf(stderr, "%s\n", device->getAdapter().toString().c_str());
#endif
std::shared_ptr<JavaAnonObj> jDeviceRef = device->getJavaObject();
JavaGlobalObj::check(jDeviceRef, E_FILE_LINE);
env->SetLongField(JavaGlobalObj::GetObject(jDeviceRef), deviceClazzTSUpdateField, (jlong)timestamp);
java_exception_check_and_throw(env, E_FILE_LINE);
jobject eirDataTypeSet = env->NewObject(eirDataTypeSetClazzRef->getClass(), eirDataTypeSetClazzCtor, (jint)updateMask);
java_exception_check_and_throw(env, E_FILE_LINE);
JNIGlobalRef::check(eirDataTypeSet, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef->getObject(), mDeviceUpdated, JavaGlobalObj::GetObject(jDeviceRef), (jlong)timestamp, eirDataTypeSet);
java_exception_check_and_throw(env, E_FILE_LINE);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
void deviceConnected(std::shared_ptr<DBTDevice> device, const uint64_t timestamp) override {
JNIEnv *env = *jni_env;
try {
#ifdef VERBOSE_ON
fprintf(stderr, "****** DBTAdapter Device CONNECTED: %s\n", device->toString().c_str());
fprintf(stderr, "Status DBTAdapter:\n");
fprintf(stderr, "%s\n", device->getAdapter().toString().c_str());
#endif
std::shared_ptr<JavaAnonObj> jDeviceRef = device->getJavaObject();
JavaGlobalObj::check(jDeviceRef, E_FILE_LINE);
env->SetLongField(JavaGlobalObj::GetObject(jDeviceRef), deviceClazzTSUpdateField, (jlong)timestamp);
java_exception_check_and_throw(env, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef->getObject(), mDeviceConnected, JavaGlobalObj::GetObject(jDeviceRef), (jlong)timestamp);
java_exception_check_and_throw(env, E_FILE_LINE);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
void deviceDisconnected(std::shared_ptr<DBTDevice> device, const uint64_t timestamp) override {
JNIEnv *env = *jni_env;
try {
#ifdef VERBOSE_ON
fprintf(stderr, "****** DBTAdapter Device DISCONNECTED: %s\n", device->toString().c_str());
fprintf(stderr, "Status DBTAdapter:\n");
fprintf(stderr, "%s\n", device->getAdapter().toString().c_str());
#endif
std::shared_ptr<JavaAnonObj> jDeviceRef = device->getJavaObject();
JavaGlobalObj::check(jDeviceRef, E_FILE_LINE);
env->SetLongField(JavaGlobalObj::GetObject(jDeviceRef), deviceClazzTSUpdateField, (jlong)timestamp);
java_exception_check_and_throw(env, E_FILE_LINE);
env->CallVoidMethod(listenerObjRef->getObject(), mDeviceDisconnected, JavaGlobalObj::GetObject(jDeviceRef), (jlong)timestamp);
java_exception_check_and_throw(env, E_FILE_LINE);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
};
jboolean Java_direct_1bt_tinyb_DBTAdapter_addStatusListener(JNIEnv *env, jobject obj, jobject statusListener, jobject jdeviceMatch)
{
try {
if( nullptr == statusListener ) {
throw IllegalArgumentException("statusListener is null", E_FILE_LINE);
}
{
JNIAdapterStatusListener * pre =
getObjectRef<JNIAdapterStatusListener>(env, statusListener, "nativeInstance");
if( nullptr != pre ) {
WARN_PRINT("statusListener's nativeInstance not null, already in use");
return false;
}
}
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
DBTDevice * deviceMatchRef = nullptr;
if( nullptr != jdeviceMatch ) {
deviceMatchRef = getInstance<DBTDevice>(env, jdeviceMatch);
JavaGlobalObj::check(deviceMatchRef->getJavaObject(), E_FILE_LINE);
}
std::shared_ptr<AdapterStatusListener> l =
std::shared_ptr<AdapterStatusListener>( new JNIAdapterStatusListener(env, adapter, statusListener, deviceMatchRef) );
if( adapter->addStatusListener( l ) ) {
setInstance(env, statusListener, l.get());
return JNI_TRUE;
}
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jboolean Java_direct_1bt_tinyb_DBTAdapter_removeStatusListener(JNIEnv *env, jobject obj, jobject statusListener)
{
try {
if( nullptr == statusListener ) {
throw IllegalArgumentException("statusListener is null", E_FILE_LINE);
}
const JNIAdapterStatusListener * pre =
getObjectRef<JNIAdapterStatusListener>(env, statusListener, "nativeInstance");
if( nullptr == pre ) {
WARN_PRINT("statusListener's nativeInstance is null, not in use");
return false;
}
setObjectRef<JNIAdapterStatusListener>(env, statusListener, nullptr, "nativeInstance");
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
if( ! adapter->removeStatusListener( pre ) ) {
WARN_PRINT("Failed to remove statusListener with nativeInstance: %p at %s", pre, adapter->toString().c_str());
return false;
}
return true;
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jint Java_direct_1bt_tinyb_DBTAdapter_removeAllStatusListener(JNIEnv *env, jobject obj) {
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return adapter->removeAllStatusListener();
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return 0;
}
jstring Java_direct_1bt_tinyb_DBTAdapter_toStringImpl(JNIEnv *env, jobject obj) {
try {
DBTAdapter *nativePtr = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE);
return from_string_to_jstring(env, nativePtr->toString());
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
jboolean Java_direct_1bt_tinyb_DBTAdapter_openImpl(JNIEnv *env, jobject obj) {
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
DBG_PRINT("Java_direct_1bt_tinyb_DBTAdapter_deleteImpl %s", adapter->toString().c_str());
std::shared_ptr<direct_bt::HCIComm> hciSession = adapter->openHCI();
if( nullptr == hciSession ) {
throw BluetoothException("Couldn't open adapter "+adapter->toString(), E_FILE_LINE);
}
return JNI_TRUE;
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
void Java_direct_1bt_tinyb_DBTAdapter_deleteImpl(JNIEnv *env, jobject obj)
{
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
DBG_PRINT("Java_direct_1bt_tinyb_DBTAdapter_deleteImpl %s", adapter->toString().c_str());
delete adapter;
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
jboolean Java_direct_1bt_tinyb_DBTAdapter_startDiscoveryImpl(JNIEnv *env, jobject obj)
{
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
return adapter->startDiscovery();
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jboolean Java_direct_1bt_tinyb_DBTAdapter_stopDiscoveryImpl(JNIEnv *env, jobject obj)
{
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
adapter->stopDiscovery();
return JNI_TRUE;
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return JNI_FALSE;
}
jobject Java_direct_1bt_tinyb_DBTAdapter_getDiscoveredDevicesImpl(JNIEnv *env, jobject obj)
{
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
std::vector<std::shared_ptr<DBTDevice>> array = adapter->getDiscoveredDevices();
return convert_vector_sharedptr_to_jarraylist(env, array);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
jint Java_direct_1bt_tinyb_DBTAdapter_removeDevicesImpl(JNIEnv *env, jobject obj)
{
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
return adapter->removeDiscoveredDevices();
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return 0;
}
//
// Discovering
//
static void disableDiscoveringNotifications(JNIEnv *env, jobject obj, DBTManager &mgmt)
{
InvocationFunc<bool, std::shared_ptr<MgmtEvent>> * funcptr =
getObjectRef<InvocationFunc<bool, std::shared_ptr<MgmtEvent>>>(env, obj, "discoveringNotificationRef");
if( nullptr != funcptr ) {
FunctionDef<bool, std::shared_ptr<MgmtEvent>> funcDef( funcptr );
funcptr = nullptr;
setObjectRef(env, obj, funcptr, "discoveringNotificationRef"); // clear java ref
int count;
if( 1 != ( count = mgmt.removeMgmtEventCallback(MgmtEvent::Opcode::DISCOVERING, funcDef) ) ) {
throw direct_bt::InternalError(std::string("removeMgmtEventCallback of ")+funcDef.toString()+" not 1 but "+std::to_string(count), E_FILE_LINE);
}
}
}
void Java_direct_1bt_tinyb_DBTAdapter_disableDiscoveringNotifications(JNIEnv *env, jobject obj)
{
// org.tinyb.AdapterStatusListener
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
DBTManager & mgmt = adapter->getManager();
disableDiscoveringNotifications(env, obj, mgmt);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
void Java_direct_1bt_tinyb_DBTAdapter_enableDiscoveringNotifications(JNIEnv *env, jobject obj, jobject javaCallback)
{
// org.tinyb.AdapterStatusListener
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
DBTManager & mgmt = adapter->getManager();
disableDiscoveringNotifications(env, obj, mgmt);
bool(*nativeCallback)(JNIGlobalRef&, std::shared_ptr<MgmtEvent>) =
[](JNIGlobalRef& javaCallback_ref, std::shared_ptr<MgmtEvent> e)->bool {
const MgmtEvtDiscovering &event = *static_cast<const MgmtEvtDiscovering *>(e.get());
jclass notification = search_class(*jni_env, *javaCallback_ref);
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, event.getEnabled() ? JNI_TRUE : JNI_FALSE);
jni_env->DeleteLocalRef(boolean_cls);
jni_env->CallVoidMethod(*javaCallback_ref, method, result);
jni_env->DeleteLocalRef(result);
return true;
};
// move JNIGlobalRef into CaptureInvocationFunc and operator== includes javaCallback comparison
FunctionDef<bool, std::shared_ptr<MgmtEvent>> funcDef = bindCaptureFunc(JNIGlobalRef(javaCallback), nativeCallback);
setObjectRef(env, obj, funcDef.cloneFunction(), "discoveringNotificationRef"); // set java ref
mgmt.addMgmtEventCallback(adapter->dev_id, MgmtEvent::Opcode::DISCOVERING, funcDef);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
//
// misc
//
void Java_direct_1bt_tinyb_DBTAdapter_setPowered(JNIEnv *env, jobject obj, jboolean value) {
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
adapter->setPowered(JNI_TRUE == value ? true : false);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
jstring Java_direct_1bt_tinyb_DBTAdapter_getAlias(JNIEnv *env, jobject obj) {
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
return from_string_to_jstring(env, adapter->getLocalName().getName());
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
void Java_direct_1bt_tinyb_DBTAdapter_setAlias(JNIEnv *env, jobject obj, jstring jnewalias) {
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
std::string newalias = from_jstring_to_string(env, jnewalias);
adapter->setLocalName(newalias, std::string());
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
void Java_direct_1bt_tinyb_DBTAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean value) {
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
adapter->setDiscoverable(JNI_TRUE == value ? true : false);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
jobject Java_direct_1bt_tinyb_DBTAdapter_connectDevice(JNIEnv *env, jobject obj, jstring jaddress, jstring jaddressType) {
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
std::string saddress = from_jstring_to_string(env, jaddress);
EUI48 address(saddress);
std::shared_ptr<DBTDevice> device = adapter->findDiscoveredDevice(address);
if( nullptr != device ) {
const BDAddressType addressType = fromJavaAdressTypeToBDAddressType(env, jaddressType);
const BDAddressType addressTypeDevice = device->getAddressType();
if( addressTypeDevice != addressType ) {
// oops?
WARN_PRINT("DBTAdapter::connectDevice: AddressType mismatch, ignoring request: Requested %s, Device %s %s",
getBDAddressTypeString(addressType).c_str(), getBDAddressTypeString(addressTypeDevice).c_str(),
device->toString().c_str());
}
std::shared_ptr<JavaAnonObj> jDeviceRef = device->getJavaObject();
JavaGlobalObj::check(jDeviceRef, E_FILE_LINE);
device->connectHCIDefault();
return JavaGlobalObj::GetObject(jDeviceRef);
}
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
void Java_direct_1bt_tinyb_DBTAdapter_setPairable(JNIEnv *env, jobject obj, jboolean value) {
try {
DBTAdapter *adapter = getInstance<DBTAdapter>(env, obj);
JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE);
adapter->setBondable(JNI_TRUE == value ? true : false);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
|