Commit fe63cf82 authored by Matej Frančeškin's avatar Matej Frančeškin

Detect bluetooth gamepad connect/disconnect events

parent 5464e107
......@@ -7,6 +7,8 @@
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"/>
</intent-filter>
......
......@@ -55,6 +55,8 @@ import android.os.PowerManager;
import android.os.Bundle;
import android.app.PendingIntent;
import android.view.WindowManager;
import android.os.Bundle;
import android.bluetooth.BluetoothDevice;
import com.hoho.android.usbserial.driver.*;
import org.qtproject.qt5.android.bindings.QtActivity;
......@@ -162,6 +164,12 @@ public class QGCActivity extends QtActivity
}
}
}
try {
nativeUpdateAvailableJoysticks();
} catch(Exception e) {
Log.e(TAG, "Exception nativeUpdateAvailableJoysticks()");
}
}
};
......@@ -169,6 +177,7 @@ public class QGCActivity extends QtActivity
private static native void nativeDeviceHasDisconnected(int userData);
private static native void nativeDeviceException(int userData, String messageA);
private static native void nativeDeviceNewData(int userData, byte[] dataA);
private static native void nativeUpdateAvailableJoysticks();
// Native C++ functions called to log output
public static native void qgcLogDebug(String message);
......@@ -200,8 +209,11 @@ public class QGCActivity extends QtActivity
// Register for USB Detach and USB Permission intent
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(ACTION_USB_PERMISSION);
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
_instance.registerReceiver(_instance._usbReceiver, filter);
// Create intent for usb permission request
......
......@@ -134,7 +134,6 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe
env->ReleaseIntArrayElements(jarr, buff, 0);
return ret;
}
......@@ -171,7 +170,6 @@ bool JoystickAndroid::handleGenericMotionEvent(jobject event) {
return true;
}
bool JoystickAndroid::_open(void) {
return true;
}
......@@ -199,8 +197,14 @@ uint8_t JoystickAndroid::_getHat(int hat,int i) {
return 0;
}
static JoystickManager *_manager = nullptr;
//helper method
bool JoystickAndroid::init() {
bool JoystickAndroid::init(JoystickManager *manager) {
if (_manager == nullptr) {
setNativeMethods(manager);
}
//this gets list of all possible buttons - this is needed to check how many buttons our gamepad supports
//instead of the whole logic below we could have just a simple array of hardcoded int values as these 'should' not change
......@@ -239,3 +243,52 @@ bool JoystickAndroid::init() {
return true;
}
static const char kJniClassName[] {"org/mavlink/qgroundcontrol/QGCActivity"};
static void jniUpdateAvailableJoysticks(JNIEnv *envA, jobject thizA)
{
Q_UNUSED(envA);
Q_UNUSED(thizA);
if (_manager != nullptr) {
qCDebug(JoystickLog) << "jniUpdateAvailableJoysticks triggered";
emit _manager->updateAvailableJoysticksSignal();
}
}
void JoystickAndroid::setNativeMethods(JoystickManager *manager)
{
qCDebug(JoystickLog) << "Registering Native Functions";
_manager = manager;
// REGISTER THE C++ FUNCTION WITH JNI
JNINativeMethod javaMethods[] {
{"nativeUpdateAvailableJoysticks", "()V", reinterpret_cast<void *>(jniUpdateAvailableJoysticks)}
};
QAndroidJniEnvironment jniEnv;
if (jniEnv->ExceptionCheck()) {
jniEnv->ExceptionDescribe();
jniEnv->ExceptionClear();
}
jclass objectClass = jniEnv->FindClass(kJniClassName);
if(!objectClass) {
qWarning() << "Couldn't find class:" << kJniClassName;
return;
}
jint val = jniEnv->RegisterNatives(objectClass, javaMethods, sizeof(javaMethods) / sizeof(javaMethods[0]));
if (val < 0) {
qWarning() << "Error registering methods: " << val;
} else {
qCDebug(JoystickLog) << "Native Functions Registered";
}
if (jniEnv->ExceptionCheck()) {
jniEnv->ExceptionDescribe();
jniEnv->ExceptionClear();
}
}
......@@ -19,7 +19,7 @@ public:
~JoystickAndroid();
static bool init();
static bool init(JoystickManager *manager);
static QMap<QString, Joystick*> discover(MultiVehicleManager* _multiVehicleManager);
......@@ -27,6 +27,8 @@ private:
bool handleKeyEvent(jobject event);
bool handleGenericMotionEvent(jobject event);
static void setNativeMethods(JoystickManager *manager);
virtual bool _open();
virtual void _close();
virtual bool _update();
......
......@@ -59,11 +59,13 @@ void JoystickManager::init() {
}
_setActiveJoystickFromSettings();
#elif defined(__android__)
if (!JoystickAndroid::init()) {
if (!JoystickAndroid::init(this)) {
return;
}
connect(this, &JoystickManager::updateAvailableJoysticksSignal, this, &JoystickManager::restartJoystickCheckTimer);
#endif
connect(&_joystickCheckTimer, &QTimer::timeout, this, &JoystickManager::_updateAvailableJoysticks);
_joystickCheckTimerCounter = 5;
_joystickCheckTimer.start(1000);
}
......@@ -185,7 +187,7 @@ void JoystickManager::setActiveJoystickName(const QString& name)
/*
* TODO: move this to the right place: JoystickSDL.cc and JoystickAndroid.cc respectively and call through Joystick.cc
*/
void JoystickManager::_updateAvailableJoysticks(void)
void JoystickManager::_updateAvailableJoysticks()
{
#ifdef __sdljoystick__
SDL_Event event;
......@@ -207,6 +209,16 @@ void JoystickManager::_updateAvailableJoysticks(void)
}
}
#elif defined(__android__)
_joystickCheckTimerCounter--;
_setActiveJoystickFromSettings();
if (_joystickCheckTimerCounter <= 0) {
_joystickCheckTimer.stop();
}
#endif
}
void JoystickManager::restartJoystickCheckTimer()
{
_joystickCheckTimerCounter = 5;
_joystickCheckTimer.start(1000);
}
......@@ -45,6 +45,8 @@ public:
QString activeJoystickName(void);
void setActiveJoystickName(const QString& name);
void restartJoystickCheckTimer(void);
// Override from QGCTool
virtual void setToolbox(QGCToolbox *toolbox);
......@@ -55,6 +57,7 @@ signals:
void activeJoystickChanged(Joystick* joystick);
void activeJoystickNameChanged(const QString& name);
void availableJoysticksChanged(void);
void updateAvailableJoysticksSignal();
private slots:
void _updateAvailableJoysticks(void);
......@@ -70,6 +73,7 @@ private:
static const char * _settingsGroup;
static const char * _settingsKeyActiveJoystick;
int _joystickCheckTimerCounter;
QTimer _joystickCheckTimer;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment