diff options
author | Alex Blasche <alexander.blasche@theqtcompany.com> | 2015-03-18 10:21:22 +0100 |
---|---|---|
committer | Alex Blasche <alexander.blasche@theqtcompany.com> | 2015-03-18 10:46:45 +0000 |
commit | 669b427653f8f708269431917f720f7e76680191 (patch) | |
tree | 1c14bc34541eb32a25efb7ef97307b18abf807c7 | |
parent | 935975a268279671a34084e306b44901d266c419 (diff) |
Android: Fix crash in QBluetoothLocalDevice ctor due to Java exception
In general BluetoothAdapter.getDefaultAdapter should not throw an
exception. If the device does not support Bluetooth the function should
return a null reference only. However some devices throw a
RuntimeException as well. We need to catch it to prevent unwinding of
the stack.
At the same time the patch simplifies the code by using
QAndroidJniObject where possible.
[ChangeLog][QtBluetooth][Android] Fixed crash in QBluetoothLocalDevice
ctor on some devices due to a platform exception in
QBluetoothAdapter.getDefaultAdapter().
Task-number: QTBUG-45066
Change-Id: I933783cda891127e5e37722f96f477c13b433ca7
Reviewed-by: Timur Pocheptsov <Timur.Pocheptsov@digia.com>
Reviewed-by: Christian Stromme <christian.stromme@theqtcompany.com>
-rw-r--r-- | src/bluetooth/qbluetoothlocaldevice_android.cpp | 38 |
1 files changed, 10 insertions, 28 deletions
diff --git a/src/bluetooth/qbluetoothlocaldevice_android.cpp b/src/bluetooth/qbluetoothlocaldevice_android.cpp index 11515743..8160a15d 100644 --- a/src/bluetooth/qbluetoothlocaldevice_android.cpp +++ b/src/bluetooth/qbluetoothlocaldevice_android.cpp @@ -83,35 +83,20 @@ void QBluetoothLocalDevicePrivate::initialize(const QBluetoothAddress &address) { QAndroidJniEnvironment env; - jclass btAdapterClass = env->FindClass("android/bluetooth/BluetoothAdapter"); - if (btAdapterClass == NULL) { - qCWarning(QT_BT_ANDROID) - << "Native registration unable to find class android/bluetooth/BluetoothAdapter"; - return; - } - - jmethodID getDefaultAdapterID - = env->GetStaticMethodID(btAdapterClass, "getDefaultAdapter", - "()Landroid/bluetooth/BluetoothAdapter;"); - if (getDefaultAdapterID == NULL) { - qCWarning(QT_BT_ANDROID) - << "Native registration unable to get method ID: " \ - "getDefaultAdapter of android/bluetooth/BluetoothAdapter"; - return; - } - - jobject btAdapterObject = env->CallStaticObjectMethod(btAdapterClass, getDefaultAdapterID); - if (btAdapterObject == NULL) { + QAndroidJniObject adapter = QAndroidJniObject::callStaticObjectMethod( + "android/bluetooth/BluetoothAdapter", "getDefaultAdapter", + "()Landroid/bluetooth/BluetoothAdapter;"); + if (!adapter.isValid()) { + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + env->ExceptionClear(); + } qCWarning(QT_BT_ANDROID) << "Device does not support Bluetooth"; - env->DeleteLocalRef(btAdapterClass); return; } - obj = new QAndroidJniObject(btAdapterObject); - if (!obj->isValid()) { - delete obj; - obj = 0; - } else if (!address.isNull()) { + obj = new QAndroidJniObject(adapter); + if (!address.isNull()) { const QString localAddress = obj->callObjectMethod("getAddress", "()Ljava/lang/String;").toString(); if (localAddress != address.toString()) { @@ -120,9 +105,6 @@ void QBluetoothLocalDevicePrivate::initialize(const QBluetoothAddress &address) obj = 0; } } - - env->DeleteLocalRef(btAdapterObject); - env->DeleteLocalRef(btAdapterClass); } bool QBluetoothLocalDevicePrivate::isValid() const |