1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
23 #include <android/log.h>
25 #include "caleutils.h"
27 #include "oic_malloc.h"
28 #include "cathreadpool.h"
29 #include "uarraylist.h"
30 #include "caadapterutils.h"
32 #define TAG PCF("CA_LE_UTILS")
34 #define METHODID_OBJECTNONPARAM "()Landroid/bluetooth/BluetoothAdapter;"
35 #define METHODID_STRINGNONPARAM "()Ljava/lang/String;"
36 #define CLASSPATH_BT_ADPATER "android/bluetooth/BluetoothAdapter"
38 jobject CALEGetUuidFromString(JNIEnv *env, const char* uuid)
40 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
41 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
43 OIC_LOG(DEBUG, TAG, "CALEGetUuidFromString");
45 jclass jni_cid_UUID = (*env)->FindClass(env, "java/util/UUID");
48 OIC_LOG(ERROR, TAG, "jni_cid_UUID is not available");
52 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_UUID, "fromString",
53 "(Ljava/lang/String;)"
55 if (!jni_mid_fromString)
57 OIC_LOG(ERROR, TAG, "jni_mid_fromString is not available");
61 jstring str_uuid = (*env)->NewStringUTF(env, uuid);
64 OIC_LOG(ERROR, TAG, "str_uuid is not available");
68 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_UUID, jni_mid_fromString,
72 OIC_LOG(ERROR, TAG, "Fail to get jni uuid object");
79 jobject CALEGetParcelUuid(JNIEnv *env, jobject uuid)
81 OIC_LOG(DEBUG, TAG, "CALEGetParcelUuid");
82 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
83 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
85 jclass jni_cid_ParcelUuid = (*env)->FindClass(env, "android/os/ParcelUuid");
86 if (!jni_cid_ParcelUuid)
88 OIC_LOG(ERROR, TAG, "jni_cid_ParcelUuid is not available");
92 jmethodID jni_mid_ParcelUuid = (*env)->GetMethodID(env, jni_cid_ParcelUuid, "<init>",
93 "(Ljava/util/UUID;)V");
94 if (!jni_mid_ParcelUuid)
96 OIC_LOG(ERROR, TAG, "jni_mid_ParcelUuid is not available");
100 jobject jni_ParcelUuid = (*env)->NewObject(env, jni_cid_ParcelUuid, jni_mid_ParcelUuid, uuid);
103 OIC_LOG(ERROR, TAG, "Fail to get jni ParcelUuid");
107 return jni_ParcelUuid;
110 bool CALEIsBondedDevice(JNIEnv *env, jobject bluetoothDevice)
112 VERIFY_NON_NULL_RET(env, TAG, "env is null", false);
113 VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", false);
115 jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
116 if (!jni_cid_device_list)
118 OIC_LOG(ERROR, TAG, "jni_cid_device_list is null");
122 jmethodID jni_mid_getBondState = (*env)->GetMethodID(env, jni_cid_device_list, "getBondState",
124 if (!jni_mid_getBondState)
126 OIC_LOG(ERROR, TAG, "jni_mid_getBondState is null");
130 jint jni_bondState = (jint)(*env)->CallIntMethod(env, bluetoothDevice, jni_mid_getBondState);
132 OIC_LOG_V(DEBUG, TAG, "bond state is %d", jni_bondState);
134 if (BOND_BONDED == jni_bondState)
136 OIC_LOG(DEBUG, TAG, "remote device is bonded");
141 OIC_LOG(DEBUG, TAG, "remote device is not bonded");
148 jobjectArray CALEGetBondedDevices(JNIEnv *env)
150 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
152 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
153 if (!jni_cid_BTAdapter)
155 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_cid_BTAdapter is null");
159 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
161 METHODID_OBJECTNONPARAM);
163 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
164 jni_mid_getDefaultAdapter);
165 if (!jni_obj_BTAdapter)
167 OIC_LOG(ERROR, TAG, "getBondedDevices: bluetooth adapter is null");
171 // Get a list of currently paired devices
172 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
174 "()Ljava/util/Set;");
175 if (!jni_mid_getBondedDevices)
177 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_getBondedDevicesr is null");
181 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
182 jni_mid_getBondedDevices);
183 if (!jni_obj_setPairedDevices)
185 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_obj_setPairedDevices is null");
189 jclass jni_cid_Set = (*env)->FindClass(env, "java/util/Set");
192 OIC_LOG(ERROR, TAG, "getBondedDevices : jni_cid_Set is null");
196 jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray",
197 "()[Ljava/lang/Object;");
198 if (!jni_mid_toArray)
200 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_mid_toArray is null");
204 jobjectArray jni_arrayPairedDevices = (jobjectArray)(
205 (*env)->CallObjectMethod(env, jni_obj_setPairedDevices, jni_mid_toArray));
206 if (!jni_arrayPairedDevices)
208 OIC_LOG(ERROR, TAG, "getBondedDevices: jni_arrayPairedDevices is null");
212 return jni_arrayPairedDevices;
215 jint CALEGetBTStateOnInfo(JNIEnv *env)
217 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
219 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
220 if (!jni_cid_BTAdapter)
222 OIC_LOG(ERROR, TAG, "getBTStateOnInfo: jni_cid_BTAdapter is null");
226 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
227 if (!jni_fid_stateon)
229 OIC_LOG(ERROR, TAG, "get_field_state is not available");
233 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
234 OIC_LOG_V(DEBUG, TAG, "bluetooth.STATE_ON state integer value : %d", jni_int_val);
239 CAResult_t CALECheckPlatformVersion(JNIEnv *env, uint16_t level)
241 jint jni_int_sdk = CALEGetBuildVersion(env);
242 if (jni_int_sdk < level)
244 OIC_LOG(ERROR, TAG, "it is not supported");
245 return CA_NOT_SUPPORTED;
251 jint CALEGetBuildVersion(JNIEnv *env)
253 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
255 // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
256 jclass jni_cls_version = (*env)->FindClass(env, "android/os/Build$VERSION");
257 if (!jni_cls_version)
259 OIC_LOG(ERROR, TAG, "jni_cls_version is null");
263 jfieldID jni_fid_sdk = (*env)->GetStaticFieldID(env, jni_cls_version, "SDK_INT", "I");
266 OIC_LOG(ERROR, TAG, "jni_fid_sdk is null");
270 jint jni_int_sdk = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_sdk);
271 OIC_LOG_V(DEBUG, TAG, "sdk version is %d", jni_int_sdk);
276 jint CALEGetBuildVersionCodeForName(JNIEnv *env, const char* versionName)
278 VERIFY_NON_NULL_RET(env, TAG, "env is null", -1);
279 VERIFY_NON_NULL_RET(versionName, TAG, "versionName is null", -1);
281 // VERSION is a nested class within android.os.Build (hence "$" rather than "/")
282 jclass jni_cls_version = (*env)->FindClass(env, "android/os/Build$VERSION_CODES");
283 if (!jni_cls_version)
285 OIC_LOG(ERROR, TAG, "jni_cls_version is null");
289 jfieldID jni_fid_version = (*env)->GetStaticFieldID(env, jni_cls_version, versionName, "I");
290 if (!jni_fid_version)
292 OIC_LOG(ERROR, TAG, "jni_fid_version is null");
296 jint jni_int_version = (*env)->GetStaticIntField(env, jni_cls_version, jni_fid_version);
297 OIC_LOG_V(DEBUG, TAG, "version [%s] is %d",versionName, jni_int_version);
299 return jni_int_version;
302 jboolean CALEIsEnableBTAdapter(JNIEnv *env)
304 VERIFY_NON_NULL_RET(env, TAG, "env is null", JNI_FALSE);
306 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
307 if (!jni_cid_BTAdapter)
309 OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter: jni_cid_BTAdapter is null");
313 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
315 METHODID_OBJECTNONPARAM);
316 if (!jni_mid_getDefaultAdapter)
318 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
322 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
323 jni_mid_getDefaultAdapter);
324 if (!jni_obj_BTAdapter)
326 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
331 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled", "()Z");
332 if (!jni_mid_isEnable)
334 OIC_LOG(ERROR, TAG, "jni_mid_isEnable is null");
338 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
339 OIC_LOG_V(DEBUG, TAG, "adapter state is %d", jni_isEnable);
344 jstring CALEGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
346 OIC_LOG(DEBUG, TAG, "IN - CALEGetAddressFromBTDevice");
347 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
348 VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", NULL);
350 jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
351 if (!jni_cid_device_list)
353 OIC_LOG(ERROR, TAG, "jni_cid_device_list is null");
357 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
358 "()Ljava/lang/String;");
359 if (!jni_mid_getAddress)
361 OIC_LOG(ERROR, TAG, "jni_mid_getAddress is null");
365 jstring jni_address = (jstring)(*env)->CallObjectMethod(env, bluetoothDevice,
369 OIC_LOG(ERROR, TAG, "jni_address is null");
373 OIC_LOG(DEBUG, TAG, "OUT - CALEGetAddressFromBTDevice");