A Python library for creating BLE peripherals using BlueZ and D-Bus
Project description
PyBLEz
PyBLEz is a Python library for creating BLE peripherals using BlueZ and D-Bus. This library allows you to easily set up BLE services and characteristics, handle read and write operations, and send notifications to connected devices.
Usage
Creating a Simple BLE Peripheral
#!/user/bin/env python3
from PyBLEz import create_ble_peripheral, enable_logs, disable_logs
# Enable debug logs
enable_logs()
def main():
# create a BLE peripheral instance
ble = create_ble_peripheral()
ble.power_on_adapter()
# Add a service
service = ble.add_service("12345678-1234-5678-1234-56789abcdef0")
# Add a characteristic to the service
char = service.add_characteristic("12345678-1234-5678-1234-56789abcdef1", ["read", "write", "notify"], bytearray("Hello", "utf-8"))
# Defind the read_value function
def read_value(options):
return char.value
char.Read = read_value
# Define the write_value function
def write_value(value, options):
data = bytes(value).decode("utf-8")
print(f"Received: {data}")
processed = data.upper()
char.value = bytearray(processed, "utf-8")
print(f"Processed: {processed}")
char.Write = write_value
# Register the GATT application
ble.register_application()
# Start advertising (10 seconds)
ble.start_advertising("HelloBLE", [service.uuid], duration=10)
# Run the main loop
ble.run()
if __name__ == "__main__":
main()
Detailed Explanation
Read Characteristic:
- Readable
characteristic
is created with the UUID12345678-1234-5678-1234-56789abcdef1
and is set to return the value "Hello". - The
read_value
function is assigned to the characteristic'sRead
method.
Write Characteristic:
- Writable is created with the UUID
12345678-1234-5678-1234-56789abcdef1
and allows writing data. - The
write_value
function is assigned to the characteristic'sWrite
method.
Advertising:
- The peripheral advertises with the local name "HelloBLE" and includes the service UUID.
Running the Peripheral:
- The
run
method starts the main loop to keep the application running and responsive to BLE interactions.
Requirements
- Python 3.6 or later
- BlueZ (5.41 or later)
- D-Bus (python-dbus)
- Pycairo
- GLib
Installation
pip install PyBLEz
Conrtibuting
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or improvements.
License
This project is licensed under the MIT License.
Contact
For any questions or inquiries, please contact goecke.dev@gmail.com
.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file PyBLEz-0.2.3.tar.gz
.
File metadata
- Download URL: PyBLEz-0.2.3.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e891413d600df53c163996c895ef6cad6bd9734ac6fae58b5e1b0fa0915d1ad9 |
|
MD5 | 116aa76a514be1a03d68060d8c874ba1 |
|
BLAKE2b-256 | 0d3564919d034def992174147b1b6061fc402156113711c3e7221d3e0c85aa84 |
File details
Details for the file PyBLEz-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: PyBLEz-0.2.3-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aee6e94d8ca146fce6b7a453cf63fa5824663baaa546cab44313c9519e9ab449 |
|
MD5 | 207d9dcfcf698c78a5a19fca38dde0be |
|
BLAKE2b-256 | 8623dffbdf4bc46091e876b9d6f68b2e0c7ef19e41bd0350c1e2954d9b5ca928 |