@@ -40,30 +40,91 @@ jobject Java_tinyb_BluetoothManager_getBluetoothType(JNIEnv *env, jobject obj)
4040 return get_bluetooth_type (env, " NONE" );
4141}
4242
43- jobject Java_tinyb_BluetoothManager_getObject (JNIEnv *env, jobject obj, jobject type,
44- jstring name, jstring identifier, jobject parent)
43+ static void getObject_setter (JNIEnv *env,
44+ jstring name, std::string **name_to_write,
45+ jstring identifier, std::string **identifier_to_write,
46+ jobject parent, BluetoothObject **b_parent)
4547{
46- (void )env;
47- (void )obj;
48- (void )type;
49- (void )name;
50- (void )identifier;
51- (void )parent;
52-
53- return nullptr ;
48+ if (!parent)
49+ {
50+ *b_parent = nullptr ;
51+ }
52+ else
53+ {
54+ *b_parent = getInstance<BluetoothObject>(env, parent);
55+ }
56+
57+ if (!name)
58+ {
59+ *name_to_write = nullptr ;
60+ }
61+ else
62+ {
63+ **name_to_write = from_jstring_to_string (env, name);
64+ }
65+
66+ if (!identifier)
67+ {
68+ *identifier_to_write = nullptr ;
69+ }
70+ else
71+ {
72+ **identifier_to_write = from_jstring_to_string (env, identifier);
73+ }
5474}
5575
56- jobject Java_tinyb_BluetoothManager_getObjects (JNIEnv *env, jobject obj, jobject type,
76+ jobject Java_tinyb_BluetoothManager_getObject (JNIEnv *env, jobject obj, jint type,
5777 jstring name, jstring identifier, jobject parent)
5878{
59- (void )env;
60- (void )obj;
61- (void )type;
62- (void )name;
63- (void )identifier;
64- (void )parent;
79+ BluetoothManager *manager = getInstance<BluetoothManager>(env, obj);
80+ BluetoothObject *b_parent;
81+ BluetoothType b_type;
82+ std::string *name_to_write;
83+ std::string *identifier_to_write;
84+
85+ getObject_setter (env,
86+ name, &name_to_write,
87+ identifier, &identifier_to_write,
88+ parent, &b_parent);
89+
90+ b_type = from_int_to_btype ((int )type);
91+ std::unique_ptr<BluetoothObject> b_object = manager->get_object (b_type, name_to_write,
92+ identifier_to_write,
93+ b_parent);
94+
95+ BluetoothObject *b_object_naked = b_object.release ();
96+ if (!b_object_naked)
97+ {
98+ return nullptr ;
99+ }
100+ jclass clazz = search_class (env, *b_object_naked);
101+ jmethodID clazz_ctor = search_method (env, clazz, " <init>" , " (J)V" , false );
102+
103+ jobject result = env->NewObject (clazz, clazz_ctor, (long )b_object_naked);
104+ return result;
105+ }
65106
66- return nullptr ;
107+ jobject Java_tinyb_BluetoothManager_getObjects (JNIEnv *env, jobject obj, jint type,
108+ jstring name, jstring identifier, jobject parent)
109+ {
110+ BluetoothManager *manager = getInstance<BluetoothManager>(env, obj);
111+ BluetoothObject *b_parent;
112+ BluetoothType b_type;
113+ std::string *name_to_write;
114+ std::string *identifier_to_write;
115+
116+ getObject_setter (env,
117+ name, &name_to_write,
118+ identifier, &identifier_to_write,
119+ parent, &b_parent);
120+
121+ b_type = from_int_to_btype ((int )type);
122+ std::vector<std::unique_ptr<BluetoothObject>> array = manager->get_objects (b_type,
123+ name_to_write,
124+ identifier_to_write,
125+ b_parent);
126+ jobject result = convert_vector_to_jobject<BluetoothObject>(env, array, " (J)V" );
127+ return result;
67128}
68129
69130jobject Java_tinyb_BluetoothManager_getAdapters (JNIEnv *env, jobject obj)
@@ -99,6 +160,10 @@ jobject Java_tinyb_BluetoothManager_getServices(JNIEnv *env, jobject obj)
99160
100161jboolean Java_tinyb_BluetoothManager_setDefaultAdapter (JNIEnv *env, jobject obj, jobject adapter)
101162{
163+ if (!adapter)
164+ {
165+ throw std::invalid_argument (" adapter argument is null\n " );
166+ }
102167 BluetoothManager *manager = getInstance<BluetoothManager>(env, obj);
103168 BluetoothAdapter *b_adapter = getInstance<BluetoothAdapter>(env, adapter);
104169
0 commit comments