Convert Figma logo to code with AI

pybluez logopybluez

Bluetooth Python extension module

2,303
667
2,303
50

Top Related Projects

1,636

Plyer is a platform-independent Python wrapper for platform-dependent APIs

1,857

A cross platform Bluetooth Low Energy Client for Python using asyncio

1,610

Python interface to Bluetooth LE on Linux

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

  1. 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}")
  1. 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}")
  1. 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:

  1. Install PyBluez:

    pip install pybluez
    
  2. Import the library in your Python script:

    import bluetooth
    
  3. 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

1,636

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.

1,857

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.

1,610

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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

⛔️ 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

Build Status

The PyBluez module allows Python code to access the host machine's Bluetooth resources.

Platform Support

LinuxRaspberry PimacOSWindows
:heavy_check_mark::heavy_check_mark::heavy_check_mark::heavy_check_mark:

Python Version Support

Python 2Python 3 (min 3.5)
Till Version 0.22Version 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:

GNU/Linux only examples:

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