+CAResult_t CALEClientStartScanWithUUIDImplForV21(JNIEnv *env, jobjectArray uuids, jobject callback)
+{
+ OIC_LOG(DEBUG, TAG, "CALEClientStartScanWithUUIDImplForV21 IN");
+ VERIFY_NON_NULL(callback, TAG, "callback is null");
+ VERIFY_NON_NULL(uuids, TAG, "uuids is null");
+ VERIFY_NON_NULL(env, TAG, "env is null");
+
+ if (!CALEIsEnableBTAdapter(env))
+ {
+ OIC_LOG(INFO, TAG, "BT adapter is not enabled");
+ return CA_ADAPTER_NOT_ENABLED;
+ }
+
+ // get bluetoothLeScanner class
+ jclass jni_cid_leScanner = (*env)->FindClass(env, CLASSPATH_LE_SCANNER);
+ if (!jni_cid_leScanner)
+ {
+ OIC_LOG(ERROR, TAG, "getState From leScanner: jni_cid_leScanner is null");
+ CACheckJNIException(env);
+ return CA_STATUS_FAILED;
+ }
+
+ // get startScan(with UUID) method
+ jmethodID jni_mid_startScan = (*env)->GetMethodID(env, jni_cid_leScanner,
+ "startScan",
+ "(Ljava/util/List;"
+ "Landroid/bluetooth/le/ScanSettings;"
+ "Landroid/bluetooth/le/ScanCallback;"
+ ")V");
+ if (!jni_mid_startScan)
+ {
+ OIC_LOG(ERROR, TAG, "startScan: jni_mid_startScan is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_cid_leScanner);
+ return CA_STATUS_FAILED;
+ }
+ (*env)->DeleteLocalRef(env, jni_cid_leScanner);
+
+ // get scanfilter.Builder class id
+ jclass jni_cid_scanfilterBuilder = (*env)->FindClass(env,
+ "android/bluetooth/le/"
+ "ScanFilter$Builder");
+ if (!jni_cid_scanfilterBuilder)
+ {
+ OIC_LOG(ERROR, TAG, "scanfilter: jni_cid_scanfilterBuilder is null");
+ CACheckJNIException(env);
+ return CA_STATUS_FAILED;
+ }
+
+ // get scanfilter.Builder(ctor) method id
+ jmethodID jni_mid_scanfilterBuilderCtor = (*env)->GetMethodID(env, jni_cid_scanfilterBuilder,
+ "<init>", "()V");
+ if (!jni_mid_scanfilterBuilderCtor)
+ {
+ OIC_LOG(ERROR, TAG, "scanfilter: jni_cid_scanfilterBuilderCtor is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_cid_scanfilterBuilder);
+ return CA_STATUS_FAILED;
+ }
+
+ // call scanfilter.Builder()
+ jobject jni_obj_scanfilterBuilder = (*env)->NewObject(env, jni_cid_scanfilterBuilder,
+ jni_mid_scanfilterBuilderCtor);
+ if (!jni_obj_scanfilterBuilder)
+ {
+ OIC_LOG(ERROR, TAG, "scanfilter: jni_obj_scanfilterBuilder is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_cid_scanfilterBuilder);
+ return CA_STATUS_FAILED;
+ }
+
+ // get scanfilter.Builder.setServiceUuid method id
+ jmethodID jni_mid_setServiceUuid = (*env)->GetMethodID(env, jni_cid_scanfilterBuilder,
+ "setServiceUuid",
+ "(Landroid/os/ParcelUuid;)Landroid/"
+ "bluetooth/le/ScanFilter$Builder;");
+ if (!jni_mid_setServiceUuid)
+ {
+ OIC_LOG(ERROR, TAG, "scanfilter: jni_mid_setServiceUuid is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_cid_scanfilterBuilder);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilterBuilder);
+ return CA_STATUS_FAILED;
+ }
+
+ // get scanfilter.Builder.build method id
+ jmethodID jni_mid_build_scanfilterBuilder = (*env)->GetMethodID(env,
+ jni_cid_scanfilterBuilder,
+ "build",
+ "()Landroid/bluetooth/le/"
+ "ScanFilter;");
+ if (!jni_mid_build_scanfilterBuilder)
+ {
+ OIC_LOG(ERROR, TAG, "scanfilter: jni_mid_build_scanfilterBuilder is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_cid_scanfilterBuilder);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilterBuilder);
+ return CA_STATUS_FAILED;
+ }
+ (*env)->DeleteLocalRef(env, jni_cid_scanfilterBuilder);
+
+ // call ParcelUuid.fromSting(uuid)
+ jobject jni_obj_parcelUuid = CALEGetParcelUuidFromString(env, OIC_GATT_SERVICE_UUID);
+ if (!jni_obj_parcelUuid)
+ {
+ OIC_LOG(ERROR, TAG, "scanSettings: jni_obj_parcelUuid is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilterBuilder);
+ return CA_STATUS_FAILED;
+ }
+
+ // call setServiceUuid(uuid)
+ jobject jni_obj_setServiceUuid = (*env)->CallObjectMethod(env,
+ jni_obj_scanfilterBuilder,
+ jni_mid_setServiceUuid,
+ jni_obj_parcelUuid);
+ if (!jni_obj_setServiceUuid)
+ {
+ OIC_LOG(ERROR, TAG, "scanfilter: jni_obj_setServiceUuid is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilterBuilder);
+ (*env)->DeleteLocalRef(env, jni_obj_parcelUuid);
+ return CA_STATUS_FAILED;
+ }
+ (*env)->DeleteLocalRef(env, jni_obj_parcelUuid);
+ (*env)->DeleteLocalRef(env, jni_obj_setServiceUuid);
+
+ // call build()
+ jobject jni_obj_scanfilter = (*env)->CallObjectMethod(env,
+ jni_obj_scanfilterBuilder,
+ jni_mid_build_scanfilterBuilder);
+ if (!jni_obj_scanfilter)
+ {
+ OIC_LOG(ERROR, TAG, "scanfilter: jni_obj_scanfilter is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilterBuilder);
+ return CA_STATUS_FAILED;
+ }
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilterBuilder);
+
+ // get scanSettings.Builder class id
+ jclass jni_cid_scanSettingsBuilder = (*env)->FindClass(env,
+ "android/bluetooth/le/"
+ "ScanSettings$Builder");
+ if (!jni_cid_scanSettingsBuilder)
+ {
+ OIC_LOG(ERROR, TAG, "scanSettings: jni_cid_scanSettingsBuilder is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+ return CA_STATUS_FAILED;
+ }
+
+ // get scanSettings.Builder(ctor) method id
+ jmethodID jni_mid_scanSettingsBuilderCtor = (*env)->GetMethodID(env, jni_cid_scanSettingsBuilder,
+ "<init>", "()V");
+ if (!jni_mid_scanSettingsBuilderCtor)
+ {
+ OIC_LOG(ERROR, TAG, "scanSettings: jni_mid_scanSettingsBuilderCtor is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+ (*env)->DeleteLocalRef(env, jni_cid_scanSettingsBuilder);
+ return CA_STATUS_FAILED;
+ }
+
+ // get scanSettings.Builder.setScanMode method id
+ jmethodID jni_mid_setScanMode = (*env)->GetMethodID(env, jni_cid_scanSettingsBuilder,
+ "setScanMode",
+ "(I)Landroid/"
+ "bluetooth/le/ScanSettings$Builder;");
+ if (!jni_mid_setScanMode)
+ {
+ OIC_LOG(ERROR, TAG, "scanSettings: jni_mid_setScanMode is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+ (*env)->DeleteLocalRef(env, jni_cid_scanSettingsBuilder);
+ return CA_STATUS_FAILED;
+ }
+
+ // get scanSettings.Builder.build method id
+ jmethodID jni_mid_build_scanSettings = (*env)->GetMethodID(env,
+ jni_cid_scanSettingsBuilder,
+ "build",
+ "()Landroid/bluetooth/le/"
+ "ScanSettings;");
+ if (!jni_mid_build_scanSettings)
+ {
+ OIC_LOG(ERROR, TAG, "scanSettings: jni_mid_build_scanSettings is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+ (*env)->DeleteLocalRef(env, jni_cid_scanSettingsBuilder);
+ return CA_STATUS_FAILED;
+ }
+
+ // call scanSettings.Builder()
+ jobject jni_obj_scanSettingBuilder = (*env)->NewObject(env, jni_cid_scanSettingsBuilder,
+ jni_mid_scanSettingsBuilderCtor);
+ if (!jni_obj_scanSettingBuilder)
+ {
+ OIC_LOG(ERROR, TAG, "scanfilter: jni_obj_scanSettingBuilder is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+ (*env)->DeleteLocalRef(env, jni_cid_scanSettingsBuilder);
+ return CA_STATUS_FAILED;
+ }
+ (*env)->DeleteLocalRef(env, jni_cid_scanSettingsBuilder);
+
+ jclass jni_cid_arrayList = (*env)->FindClass(env, "java/util/ArrayList");
+ if (!jni_cid_arrayList)
+ {
+ OIC_LOG(ERROR, TAG, "ArrayList: jni_cid_arrayList is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+ (*env)->DeleteLocalRef(env, jni_obj_scanSettingBuilder);
+ return CA_STATUS_FAILED;
+ }
+
+ jmethodID jni_mid_arrayListCtor = (*env)->GetMethodID(env, jni_cid_arrayList, "<init>", "()V");
+ if (!jni_mid_arrayListCtor)
+ {
+ OIC_LOG(ERROR, TAG, "ArrayList: jni_mid_arrayListCtor is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+ (*env)->DeleteLocalRef(env, jni_cid_arrayList);
+ (*env)->DeleteLocalRef(env, jni_obj_scanSettingBuilder);
+ return CA_STATUS_FAILED;
+ }
+
+ jmethodID jni_mid_arrayListAdd = (*env)->GetMethodID(env, jni_cid_arrayList,
+ "add", "(Ljava/lang/Object;)Z");
+ if (!jni_mid_arrayListAdd)
+ {
+ OIC_LOG(ERROR, TAG, "ArrayList: jni_mid_arrayListAdd is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+ (*env)->DeleteLocalRef(env, jni_cid_arrayList);
+ (*env)->DeleteLocalRef(env, jni_obj_scanSettingBuilder);
+ return CA_STATUS_FAILED;
+ }
+
+ jobject jni_obj_filterList = (*env)->NewObject(env, jni_cid_arrayList, jni_mid_arrayListCtor);
+ if (!jni_obj_filterList)
+ {
+ OIC_LOG(ERROR, TAG, "ArrayList: jni_obj_filterList is null");
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+ (*env)->DeleteLocalRef(env, jni_cid_arrayList);
+ (*env)->DeleteLocalRef(env, jni_obj_scanSettingBuilder);
+ return CA_STATUS_FAILED;
+ }
+ (*env)->DeleteLocalRef(env, jni_cid_arrayList);
+
+ jboolean jni_bool_arrayListIsAdded = (*env)->CallBooleanMethod(env, jni_obj_filterList,
+ jni_mid_arrayListAdd,
+ jni_obj_scanfilter);
+ if (!jni_bool_arrayListIsAdded)
+ {
+ OIC_LOG(ERROR, TAG, "ArrayList: jni_bool_arrayListIsAdded is null");
+ (*env)->DeleteLocalRef(env, jni_obj_filterList);
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+ (*env)->DeleteLocalRef(env, jni_obj_scanSettingBuilder);
+ return CA_STATUS_FAILED;
+ }
+ (*env)->DeleteLocalRef(env, jni_obj_scanfilter);
+
+ // get ScanSettings.SCAN_MODE_BALANCED jint value
+ jint jni_int_scanBalancedMode = CALEGetConstantsValue(env, CLASSPATH_LE_SCANSETTINGS,
+ "SCAN_MODE_BALANCED");
+ CACheckJNIException(env);
+
+ // call setScanMode(SCAN_MODE_BALANCED)
+ jobject jni_obj_setScanMode = (*env)->CallObjectMethod(env, jni_obj_scanSettingBuilder,
+ jni_mid_setScanMode,
+ jni_int_scanBalancedMode);
+ if (!jni_obj_setScanMode)
+ {
+ OIC_LOG(ERROR, TAG, "scanfilter: jni_obj_setScanMode is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_obj_scanSettingBuilder);
+ (*env)->DeleteLocalRef(env, jni_obj_filterList);
+ return CA_STATUS_FAILED;
+ }
+
+ // call build
+ jobject jni_obj_scanSettings = (*env)->CallObjectMethod(env, jni_obj_scanSettingBuilder,
+ jni_mid_build_scanSettings);
+ if (!jni_obj_scanSettings)
+ {
+ OIC_LOG(ERROR, TAG, "scanfilter: jni_obj_scanSettings is null");
+ (*env)->DeleteLocalRef(env, jni_obj_scanSettingBuilder);
+ (*env)->DeleteLocalRef(env, jni_obj_filterList);
+ return CA_STATUS_FAILED;
+ }
+ (*env)->DeleteLocalRef(env, jni_obj_scanSettingBuilder);
+
+ CAResult_t res = CA_STATUS_FAILED;
+ // get default bt adapter class
+ jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
+ if (!jni_cid_BTAdapter)
+ {
+ OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
+ CACheckJNIException(env);
+ goto error_exit;
+ }
+
+ jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
+ "getDefaultAdapter",
+ "()Landroid/bluetooth/"
+ "BluetoothAdapter;");
+ if (!jni_mid_getDefaultAdapter)
+ {
+ OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+ goto error_exit;
+ }
+
+ jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
+ jni_mid_getDefaultAdapter);
+ if (!jni_obj_BTAdapter)
+ {
+ OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+ goto error_exit;
+ }
+
+ // get remote bt adapter method
+ jmethodID jni_mid_getBluetoothLeScanner = (*env)->GetMethodID(env, jni_cid_BTAdapter,
+ "getBluetoothLeScanner",
+ "()Landroid/bluetooth/"
+ "le/BluetoothLeScanner;");
+ if (!jni_mid_getBluetoothLeScanner)
+ {
+ OIC_LOG(ERROR, TAG, "jni_mid_getBluetoothLeScanner is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+ (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+ goto error_exit;
+ }
+
+ // get le scanner object
+ jobject jni_obj_leScanner = (*env)->CallObjectMethod(env, jni_obj_BTAdapter,
+ jni_mid_getBluetoothLeScanner);
+ if (!jni_obj_leScanner)
+ {
+ OIC_LOG(ERROR, TAG, "jni_obj_leScanner is null");
+ CACheckJNIException(env);
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+ (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+ goto error_exit;
+ }
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+ (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+
+ // call startScan method
+ OIC_LOG(INFO, TAG, "CALL API - startScanWithUUID(for level 21)");
+ (*env)->CallVoidMethod(env, jni_obj_leScanner, jni_mid_startScan, jni_obj_filterList,
+ jni_obj_scanSettings, callback);
+ if (CACheckJNIException(env))
+ {
+ OIC_LOG(INFO, TAG, "startScan has failed");
+ }
+ else
+ {
+ res = CA_STATUS_OK;
+ }
+ (*env)->DeleteLocalRef(env, jni_obj_leScanner);
+
+error_exit:
+ (*env)->DeleteLocalRef(env, jni_obj_scanSettings);
+ (*env)->DeleteLocalRef(env, jni_obj_filterList);
+ return res;
+}
+