KDE Conect / GSConnect: How to Lock / Unlock Linux Desktop with Android Device

KDE connection with GSConnect Supports running commands from an Android device to the Linux desktop. This article explains how to lock and unlock the Linux desktop with a single click on your Android device KDE Conect Android Application. You will also find a workaround for GNOME 3.34, whose session is managed by systemd, which makes it a bit more complicated.
KDE Conect is a tool that enables devices such as desktops running Linux and Android devices to communicate with each other. To use it, you must install the desktop components on your Linux desktop and the Android application on your Android device. GSConnect is a complete implementation of KDE Connect created for the GNOME Shell desktop.
It’s worth noting that the commands we use to lock / unlock the screen in this article are not related to the desktop and can be used with any desktop environment (and any Linux distribution) that uses a login name.
In addition, no password is required to unlock the screen. This is because the lock / unlock command we will use below does not require superuser privileges. Also, because KDE Connect can run commands on the Linux desktop even when the screen is locked, the unlock command can unlock the session without a password.
Edit: take a look video Display screen lock / unlock using KDE Connect Android app and GSConnect (using GNOME 3.34) installed on Fedora 31; I am using scrcpy to display Android phone screen on desktop): in order to be able to lock / unlock screen device using Android Open KDE Conect / GSConnect and:

  • KDE Connect: In the list of application plugins, make sure the Run Command plugin is enabled, then click Run commands Configuration button
  • GSConnect: click on your device name and you will find one Commands Section in sidebar
KDE Connect command

Next, add the following command to GNOME 3.32 or earlier KDE Connect / GSConnect on your desktop (not applicable to GNOME 3.34 or later!).

  • Lock screen: loginctl lock-session
  • Unlock the screen: loginctl unlock-session

After adding these commands to the Linux desktop, they will appear in the KDE Conect Android app as ” Run Command:KDE Conect Android Lock / Unlock Screen

Since GNOME switch In order to use systemd (with GNOME 3.34) to manage their sessions, these loginctl lock/unlock The command stops working on its own and requires the session ID to lock or unlock the screen. I managed to work around a workaround for this-use the following command (see below) loginctl List all sessions, search the current user in the session list, get the ID of that session, then use loginctl lock-session Either unlock-session Then comes the session ID.

GSConnect command
GSConnect command

So if you use GSConnect with GNOME 3.34 (or later), use the following command instead of the command I mentioned above:

  • Lock screen:
SESSION=$(loginctl list-sessions | grep $(whoami) | awk '{print $1}'); loginctl lock-session $SESSION
  • Unlock the screen:
SESSION=$(loginctl list-sessions | grep $(whoami) | awk '{print $1}'); loginctl unlock-session $SESSION

I currently have only one computer running GNOME 3.34 that can test this and run it in it (using Ubuntu 19.10), hopefully it will also work on other Linux distributions. Edit: I also tried Fedora 31 and it worked as expected.KDE Conect Android WidgetKDE Conect Android application supports widgets. So you don’t need to open KDE Connect Android application every time you want to lock or unlock your computer, just add a widget to click and lock and unlock the Linux desktop.
You can find more useful commands on KDE Conect / GSConnect. This page.