Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Root access on Android



  • I have read a tons of forums and materials about this thing, but I have no correct idea how to do this. I'm working on some soft based on 5.15.* Qt version. Porting to Android was successful, but I have some problems with QSerialPort. As far I understand some issues can be solved with rooted devices. I have one and successfully can catch usb attaching event. Moreover QSerialPort::availablePorts() finds some ports, but QSerialPortError::PersmissionDenied is caught.

    So I want try to run my program with root privileges, but don't know how to do this. I would be appreciate for any help.


  • Lifetime Qt Champion

    @St-Stanislav So, did you root this device?
    To run an application as root you usually do:

    sudo ./MyApp
    # Or
    su
    ./MyApp
    

    Also, it is actually better to give the user running the app needed access rights instead of running whole application as root. For that you need to add your user to the group which is set on the device file.



  • @St-Stanislav said in Root access on Android:

    So I want try to run my program with root privileges, but don't know how to do this. I would be appreciate for any help.

    I think you take the problem from the wrong side, as @jsulm suggest, this is a permission issue. Which often happens with Android, depending on which API level your device is using!

    Have you checked if you have request corresponding permission in your manifest file?

    <uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
    

    ==> https://developer.android.com/things/sdk/pio/uart

    I suppose your device is rooted, so you can try to install your app as system app ==> https://stackoverflow.com/questions/28302833/how-to-install-an-app-in-system-app-while-developing-from-android-studio



  • @jsulm Yes, device is rooted and I have checked some applications, that request root privileges with popup on Android. Now I am able to show the same popup with

    QProcess serialProcess;
    serialProcess.start("su");
    serialProcess.waitForStarted(10000);
    serialProcess.waitForFinished();
    

    This trick shows permission's popup, but already found serial ports can not be opened anyway. I think that it's so due to main process stays non-root.


  • Lifetime Qt Champion

    @St-Stanislav I suggest you first try to change access rights for the device, then there is no need for root at all.



  • @KroMignon @jsulm Yes, I have the following permissions in the manifest file

    <uses-permission android:name="android.permission.USB_PERMISSION"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
    

    Then I catch usb attaching event:

    <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
    </intent-filter>
    

    And filter devices by PID and VID:

    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
    

    It works, when I attach my serial convertor with OTG, popup is opened and I can chose my program to be opened. But I don't really know, where my devices is mounted. Now I looking for any information with dmesg on my tablet.

    Thank you for your link, I will try it out.


Log in to reply