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>
24 #include "caedrutils.h"
26 #include "oic_malloc.h"
27 #include "cathreadpool.h"
28 #include "uarraylist.h"
30 #define ERROR_CODE (-1)
31 #define TAG PCF("CA_EDR_UTILS")
33 static const char *METHODID_OBJECTNONPARAM = "()Landroid/bluetooth/BluetoothAdapter;";
34 static const char *METHODID_STRINGNONPARAM = "()Ljava/lang/String;";
35 static const char *CLASSPATH_BT_ADPATER = "android/bluetooth/BluetoothAdapter";
41 // get address from bluetooth socket
42 jstring CAEDRNativeGetAddressFromDeviceSocket(JNIEnv *env, jobject bluetoothSocketObj)
44 if(!bluetoothSocketObj)
49 jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
52 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_cid_BTSocket is null");
56 jmethodID jni_mid_getRemoteDevice = (*env)->GetMethodID(env,
57 jni_cid_BTSocket, "getRemoteDevice",
58 "()Landroid/bluetooth/BluetoothDevice;");
59 if(!jni_mid_getRemoteDevice)
61 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_mid_getRemoteDevice is null");
65 jobject jni_obj_remoteBTDevice = (*env)->CallObjectMethod(env,
66 bluetoothSocketObj, jni_mid_getRemoteDevice);
67 if(!jni_obj_remoteBTDevice)
69 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_obj_remoteBTDevice is null");
73 jclass jni_cid_BTDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
76 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: jni_cid_BTDevice is null");
79 jmethodID j_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTDevice,
80 "getAddress", "()Ljava/lang/String;");
83 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: j_mid_getAddress is null");
87 jstring j_str_address = (*env)->CallObjectMethod(env, jni_obj_remoteBTDevice, j_mid_getAddress);
90 OIC_LOG(ERROR, TAG, "[EDR] getRemoteAddress: j_str_address is null");
97 jstring CAEDRNativeGetLocalDeviceAddress(JNIEnv* env)
99 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
100 if(!jni_cid_BTAdapter)
102 OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_cid_BTAdapter is null");
106 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env,
107 jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
108 if(!jni_mid_getDefaultAdapter)
110 OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_mid_getDefaultAdapter is null");
114 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_BTAdapter,
115 "getAddress", METHODID_STRINGNONPARAM);
116 if(!jni_mid_getAddress)
118 OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_mid_getAddress is null");
122 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env,
123 jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
124 if(!jni_obj_BTAdapter)
126 OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_obj_BTAdapter is null");
130 jstring jni_str_address = (jstring)(*env)->CallObjectMethod(env,
131 jni_obj_BTAdapter, jni_mid_getAddress);
134 OIC_LOG(ERROR, TAG, "[EDR][Native] getAddress: jni_str_address is null");
138 return jni_str_address;
141 jobjectArray CAEDRNativeGetBondedDevices(JNIEnv *env)
143 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
144 if(!jni_cid_BTAdapter)
146 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_cid_BTAdapter is null");
150 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env,
151 jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
153 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env,
154 jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
155 if(!jni_obj_BTAdapter)
157 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: bluetooth adapter is null");
161 // Get a list of currently paired devices
162 jmethodID jni_mid_getBondedDevices = (*env)->GetMethodID(env, jni_cid_BTAdapter,
163 "getBondedDevices", "()Ljava/util/Set;");
164 if(!jni_mid_getBondedDevices)
166 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_mid_getBondedDevicesr is null");
170 jobject jni_obj_setPairedDevices = (*env)->CallObjectMethod(env,
171 jni_obj_BTAdapter, jni_mid_getBondedDevices);
172 if(!jni_obj_setPairedDevices)
174 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_obj_setPairedDevices is null");
178 // Convert the set to an object array
179 // object[] array = Set<BluetoothDevice>.toArray();
180 jclass jni_cid_Set = (*env)->FindClass(env, "java/util/Set");
181 jmethodID jni_mid_toArray = (*env)->GetMethodID(env, jni_cid_Set, "toArray",
182 "()[Ljava/lang/Object;");
186 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_mid_toArray is null");
190 jobjectArray jni_arrayPairedDevices = (jobjectArray)((*env)->CallObjectMethod(env,
191 jni_obj_setPairedDevices, jni_mid_toArray));
192 if(!jni_arrayPairedDevices)
194 OIC_LOG(ERROR, TAG, "[EDR][Native] getBondedDevices: jni_arrayPairedDevices is null");
198 return jni_arrayPairedDevices;
201 jint CAEDRNativeGetBTStateOnInfo(JNIEnv *env)
203 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
204 if(!jni_cid_BTAdapter)
206 OIC_LOG(ERROR, TAG, "[EDR][Native] getBTStateOnInfo: jni_cid_BTAdapter is null");
210 jfieldID jni_fid_stateon = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
211 if (jni_fid_stateon == 0)
213 OIC_LOG(DEBUG, TAG, "[EDR][Native] get_field_state is 0");
216 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, jni_fid_stateon);
218 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] bluetooth STATE_ON state integer value : %d", jni_int_val);
223 jboolean CAEDRNativeIsEnableBTAdapter(JNIEnv *env)
225 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
226 if(!jni_cid_BTAdapter)
228 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTAdapter: jni_cid_BTAdapter is null");
232 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env,
233 jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
234 if(!jni_mid_getDefaultAdapter)
236 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_getDefaultAdapter is null");
240 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env,
241 jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
242 if(!jni_obj_BTAdapter)
244 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_obj_BTAdapter is null");
249 jmethodID jni_mid_isEnable = (*env)->GetMethodID(env, jni_cid_BTAdapter, "isEnabled",
251 if(!jni_mid_isEnable)
253 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_isEnable is null");
257 jboolean jni_isEnable = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter, jni_mid_isEnable);
258 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] adapter state is %d", jni_isEnable);
263 jstring CAEDRNativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
265 jclass jni_cid_device_list = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
266 if(!jni_cid_device_list)
268 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_device_list is null");
272 jmethodID jni_mid_getAddress = (*env)->GetMethodID(env, jni_cid_device_list, "getAddress",
273 "()Ljava/lang/String;");
274 if(!jni_mid_getAddress)
276 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_getAddress is null");
280 jstring jni_address = (jstring)(*env)->CallObjectMethod(env,
281 bluetoothDevice, jni_mid_getAddress);
284 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_address is null");