You can download this code by clicking the button below.
This code is now available for download.
This function uses the `discover_devices` method from the PyBluez library to find nearby Bluetooth Low Energy (BLE) devices, with an option to filter by device name.
Technology Stack : PyBluez
Code Type : Function
Code Difficulty : Intermediate
import random
from pybluez import bluetooth
def discover_devices(name=None, lookup_names=True):
# Discover nearby BLE devices with optional name filter
nearby_devices = bluetooth.discover_devices(lookup_names=lookup_names, duration=10)
if name:
nearby_devices = [dev for dev in nearby_devices if dev['name'] == name]
return nearby_devices
# JSON Explanation