ROS topic does not unbind/close when app closes

asked 2019-08-28 03:52:33 -0500

rosmax gravatar image

Hello there,

I went through the ROS Tutorials as well as the Android Core Examples to start creating my own rosjava application within AndroidStudio. I develop on macOS 10.14 on a MacBook Air Mid'15 where ROS is not installed. I pulled the necessary ROS libraries via Maven as mentioned here. My Android device is a LG VK810 Tablet running Android 5.0.1. The ROS Kinetic installation is on a Linux Ubuntu 16.04 machine where roscore and the publisher node are running. In order to make my app work I had to add the dependency org.ros.android_core:android_core_components:0.4.0 as well as other ones so that my MainActivity could extend RosActivity and my node classes extend AbstractNodeMain. So I added a few more dependencies to my module build.gradle file as visible below:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion = '28.0.3'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'org.ros.rosjava_core:rosjava:[0.3,0.4)'
    implementation 'org.ros.rosjava_core:rosjava_geometry:[0.3,0.4)'
    implementation 'org.ros.rosjava_messages:geometry_msgs:[1.12,1.13)'
    implementation "org.ros.rosjava_messages:diagnostic_msgs:[1.12,1.13)"
    implementation "org.ros.rosjava_messages:sensor_msgs:[1.12,1.13)"
    implementation "org.ros.rosjava_messages:std_msgs:0.5.11"
    implementation 'org.ros.rosjava_messages:visualization_msgs:[1.12,1.13)'
    implementation 'org.ros.rosjava_bootstrap:message_generation:0.3.3:'
    implementation 'org.ros.android_core:android_core_components:0.4.0'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

The app is generally working fine. I only realised that unlike the android_core_pub_sub_tutorial my app doesn't shut down the connection when it's being closed or even killed by the Android task manager. Afterwards I can still see my subscribing nodes with rostopic list. Additional to that, when relaunching the app the MasterChooserActivity does not get loaded but I see my actual activity_main.xml right away. In order to be able to see the MasterChooser again I have to reinstall the app via AndroidStudio.

Whenever I close the android_core_pub_sub_tutorial a little toast at the bottom of the screen saying "Shutting down..." tells me that the node is completely closed and the rostopic is no longer visible using rostopic list.

By searching my project for that little toast message I found out that the NodeMainExecutorService is responsible for a) toasting and b) shutting down.

public void onDestroy() {
        this.toast("Shutting down...");
        this.nodeMainExecutor.shutdown();
        if (this.rosCore != null) {
            this.rosCore.shutdown();
        }

        if (this.wakeLock.isHeld()) {
            this.wakeLock.release();
        }

        if (this.wifiLock.isHeld()) {
            this.wifiLock.release ...
(more)
edit retag flag offensive close merge delete