Top Related Projects
Quick Overview
PyBluez is a Python extension module that provides access to system Bluetooth resources. It allows Python developers to create Bluetooth applications, enabling device discovery, service advertisement, and communication between Bluetooth-enabled devices. PyBluez supports both Linux and Windows platforms.
Pros
- Cross-platform compatibility (Linux and Windows)
- Simplifies Bluetooth programming in Python
- Supports both RFCOMM and L2CAP protocols
- Active community and ongoing development
Cons
- Limited documentation and examples
- Compatibility issues with newer Python versions
- Lack of native macOS support
- Some users report installation difficulties
Code Examples
- Discovering nearby Bluetooth devices:
import bluetooth
nearby_devices = bluetooth.discover_devices(lookup_names=True)
for addr, name in nearby_devices:
print(f"Address: {addr}, Name: {name}")
- Establishing an RFCOMM connection:
import bluetooth
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
bluetooth.advertise_service(server_sock, "SampleServer", service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE])
client_sock, client_info = server_sock.accept()
print(f"Accepted connection from {client_info}")
- Sending data over Bluetooth:
import bluetooth
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect(("00:11:22:33:44:55", 1))
data = "Hello, Bluetooth!"
sock.send(data)
sock.close()
Getting Started
To get started with PyBluez:
-
Install PyBluez:
pip install pybluez
-
Import the library in your Python script:
import bluetooth
-
Use the provided functions to interact with Bluetooth devices:
nearby_devices = bluetooth.discover_devices() for addr in nearby_devices: print(f"Found device: {addr}")
Note: On some systems, you may need to install additional dependencies or run the script with elevated privileges to access Bluetooth resources.
Competitor Comparisons
Plyer is a platform-independent Python wrapper for platform-dependent APIs
Pros of Plyer
- Broader platform support, including iOS, Android, and desktop systems
- More comprehensive feature set beyond just Bluetooth functionality
- Simpler, more abstracted API for cross-platform development
Cons of Plyer
- Less specialized for Bluetooth operations compared to PyBluez
- May have more overhead due to its broader scope
- Potentially slower development cycle for Bluetooth-specific features
Code Comparison
PyBluez example:
import bluetooth
nearby_devices = bluetooth.discover_devices(lookup_names=True)
for addr, name in nearby_devices:
print(f"Address: {addr}, Name: {name}")
Plyer example:
from plyer import bluetooth
bluetooth.start_discovery()
devices = bluetooth.get_discovered_devices()
for device in devices:
print(f"Address: {device['address']}, Name: {device['name']}")
Summary
Plyer offers a more versatile toolkit for cross-platform development with a wider range of features, while PyBluez specializes in Bluetooth functionality. PyBluez may be more suitable for projects focused solely on Bluetooth operations, whereas Plyer is better suited for applications requiring broader platform support and additional features beyond Bluetooth.
A cross platform Bluetooth Low Energy Client for Python using asyncio
Pros of Bleak
- Cross-platform support (Windows, macOS, Linux) for Bluetooth Low Energy (BLE)
- Asynchronous API design, leveraging Python's asyncio for better performance
- Active development and maintenance, with regular updates and bug fixes
Cons of Bleak
- Limited to BLE devices only, not supporting classic Bluetooth
- Steeper learning curve due to its asynchronous nature
- May require additional dependencies for certain platforms
Code Comparison
Bleak example:
import asyncio
from bleak import BleakClient
async def main():
address = "XX:XX:XX:XX:XX:XX"
async with BleakClient(address) as client:
await client.write_gatt_char(CHARACTERISTIC_UUID, b"Hello World!")
asyncio.run(main())
PyBluez example:
import bluetooth
nearby_devices = bluetooth.discover_devices(lookup_names=True)
for addr, name in nearby_devices:
print(f"Address: {addr}, Name: {name}")
Summary
Bleak offers modern, cross-platform BLE support with an asynchronous API, making it suitable for complex IoT projects. PyBluez provides a simpler interface for both classic Bluetooth and BLE but lacks cross-platform consistency and active development. Choose Bleak for BLE-focused, high-performance applications, and PyBluez for simpler, classic Bluetooth tasks or legacy systems.
Python interface to Bluetooth LE on Linux
Pros of bluepy
- Focused on Bluetooth Low Energy (BLE) devices, providing more specialized functionality
- More actively maintained with recent updates and contributions
- Better documentation and examples for BLE-specific use cases
Cons of bluepy
- Limited to Linux systems, reducing cross-platform compatibility
- Requires root privileges for certain operations, which may be a security concern
- Steeper learning curve for users new to BLE concepts
Code Comparison
bluepy example:
from bluepy.btle import Scanner, DefaultDelegate
scanner = Scanner().withDelegate(DefaultDelegate())
devices = scanner.scan(10.0)
for dev in devices:
print(f"Device {dev.addr} ({dev.addrType}), RSSI={dev.rssi} dB")
PyBluez example:
import bluetooth
nearby_devices = bluetooth.discover_devices(lookup_names=True)
for addr, name in nearby_devices:
print(f"Device {addr} - {name}")
PyBluez offers a simpler API for general Bluetooth operations, while bluepy provides more detailed control over BLE-specific features. PyBluez has broader platform support but may lack some advanced BLE capabilities found in bluepy.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
âï¸ This project is not under development
Consider some alternatives:
- Bleak: Cross-platform Bluetooth Low Energy client library
- Bless: Cross-platform Bluetooth Low Energy server library
- PyQt: Python bindings for cross-platform QtBluetooth API
PyBluez
The PyBluez module allows Python code to access the host machine's Bluetooth resources.
Platform Support
Linux | Raspberry Pi | macOS | Windows |
---|---|---|---|
:heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
Python Version Support
Python 2 | Python 3 (min 3.5) |
---|---|
Till Version 0.22 | Version 0.23 and newer |
Examples
# simple inquiry example
import bluetooth
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print("Found {} devices.".format(len(nearby_devices)))
for addr, name in nearby_devices:
print(" {} - {}".format(addr, name))
# bluetooth low energy scan
from bluetooth.ble import DiscoveryService
service = DiscoveryService()
devices = service.discover(2)
for address, name in devices.items():
print("name: {}, address: {}".format(name, address))
GNU/Linux and Windows XP examples:
- examples/simple/inquiry.py - Detecting nearby Bluetooth devices
- examples/simple/sdp-browse.py - Browsing SDP services on a Bluetooth device.
- examples/simple/rfcomm-server.py - establishing an RFCOMM connection.
- examples/simple/rfcomm-client.py - establishing an RFCOMM connection.
- examples/advanced/read-local-bdaddr.py - provides the local Bluetooth device address.
GNU/Linux only examples:
- examples/simple/l2capserver.py
- examples/simple/l2capclient.py
- examples/simple/asynchronous-inquiry.py
- examples/bluezchat
- examples/advanced/inquiry-with-rssi.py
- examples/advanced/l2-unreliable-server.py
- examples/advanced/l2-unreliable-client.py
GNU/Linux experimental BLE support:
Contact
Please file bugs to the issue tracker. Questions can be asked on the mailing list hosted on Google Groups, but unfortunately it is not very active.
Installation
Please refer to the installation instructions.
License
PyBluez is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
PyBluez is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with PyBluez; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Top Related Projects
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot