Gaurav Asati | 27604c1 | 2013-08-19 18:54:29 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Linux Foundation. All rights reserved |
| 3 | * Not a Contribution. |
| 4 | * Copyright (C) 2012 The Android Open Source Project |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #define LOG_TAG "BluetoothHandsfreeClientServiceJni" |
| 20 | #define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include "com_android_bluetooth.h" |
| 23 | #include "hardware/bt_hf_client.h" |
| 24 | #include "utils/Log.h" |
| 25 | #include "android_runtime/AndroidRuntime.h" |
| 26 | |
| 27 | #define CHECK_CALLBACK_ENV \ |
| 28 | if (!checkCallbackThread()) { \ |
| 29 | ALOGE("Callback: '%s' is not called on the correct thread", __FUNCTION__);\ |
| 30 | return; \ |
| 31 | } |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | static bthf_client_interface_t *sBluetoothHfpClientInterface = NULL; |
| 36 | static jobject mCallbacksObj = NULL; |
| 37 | static JNIEnv *sCallbackEnv = NULL; |
| 38 | |
| 39 | static jmethodID method_onConnectionStateChanged; |
| 40 | static jmethodID method_onAudioStateChanged; |
| 41 | static jmethodID method_onVrStateChanged; |
| 42 | static jmethodID method_onNetworkState; |
| 43 | static jmethodID method_onNetworkRoaming; |
| 44 | static jmethodID method_onNetworkSignal; |
| 45 | static jmethodID method_onBatteryLevel; |
| 46 | static jmethodID method_onCurrentOperator; |
| 47 | static jmethodID method_onCall; |
| 48 | static jmethodID method_onCallSetup; |
| 49 | static jmethodID method_onCallHeld; |
| 50 | static jmethodID method_onRespAndHold; |
| 51 | static jmethodID method_onClip; |
| 52 | static jmethodID method_onCallWaiting; |
| 53 | static jmethodID method_onCurrentCalls; |
| 54 | static jmethodID method_onVolumeChange; |
| 55 | static jmethodID method_onCmdResult; |
| 56 | static jmethodID method_onSubscriberInfo; |
| 57 | static jmethodID method_onInBandRing; |
| 58 | static jmethodID method_onLastVoiceTagNumber; |
Gaurav Asati | 012e866 | 2013-12-26 14:30:25 +0530 | [diff] [blame] | 59 | static jmethodID method_onRingIndication; |
Gaurav Asati | 27604c1 | 2013-08-19 18:54:29 +0530 | [diff] [blame] | 60 | |
| 61 | static bool checkCallbackThread() { |
| 62 | // Always fetch the latest callbackEnv from AdapterService. |
| 63 | // Caching this could cause this sCallbackEnv to go out-of-sync |
| 64 | // with the AdapterService's ENV if an ASSOCIATE/DISASSOCIATE event |
| 65 | // is received |
| 66 | sCallbackEnv = getCallbackEnv(); |
| 67 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 68 | if (sCallbackEnv != env || sCallbackEnv == NULL) return false; |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | static void connection_state_cb(bthf_client_connection_state_t state, unsigned int peer_feat, unsigned int chld_feat, bt_bdaddr_t *bd_addr) { |
| 73 | jbyteArray addr; |
| 74 | |
| 75 | CHECK_CALLBACK_ENV |
| 76 | |
| 77 | addr = sCallbackEnv->NewByteArray(sizeof(bt_bdaddr_t)); |
| 78 | if (!addr) { |
| 79 | ALOGE("Fail to new jbyteArray bd addr for connection state"); |
| 80 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte*) bd_addr); |
| 85 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onConnectionStateChanged, (jint) state, (jint) peer_feat, (jint) chld_feat, addr); |
| 86 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 87 | sCallbackEnv->DeleteLocalRef(addr); |
| 88 | } |
| 89 | |
| 90 | static void audio_state_cb(bthf_client_audio_state_t state, bt_bdaddr_t *bd_addr) { |
| 91 | jbyteArray addr; |
| 92 | |
| 93 | CHECK_CALLBACK_ENV |
| 94 | |
| 95 | addr = sCallbackEnv->NewByteArray(sizeof(bt_bdaddr_t)); |
| 96 | if (!addr) { |
| 97 | ALOGE("Fail to new jbyteArray bd addr for audio state"); |
| 98 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte *) bd_addr); |
| 103 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onAudioStateChanged, (jint) state, addr); |
| 104 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 105 | sCallbackEnv->DeleteLocalRef(addr); |
| 106 | } |
| 107 | |
| 108 | static void vr_cmd_cb(bthf_client_vr_state_t state) { |
| 109 | CHECK_CALLBACK_ENV |
| 110 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onVrStateChanged, (jint) state); |
| 111 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 112 | } |
| 113 | |
| 114 | static void network_state_cb (bthf_client_network_state_t state) { |
| 115 | CHECK_CALLBACK_ENV |
| 116 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onNetworkState, (jint) state); |
| 117 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 118 | } |
| 119 | |
| 120 | static void network_roaming_cb (bthf_client_service_type_t type) { |
| 121 | CHECK_CALLBACK_ENV |
| 122 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onNetworkRoaming, (jint) type); |
| 123 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 124 | } |
| 125 | |
| 126 | static void network_signal_cb (int signal) { |
| 127 | CHECK_CALLBACK_ENV |
| 128 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onNetworkSignal, (jint) signal); |
| 129 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 130 | } |
| 131 | |
| 132 | static void battery_level_cb (int level) { |
| 133 | CHECK_CALLBACK_ENV |
| 134 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onBatteryLevel, (jint) level); |
| 135 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 136 | } |
| 137 | |
| 138 | static void current_operator_cb (const char *name) { |
| 139 | jstring js_name; |
| 140 | |
| 141 | CHECK_CALLBACK_ENV |
| 142 | |
| 143 | js_name = sCallbackEnv->NewStringUTF(name); |
| 144 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onCurrentOperator, js_name); |
| 145 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 146 | sCallbackEnv->DeleteLocalRef(js_name); |
| 147 | } |
| 148 | |
| 149 | static void call_cb (bthf_client_call_t call) { |
| 150 | CHECK_CALLBACK_ENV |
| 151 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onCall, (jint) call); |
| 152 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 153 | } |
| 154 | |
| 155 | static void callsetup_cb (bthf_client_callsetup_t callsetup) { |
| 156 | CHECK_CALLBACK_ENV |
| 157 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onCallSetup, (jint) callsetup); |
| 158 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 159 | } |
| 160 | |
| 161 | static void callheld_cb (bthf_client_callheld_t callheld) { |
| 162 | CHECK_CALLBACK_ENV |
| 163 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onCallHeld, (jint) callheld); |
| 164 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 165 | } |
| 166 | |
| 167 | static void resp_and_hold_cb (bthf_client_resp_and_hold_t resp_and_hold) { |
| 168 | CHECK_CALLBACK_ENV |
| 169 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onRespAndHold, (jint) resp_and_hold); |
| 170 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 171 | } |
| 172 | |
| 173 | static void clip_cb (const char *number) { |
| 174 | jstring js_number; |
| 175 | |
| 176 | CHECK_CALLBACK_ENV |
| 177 | |
| 178 | js_number = sCallbackEnv->NewStringUTF(number); |
| 179 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onClip, js_number); |
| 180 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 181 | sCallbackEnv->DeleteLocalRef(js_number); |
| 182 | } |
| 183 | |
| 184 | static void call_waiting_cb (const char *number) { |
| 185 | jstring js_number; |
| 186 | |
| 187 | CHECK_CALLBACK_ENV |
| 188 | |
| 189 | js_number = sCallbackEnv->NewStringUTF(number); |
| 190 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onCallWaiting, js_number); |
| 191 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 192 | sCallbackEnv->DeleteLocalRef(js_number); |
| 193 | } |
| 194 | |
| 195 | static void current_calls_cb (int index, bthf_client_call_direction_t dir, |
| 196 | bthf_client_call_state_t state, |
| 197 | bthf_client_call_mpty_type_t mpty, |
| 198 | const char *number) { |
| 199 | jstring js_number; |
| 200 | |
| 201 | CHECK_CALLBACK_ENV |
| 202 | |
| 203 | js_number = sCallbackEnv->NewStringUTF(number); |
| 204 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onCurrentCalls, index, dir, state, mpty, js_number); |
| 205 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 206 | sCallbackEnv->DeleteLocalRef(js_number); |
| 207 | } |
| 208 | |
| 209 | static void volume_change_cb (bthf_client_volume_type_t type, int volume) { |
| 210 | CHECK_CALLBACK_ENV |
| 211 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onVolumeChange, (jint) type, (jint) volume); |
| 212 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 213 | } |
| 214 | |
| 215 | static void cmd_complete_cb (bthf_client_cmd_complete_t type, int cme) { |
| 216 | CHECK_CALLBACK_ENV |
| 217 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onCmdResult, (jint) type, (jint) cme); |
| 218 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 219 | } |
| 220 | |
| 221 | static void subscriber_info_cb (const char *name, bthf_client_subscriber_service_type_t type) { |
| 222 | jstring js_name; |
| 223 | |
| 224 | CHECK_CALLBACK_ENV |
| 225 | |
| 226 | js_name = sCallbackEnv->NewStringUTF(name); |
| 227 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onSubscriberInfo, js_name, (jint) type); |
| 228 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 229 | sCallbackEnv->DeleteLocalRef(js_name); |
| 230 | } |
| 231 | |
| 232 | static void in_band_ring_cb (bthf_client_in_band_ring_state_t in_band) { |
| 233 | CHECK_CALLBACK_ENV |
| 234 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onInBandRing, (jint) in_band); |
| 235 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 236 | } |
| 237 | |
| 238 | static void last_voice_tag_number_cb (const char *number) { |
| 239 | jstring js_number; |
| 240 | |
| 241 | CHECK_CALLBACK_ENV |
| 242 | |
| 243 | js_number = sCallbackEnv->NewStringUTF(number); |
| 244 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onLastVoiceTagNumber, js_number); |
| 245 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 246 | sCallbackEnv->DeleteLocalRef(js_number); |
| 247 | } |
| 248 | |
Gaurav Asati | 012e866 | 2013-12-26 14:30:25 +0530 | [diff] [blame] | 249 | static void ring_indication_cb () { |
| 250 | CHECK_CALLBACK_ENV |
| 251 | sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onRingIndication); |
| 252 | checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); |
| 253 | } |
| 254 | |
Gaurav Asati | 27604c1 | 2013-08-19 18:54:29 +0530 | [diff] [blame] | 255 | static bthf_client_callbacks_t sBluetoothHfpClientCallbacks = { |
| 256 | sizeof(sBluetoothHfpClientCallbacks), |
| 257 | connection_state_cb, |
| 258 | audio_state_cb, |
| 259 | vr_cmd_cb, |
| 260 | network_state_cb, |
| 261 | network_roaming_cb, |
| 262 | network_signal_cb, |
| 263 | battery_level_cb, |
| 264 | current_operator_cb, |
| 265 | call_cb, |
| 266 | callsetup_cb, |
| 267 | callheld_cb, |
| 268 | resp_and_hold_cb, |
| 269 | clip_cb, |
| 270 | call_waiting_cb, |
| 271 | current_calls_cb, |
| 272 | volume_change_cb, |
| 273 | cmd_complete_cb, |
| 274 | subscriber_info_cb, |
| 275 | in_band_ring_cb, |
| 276 | last_voice_tag_number_cb, |
Gaurav Asati | 012e866 | 2013-12-26 14:30:25 +0530 | [diff] [blame] | 277 | ring_indication_cb, |
Gaurav Asati | 27604c1 | 2013-08-19 18:54:29 +0530 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | static void classInitNative(JNIEnv* env, jclass clazz) { |
| 281 | method_onConnectionStateChanged = env->GetMethodID(clazz, "onConnectionStateChanged", "(III[B)V"); |
| 282 | method_onAudioStateChanged = env->GetMethodID(clazz, "onAudioStateChanged", "(I[B)V"); |
| 283 | method_onVrStateChanged = env->GetMethodID(clazz, "onVrStateChanged", "(I)V"); |
| 284 | method_onNetworkState = env->GetMethodID(clazz, "onNetworkState", "(I)V"); |
| 285 | method_onNetworkRoaming = env->GetMethodID(clazz, "onNetworkRoaming", "(I)V"); |
| 286 | method_onNetworkSignal = env->GetMethodID(clazz, "onNetworkSignal", "(I)V"); |
| 287 | method_onBatteryLevel = env->GetMethodID(clazz, "onBatteryLevel", "(I)V"); |
| 288 | method_onCurrentOperator = env->GetMethodID(clazz, "onCurrentOperator", "(Ljava/lang/String;)V"); |
| 289 | method_onCall = env->GetMethodID(clazz, "onCall", "(I)V"); |
| 290 | method_onCallSetup = env->GetMethodID(clazz, "onCallSetup", "(I)V"); |
| 291 | method_onCallHeld = env->GetMethodID(clazz, "onCallHeld", "(I)V"); |
| 292 | method_onRespAndHold = env->GetMethodID(clazz, "onRespAndHold", "(I)V"); |
| 293 | method_onClip = env->GetMethodID(clazz, "onClip", "(Ljava/lang/String;)V"); |
| 294 | method_onCallWaiting = env->GetMethodID(clazz, "onCallWaiting", "(Ljava/lang/String;)V"); |
| 295 | method_onCurrentCalls = env->GetMethodID(clazz, "onCurrentCalls", "(IIIILjava/lang/String;)V"); |
| 296 | method_onVolumeChange = env->GetMethodID(clazz, "onVolumeChange", "(II)V"); |
| 297 | method_onCmdResult = env->GetMethodID(clazz, "onCmdResult", "(II)V"); |
| 298 | method_onSubscriberInfo = env->GetMethodID(clazz, "onSubscriberInfo", "(Ljava/lang/String;I)V"); |
| 299 | method_onInBandRing = env->GetMethodID(clazz, "onInBandRing", "(I)V"); |
| 300 | method_onLastVoiceTagNumber = env->GetMethodID(clazz, "onLastVoiceTagNumber", |
| 301 | "(Ljava/lang/String;)V"); |
Gaurav Asati | 012e866 | 2013-12-26 14:30:25 +0530 | [diff] [blame] | 302 | method_onRingIndication = env->GetMethodID(clazz, "onRingIndication","()V"); |
Gaurav Asati | 27604c1 | 2013-08-19 18:54:29 +0530 | [diff] [blame] | 303 | |
| 304 | ALOGI("%s succeeds", __FUNCTION__); |
| 305 | } |
| 306 | |
| 307 | static void initializeNative(JNIEnv *env, jobject object) { |
| 308 | const bt_interface_t* btInf; |
| 309 | bt_status_t status; |
| 310 | |
| 311 | btInf = getBluetoothInterface(); |
| 312 | if (btInf == NULL) { |
| 313 | ALOGE("Bluetooth module is not loaded"); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | if (sBluetoothHfpClientInterface != NULL) { |
| 318 | ALOGW("Cleaning up Bluetooth HFP Client Interface before initializing"); |
| 319 | sBluetoothHfpClientInterface->cleanup(); |
| 320 | sBluetoothHfpClientInterface = NULL; |
| 321 | } |
| 322 | |
| 323 | if (mCallbacksObj != NULL) { |
| 324 | ALOGW("Cleaning up Bluetooth HFP Client callback object"); |
| 325 | env->DeleteGlobalRef(mCallbacksObj); |
| 326 | mCallbacksObj = NULL; |
| 327 | } |
| 328 | |
| 329 | sBluetoothHfpClientInterface = (bthf_client_interface_t *) |
| 330 | btInf->get_profile_interface(BT_PROFILE_HANDSFREE_CLIENT_ID); |
| 331 | if (sBluetoothHfpClientInterface == NULL) { |
| 332 | ALOGE("Failed to get Bluetooth HFP Client Interface"); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | status = sBluetoothHfpClientInterface->init(&sBluetoothHfpClientCallbacks); |
| 337 | if (status != BT_STATUS_SUCCESS) { |
| 338 | ALOGE("Failed to initialize Bluetooth HFP Client, status: %d", status); |
| 339 | sBluetoothHfpClientInterface = NULL; |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | mCallbacksObj = env->NewGlobalRef(object); |
| 344 | } |
| 345 | |
| 346 | static void cleanupNative(JNIEnv *env, jobject object) { |
| 347 | const bt_interface_t* btInf; |
| 348 | bt_status_t status; |
| 349 | |
| 350 | if ( (btInf = getBluetoothInterface()) == NULL) { |
| 351 | ALOGE("Bluetooth module is not loaded"); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | if (sBluetoothHfpClientInterface != NULL) { |
| 356 | ALOGW("Cleaning up Bluetooth HFP Client Interface..."); |
| 357 | sBluetoothHfpClientInterface->cleanup(); |
| 358 | sBluetoothHfpClientInterface = NULL; |
| 359 | } |
| 360 | |
| 361 | if (mCallbacksObj != NULL) { |
| 362 | ALOGW("Cleaning up Bluetooth HFP Client callback object"); |
| 363 | env->DeleteGlobalRef(mCallbacksObj); |
| 364 | mCallbacksObj = NULL; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | static jboolean connectNative(JNIEnv *env, jobject object, jbyteArray address) { |
| 369 | jbyte *addr; |
| 370 | bt_status_t status; |
| 371 | |
| 372 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 373 | |
| 374 | addr = env->GetByteArrayElements(address, NULL); |
| 375 | if (!addr) { |
| 376 | jniThrowIOException(env, EINVAL); |
| 377 | return JNI_FALSE; |
| 378 | } |
| 379 | |
| 380 | if ((status = sBluetoothHfpClientInterface->connect((bt_bdaddr_t *)addr)) != BT_STATUS_SUCCESS) { |
| 381 | ALOGE("Failed AG connection, status: %d", status); |
| 382 | } |
| 383 | env->ReleaseByteArrayElements(address, addr, 0); |
| 384 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 385 | } |
| 386 | |
| 387 | static jboolean disconnectNative(JNIEnv *env, jobject object, jbyteArray address) { |
| 388 | jbyte *addr; |
| 389 | bt_status_t status; |
| 390 | |
| 391 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 392 | |
| 393 | addr = env->GetByteArrayElements(address, NULL); |
| 394 | if (!addr) { |
| 395 | jniThrowIOException(env, EINVAL); |
| 396 | return JNI_FALSE; |
| 397 | } |
| 398 | |
| 399 | if ( (status = sBluetoothHfpClientInterface->disconnect((bt_bdaddr_t *)addr)) != BT_STATUS_SUCCESS) { |
| 400 | ALOGE("Failed AG disconnection, status: %d", status); |
| 401 | } |
| 402 | env->ReleaseByteArrayElements(address, addr, 0); |
| 403 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 404 | } |
| 405 | |
| 406 | static jboolean connectAudioNative(JNIEnv *env, jobject object, jbyteArray address) { |
| 407 | jbyte *addr; |
| 408 | bt_status_t status; |
| 409 | |
| 410 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 411 | |
| 412 | addr = env->GetByteArrayElements(address, NULL); |
| 413 | if (!addr) { |
| 414 | jniThrowIOException(env, EINVAL); |
| 415 | return JNI_FALSE; |
| 416 | } |
| 417 | |
| 418 | if ( (status = sBluetoothHfpClientInterface->connect_audio((bt_bdaddr_t *)addr)) != |
| 419 | BT_STATUS_SUCCESS) { |
| 420 | ALOGE("Failed AG audio connection, status: %d", status); |
| 421 | } |
| 422 | env->ReleaseByteArrayElements(address, addr, 0); |
| 423 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 424 | } |
| 425 | |
| 426 | static jboolean disconnectAudioNative(JNIEnv *env, jobject object, jbyteArray address) { |
| 427 | jbyte *addr; |
| 428 | bt_status_t status; |
| 429 | |
| 430 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 431 | |
| 432 | addr = env->GetByteArrayElements(address, NULL); |
| 433 | if (!addr) { |
| 434 | jniThrowIOException(env, EINVAL); |
| 435 | return JNI_FALSE; |
| 436 | } |
| 437 | |
| 438 | if ( (status = sBluetoothHfpClientInterface->disconnect_audio((bt_bdaddr_t *) addr)) != |
| 439 | BT_STATUS_SUCCESS) { |
| 440 | ALOGE("Failed AG audio disconnection, status: %d", status); |
| 441 | } |
| 442 | env->ReleaseByteArrayElements(address, addr, 0); |
| 443 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 444 | } |
| 445 | |
| 446 | static jboolean startVoiceRecognitionNative(JNIEnv *env, jobject object) { |
| 447 | bt_status_t status; |
| 448 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 449 | |
| 450 | if ( (status = sBluetoothHfpClientInterface->start_voice_recognition()) != BT_STATUS_SUCCESS) { |
| 451 | ALOGE("Failed to start voice recognition, status: %d", status); |
| 452 | } |
| 453 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 454 | } |
| 455 | |
| 456 | static jboolean stopVoiceRecognitionNative(JNIEnv *env, jobject object) { |
| 457 | bt_status_t status; |
| 458 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 459 | |
| 460 | if ( (status = sBluetoothHfpClientInterface->stop_voice_recognition()) != BT_STATUS_SUCCESS) { |
| 461 | ALOGE("Failed to stop voice recognition, status: %d", status); |
| 462 | } |
| 463 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 464 | } |
| 465 | |
| 466 | static jboolean setVolumeNative(JNIEnv *env, jobject object, jint volume_type, jint volume) { |
| 467 | bt_status_t status; |
| 468 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 469 | |
| 470 | if ( (status = sBluetoothHfpClientInterface->volume_control((bthf_client_volume_type_t) volume_type, |
| 471 | volume)) != BT_STATUS_SUCCESS) { |
| 472 | ALOGE("FAILED to control volume, status: %d", status); |
| 473 | } |
| 474 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 475 | } |
| 476 | |
| 477 | static jboolean dialNative(JNIEnv *env, jobject object, jstring number_str) { |
| 478 | bt_status_t status; |
| 479 | const char *number; |
| 480 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 481 | |
| 482 | number = env->GetStringUTFChars(number_str, NULL); |
| 483 | |
| 484 | if ( (status = sBluetoothHfpClientInterface->dial(number)) != BT_STATUS_SUCCESS) { |
| 485 | ALOGE("Failed to dial, status: %d", status); |
| 486 | } |
| 487 | env->ReleaseStringUTFChars(number_str, number); |
| 488 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 489 | } |
| 490 | |
| 491 | static jboolean dialMemoryNative(JNIEnv *env, jobject object, jint location) { |
| 492 | bt_status_t status; |
| 493 | |
| 494 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 495 | |
| 496 | if ( (status = sBluetoothHfpClientInterface->dial_memory((int)location)) != BT_STATUS_SUCCESS) { |
| 497 | ALOGE("Failed to dial from memory, status: %d", status); |
| 498 | } |
| 499 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 500 | } |
| 501 | |
| 502 | static jboolean handleCallActionNative(JNIEnv *env, jobject object, jint action, jint index) { |
| 503 | bt_status_t status; |
| 504 | |
| 505 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 506 | |
| 507 | if ( (status = sBluetoothHfpClientInterface->handle_call_action((bthf_client_call_action_t)action, (int)index)) != BT_STATUS_SUCCESS) { |
| 508 | ALOGE("Failed to enter private mode, status: %d", status); |
| 509 | } |
| 510 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 511 | } |
| 512 | |
| 513 | static jboolean queryCurrentCallsNative(JNIEnv *env, jobject object) { |
| 514 | bt_status_t status; |
| 515 | |
| 516 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 517 | |
| 518 | if ( (status = sBluetoothHfpClientInterface->query_current_calls()) != BT_STATUS_SUCCESS) { |
| 519 | ALOGE("Failed to query current calls, status: %d", status); |
| 520 | } |
| 521 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 522 | } |
| 523 | |
| 524 | static jboolean queryCurrentOperatorNameNative(JNIEnv *env, jobject object) { |
| 525 | bt_status_t status; |
| 526 | |
| 527 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 528 | |
| 529 | if ( (status = sBluetoothHfpClientInterface->query_current_operator_name()) != BT_STATUS_SUCCESS) { |
| 530 | ALOGE("Failed to query current operator name, status: %d", status); |
| 531 | } |
| 532 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 533 | } |
| 534 | |
| 535 | static jboolean retrieveSubscriberInfoNative(JNIEnv *env, jobject object) { |
| 536 | bt_status_t status; |
| 537 | |
| 538 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 539 | |
| 540 | if ( (status = sBluetoothHfpClientInterface->retrieve_subscriber_info()) != BT_STATUS_SUCCESS) { |
| 541 | ALOGE("Failed to retrieve subscriber info, status: %d", status); |
| 542 | } |
| 543 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 544 | } |
| 545 | |
| 546 | static jboolean sendDtmfNative(JNIEnv *env, jobject object, jbyte code) { |
| 547 | bt_status_t status; |
| 548 | |
| 549 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 550 | |
| 551 | if ( (status = sBluetoothHfpClientInterface->send_dtmf((char)code)) != BT_STATUS_SUCCESS) { |
| 552 | ALOGE("Failed to send DTMF, status: %d", status); |
| 553 | } |
| 554 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 555 | } |
| 556 | |
| 557 | static jboolean requestLastVoiceTagNumberNative(JNIEnv *env, jobject object) { |
| 558 | bt_status_t status; |
| 559 | |
| 560 | if (!sBluetoothHfpClientInterface) return JNI_FALSE; |
| 561 | |
| 562 | if ( (status = sBluetoothHfpClientInterface->request_last_voice_tag_number()) != BT_STATUS_SUCCESS) { |
| 563 | ALOGE("Failed to request last Voice Tag number, status: %d", status); |
| 564 | } |
| 565 | return (status == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; |
| 566 | } |
| 567 | |
| 568 | static JNINativeMethod sMethods[] = { |
| 569 | {"classInitNative", "()V", (void *) classInitNative}, |
| 570 | {"initializeNative", "()V", (void *) initializeNative}, |
| 571 | {"cleanupNative", "()V", (void *) cleanupNative}, |
| 572 | {"connectNative", "([B)Z", (void *) connectNative}, |
| 573 | {"disconnectNative", "([B)Z", (void *) disconnectNative}, |
| 574 | {"connectAudioNative", "([B)Z", (void *) connectAudioNative}, |
| 575 | {"disconnectAudioNative", "([B)Z", (void *) disconnectAudioNative}, |
| 576 | {"startVoiceRecognitionNative", "()Z", (void *) startVoiceRecognitionNative}, |
| 577 | {"stopVoiceRecognitionNative", "()Z", (void *) stopVoiceRecognitionNative}, |
| 578 | {"setVolumeNative", "(II)Z", (void *) setVolumeNative}, |
| 579 | {"dialNative", "(Ljava/lang/String;)Z", (void *) dialNative}, |
| 580 | {"dialMemoryNative", "(I)Z", (void *) dialMemoryNative}, |
| 581 | {"handleCallActionNative", "(II)Z", (void *) handleCallActionNative}, |
| 582 | {"queryCurrentCallsNative", "()Z", (void *) queryCurrentCallsNative}, |
| 583 | {"queryCurrentOperatorNameNative", "()Z", (void *) queryCurrentOperatorNameNative}, |
| 584 | {"retrieveSubscriberInfoNative", "()Z", (void *) retrieveSubscriberInfoNative}, |
| 585 | {"sendDtmfNative", "(B)Z", (void *) sendDtmfNative}, |
| 586 | {"requestLastVoiceTagNumberNative", "()Z", |
| 587 | (void *) requestLastVoiceTagNumberNative}, |
| 588 | }; |
| 589 | |
| 590 | int register_com_android_bluetooth_hfpclient(JNIEnv* env) |
| 591 | { |
| 592 | return jniRegisterNativeMethods(env, "com/android/bluetooth/hfpclient/HandsfreeClientStateMachine", |
| 593 | sMethods, NELEM(sMethods)); |
| 594 | } |
| 595 | |
| 596 | } /* namespace android */ |