DEV Community

Cover image for Automated Session Control with Bluetooth: An Insight into ble-lock-session
Connor Manraz
Connor Manraz

Posted on

Automated Session Control with Bluetooth: An Insight into ble-lock-session

Introduction

ble-lock-session is a simple Python tool that uses Bluetooth to automatically lock or unlock your computer based on the proximity of a device, such as your smartphone or smartwatch. It was created to bring a touch of automation to everyday computer usage, securing your environment effortlessly and with minimal hardware requirements.

In this article, we will look into how ble-lock-session works, its underlying design, and some interesting ways you can extend its functionality beyond the basic lock/unlock mechanism.

What is ble-lock-session?

ble-lock-session is a Python-based tool that interacts with your system’s Bluetooth stack to determine whether a paired device is nearby. Based on this information, it can automatically run a command to either lock or unlock your session.

The goal is straightforward: when your device (like a phone) is detected, the computer unlocks, and when the device is no longer in range, the computer locks itself.

How Does It Work?

Here's a brief breakdown of how ble-lock-session works:

  1. Configuration: The tool reads settings from a configuration file (config.ini), which includes the target Bluetooth device address, the commands for locking and unlocking, and intervals for checking device presence.

  2. Bluetooth Monitoring: Using the Python bluetooth library, it regularly scans for the specified Bluetooth device. If the device is present, an unlock command is executed; if not, a lock command is triggered.

  3. Customizable Commands: The flexibility comes from being able to configure the commands that are executed when the Bluetooth device is detected or lost. This means you aren't limited to just locking or unlocking—any command can be used, making it highly customizable.

Here’s a typical use case:

  • Locking your PC: gnome-screensaver-command --lock.
  • Unlocking your PC: gnome-screensaver-command -d (for GNOME desktops).

But the possibilities go well beyond this.

Going Beyond Session Locking/Unlocking

The original purpose of ble-lock-session was to secure your computer based on proximity, but since it runs shell commands, you can use it for a range of tasks that suit your workflow or lifestyle.

Examples of Extended Uses

Here are a few creative ways to use ble-lock-session:

  • Music Playback Control: Pause your music when you walk away, and resume it when you return:
  lock_cmd = "playerctl pause"
  unlock_cmd = "playerctl play"
Enter fullscreen mode Exit fullscreen mode
  • Disable WiFi/Ethernet on Leave: Reduce distractions or conserve energy by disabling your network interface when you leave your desk:
  lock_cmd = "nmcli radio wifi off"
  unlock_cmd = "nmcli radio wifi on"
Enter fullscreen mode Exit fullscreen mode

The versatility of ble-lock-session comes from being able to use any shell command, making it a powerful tool for building customized automation workflows that fit your unique needs.

A Quick Start

To get started with ble-lock-session, clone the GitHub repository and install the required dependencies:

git clone https://github.com/azratul/ble-lock-session.git
cd ble-lock-session
pip install pybluez
Enter fullscreen mode Exit fullscreen mode

Scan for your Bluetooth device and configure the commands:

./ble-lock-session.py --scan
Enter fullscreen mode Exit fullscreen mode

Once configured, start the lock/unlock feature:

./ble-lock-session.py --start
Enter fullscreen mode Exit fullscreen mode

You can also update the configuration interactively:

./ble-lock-session.py --config
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

ble-lock-session is a straightforward tool that brings automation into your daily workflow with minimal setup. By leveraging the proximity of a Bluetooth device, you can lock and unlock your computer seamlessly and do much more.

This kind of flexibility is perfect for those who like to experiment with automation. Whether you use it to control your music, manage your network, or any other creative scenario, ble-lock-session can be adapted to fit your needs.

Try it out and see what kind of unique automations you can come up with.

Useful Links

Top comments (0)