Have you ever wanted a high-tech gizmo that could identify neighboring devices, akin to James Bond’s? You may, however, make your own Bluetooth device scanner using Python! Like a digital bloodhound, this program will detect Bluetooth Low Energy (LE) devices nearby and report their presence.
This blog will walk you through every step of the process, from installing required libraries to writing the code and comprehending how it works. At the conclusion, you’ll have a Python script that functions as a Bluetooth radar, demonstrating Python’s amazing capabilities for hardware interaction.
Installation
To embark on this Bluetooth scanning adventure, we’ll need a trusty Python library called pybluez
. Here’s how to install it:
- Open your terminal or command prompt.
- Type the following command and press Enter:
pip install pybluez
This command instructs the pip
package manager to install pybluez
for your Python environment.
Building the Scanner
Now, let’s delve into the code! Create a new Python file (e.g., scanner.py
) and paste the following:
import bluetooth
print("Scanning for Bluetooth devices...")
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print("Found %d devices" % len(nearby_devices))
for addr, name in nearby_devices:
try:
print("Address: " + addr + " , Name: " + name)
except TypeError:
print("Address: " + addr + " , Name: unknown")
Explanation
- Import
bluetooth
: This line imports thepybluez
library, granting us access to Bluetooth functionalities in Python. - Print a message: This line simply informs the user that the scanning process is beginning.
- Discover devices: The
discover_devices
the function does the heavy lifting. It searches for Bluetooth LE devices within range and returns a list containing two elements for each device: its MAC address and its name (if available). The lookup_names argument is set toTrue
attempt to retrieve device names. - Print number of devices: This line displays the total number of devices discovered.
- Iterate through devices: We loop through the list of devices returned by
discover_devices
. - Extract address and name: Inside the loop,
addr
stores the device’s MAC address, andname
holds its name (if retrieved successfully). - Print device information: This line prints the MAC address and the device name (or “unknown” if unavailable) for each discovered device.
- Handle errors: The
try-except
block gracefully handles potential errors that might occur during name retrieval.
Running the Scanner
Save the scanner.py
file and navigate to its location in your terminal. Now, execute the script using the following command:
python scanner.py
The program will initiate the scan, displaying a message indicating the search and then listing the discovered devices with their addresses and names (or “unknown”).
Voila! You’ve built a basic Bluetooth device scanner in Python. This is just the beginning; you can extend this code to filter specific devices, display additional information like signal strength, or even connect to paired devices!
Remember: This scanner primarily detects Bluetooth LE devices, commonly found in smartphones, wearables, and other low-power gadgets. It might not detect classic Bluetooth devices like speakers or headsets.
This is very exciting, I a pythoner for 2yrs but didn’t knew that this exists, thank you
Thank you for reading blog
An excellent read that will keep readers – particularly me – coming back for more! Also, I’d genuinely appreciate if you check my website QN6 about Car Purchase. Thank you and best of luck!