diff options
author | Alex Blasche <alexander.blasche@digia.com> | 2014-03-04 11:05:08 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-03-11 10:59:00 +0100 |
commit | 54ba9be8850dc7fbeb7151ed5579a22965851008 (patch) | |
tree | 9e50ea80992a8dc442f21d04d81f98f7b8f672cb /src/bluetooth/android/localdevicebroadcastreceiver.cpp | |
parent | b2cd03fb14c9727e23d5ada409551ab236d1abc6 (diff) |
Android: Don't directly use java field values
Action strings were previously converted. This patch fixes all
non-action fields.
Task-number: QTBUG-36810
Change-Id: I4011c619e900dcb4137c6552046f0342327a7288
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
Diffstat (limited to 'src/bluetooth/android/localdevicebroadcastreceiver.cpp')
-rw-r--r-- | src/bluetooth/android/localdevicebroadcastreceiver.cpp | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/src/bluetooth/android/localdevicebroadcastreceiver.cpp b/src/bluetooth/android/localdevicebroadcastreceiver.cpp index c221a7cc..5b7e4a38 100644 --- a/src/bluetooth/android/localdevicebroadcastreceiver.cpp +++ b/src/bluetooth/android/localdevicebroadcastreceiver.cpp @@ -48,6 +48,9 @@ QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID) +const char *scanModes[] = {"SCAN_MODE_NONE", "SCAN_MODE_CONNECTABLE", "SCAN_MODE_CONNECTABLE_DISCOVERABLE"}; +const char *bondModes[] = {"BOND_NONE", "BOND_BONDING", "BOND_BONDED"}; + LocalDeviceBroadcastReceiver::LocalDeviceBroadcastReceiver(QObject *parent) : AndroidBroadcastReceiver(parent), previousScanMode(0) { @@ -57,6 +60,31 @@ LocalDeviceBroadcastReceiver::LocalDeviceBroadcastReceiver(QObject *parent) : addAction(valueForStaticField(JavaNames::BluetoothDevice, JavaNames::ActionAclDisconnected)); if (QtAndroidPrivate::androidSdkVersion() >= 19) addAction(valueForStaticField(JavaNames::BluetoothDevice, JavaNames::ActionPairingRequest)); //API 19 + + //cache integer values for host & bonding mode + //don't use the java fields directly but refer to them by name + QAndroidJniEnvironment env; + for (uint i = 0; i < (sizeof(hostModePreset)/sizeof(hostModePreset[0])); i++) { + hostModePreset[i] = QAndroidJniObject::getStaticField<jint>( + "android/bluetooth/BluetoothAdapter", + scanModes[i]); + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + env->ExceptionClear(); + hostModePreset[i] = 0; + } + } + + for (uint i = 0; i < (sizeof(bondingModePreset)/sizeof(bondingModePreset[0])); i++) { + bondingModePreset[i] = QAndroidJniObject::getStaticField<jint>( + "android/bluetooth/BluetoothDevice", + bondModes[i]); + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + env->ExceptionClear(); + bondingModePreset[i] = 0; + } + } } void LocalDeviceBroadcastReceiver::onReceive(JNIEnv *env, jobject context, jobject intent) @@ -83,20 +111,14 @@ void LocalDeviceBroadcastReceiver::onReceive(JNIEnv *env, jobject context, jobje if (previousScanMode != extra) { previousScanMode = extra; - switch (extra) { - case 20: //BluetoothAdapter.SCAN_MODE_NONE - emit hostModeStateChanged(QBluetoothLocalDevice::HostPoweredOff); - break; - case 21: //BluetoothAdapter.SCAN_MODE_CONNECTABLE - emit hostModeStateChanged(QBluetoothLocalDevice::HostConnectable); - break; - case 23: //BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE - emit hostModeStateChanged(QBluetoothLocalDevice::HostDiscoverable); - break; - default: - qCWarning(QT_BT_ANDROID) << "Unknown Host State"; - break; - } + if (extra == hostModePreset[0]) + emit hostModeStateChanged(QBluetoothLocalDevice::HostPoweredOff); + else if (extra == hostModePreset[1]) + emit hostModeStateChanged(QBluetoothLocalDevice::HostConnectable); + else if (extra == hostModePreset[2]) + emit hostModeStateChanged(QBluetoothLocalDevice::HostDiscoverable); + else + qCWarning(QT_BT_ANDROID) << "Unknown Host State"; } } else if (action == valueForStaticField(JavaNames::BluetoothDevice, JavaNames::ActionBondStateChanged).toString()) { @@ -120,17 +142,13 @@ void LocalDeviceBroadcastReceiver::onReceive(JNIEnv *env, jobject context, jobje if (address.isNull()) return; - switch (bondState) { - case 10: //BluetoothDevice.BOND_NONE + if (bondState == bondingModePreset[0]) emit pairingStateChanged(address, QBluetoothLocalDevice::Unpaired); - break; - case 11: //BluetoothDevice.BOND_BONDING - //we ignore this as Qt doesn't have equivalent API. - break; - case 12: //BluetoothDevice.BOND_BONDED + else if (bondState == bondingModePreset[1]) + ; //we ignore this as Qt doesn't have equivalent API value + else if (bondState == bondingModePreset[2]) emit pairingStateChanged(address, QBluetoothLocalDevice::Paired); - break; - default: + else qCWarning(QT_BT_ANDROID) << "Unknown BOND_STATE_CHANGED value:" << bondState; } else if (action == valueForStaticField(JavaNames::BluetoothDevice, |