Skip to content
Related Articles

Related Articles

Locking computer when Bluetooth device is not in range using Python

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Jul, 2020
View Discussion
Improve Article
Save Article

Python provides the accessibility of using Bluetooth UUID address of one’s phone as a physical security token for any system. This can be done with the help of a Python package named PyBluez. Pybluez can be installed in Linux, Windows, and macOS and it is compatible with Python 2.7 and 3.x.

Required Installations:

The modules needed are:

  • Pybluez: This module allows to use system Bluetooth resources. To install it type the below command in the terminal.
    python3 -m pip install pybluez
  • Schedule: Schedule Library is used to schedule a task at a particular time every day or a particular day of a week. To install it type the below command in the terminal.
    python3 -m pip install schedule


PyBluez is a package with Bluetooth resources which allows Python developers to easily create Bluetooth applications. At first, the necessary packages have been imported into the program. PyBluez is imported as Bluetooth, the schedule is imported for scheduling the program, time package is imported to handle time-related tasks and ctypes is imported to use the existing libraries in other languages, by writing simple wrappers in Python. Following are the steps.

  • A function job() is defined in which bluetooth address of your mobile is declared in the variable inputBdaddr. The variable passed is initialized to False to keep track whether the given bluetooth address is found in the discovered devices. To search for the available bluetooth devices nearby, bluetooth.discover_devices() is used. The results are saved in the variable scan inside the try-except block. If the required device is found in scan, the variable passed is set to true, else false.
  • If passed is found as false, meaning that the required device is not found, the workstation is locked.
  • Now schedule the above steps by calling function job() every 30 seconds. A while loop is used to check whether any scheduled task is pending to run or not.

Below is the implementation:

# Import required packages
import schedule
import time
import bluetooth
import ctypes
def job():
    # Find your bluetooth uuid in your
    # mobile and give set it in the 
    # variable
    inputBdaddr = "XX:XX:XX:XX:XX:XX"
    # Variable to find whether the
    # given bluetooth uuid is 
    # present in the discovered devices
    passed = False
    # Try to search for the nearby 
    # visible devices
        # Get the list of discovered devices
        scan = bluetooth.discover_devices()
        # Search for your bluetooth uuid 
        # in the scanned devices If found
        # set the variable to true else 
        # set the variable to false
        if inputBdaddr in scan:
            passed = True
            passed = False
        passed = False
    # When bluetooth device 
    # is not found, lock the
    # workstation
    if not passed:
# Schedule the process 
# to run every 30 seconds
# Check whether a scheduled 
# task is pending to run or not
while 1:


Since PyBluez is not under active development, Bluetooth detection is probabilistic. discover_devices() will sometimes fail to detect devices that are in range. In this case, it may be a good idea to try again once or twice before giving up.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!