mirror of
https://github.com/OpenBluetoothToolbox/SimpleBLE
synced 2025-10-19 11:18:36 +08:00

* Fixed broken examples, added unit tests. * Linting * More fixes * Fixed PyTest execution * Fixed PyTest execution * Fixed PyTest execution * Fixed uninitialized memory
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
import simplepyble
|
|
|
|
if __name__ == "__main__":
|
|
adapters = simplepyble.Adapter.get_adapters()
|
|
|
|
if len(adapters) == 0:
|
|
print("No adapters found")
|
|
|
|
# Query the user to pick an adapter
|
|
print("Please select an adapter:")
|
|
for i, adapter in enumerate(adapters):
|
|
print(f"{i}: {adapter.identifier()} [{adapter.address()}]")
|
|
|
|
choice = int(input("Enter choice: "))
|
|
adapter = adapters[choice]
|
|
|
|
print(f"Selected adapter: {adapter.identifier()} [{adapter.address()}]")
|
|
|
|
adapter.set_callback_on_scan_start(lambda: print("Scan started."))
|
|
adapter.set_callback_on_scan_stop(lambda: print("Scan complete."))
|
|
adapter.set_callback_on_scan_found(lambda peripheral: print(f"Found {peripheral.identifier()} [{peripheral.address()}]"))
|
|
|
|
# Scan for 5 seconds
|
|
adapter.scan_for(5000)
|
|
peripherals = adapter.scan_get_results()
|
|
|
|
# Query the user to pick a peripheral
|
|
print("Please select a peripheral:")
|
|
for i, peripheral in enumerate(peripherals):
|
|
print(f"{i}: {peripheral.identifier()} [{peripheral.address()}]")
|
|
|
|
choice = int(input("Enter choice: "))
|
|
peripheral = peripherals[choice]
|
|
|
|
print(f"Connecting to: {peripheral.identifier()} [{peripheral.address()}]")
|
|
peripheral.connect()
|
|
|
|
print("Successfully connected, listing services...")
|
|
services = peripheral.services()
|
|
service_characteristic_pair = []
|
|
for service in services:
|
|
for characteristic in service.characteristics():
|
|
service_characteristic_pair.append((service.uuid(), characteristic.uuid()))
|
|
|
|
# Query the user to pick a service/characteristic pair
|
|
print("Please select a service/characteristic pair:")
|
|
for i, (service_uuid, characteristic) in enumerate(service_characteristic_pair):
|
|
print(f"{i}: {service_uuid} {characteristic}")
|
|
|
|
choice = int(input("Enter choice: "))
|
|
service_uuid, characteristic_uuid = service_characteristic_pair[choice]
|
|
|
|
# Query the user for content to write
|
|
content = input("Enter content to write: ")
|
|
|
|
# Write the content to the characteristic
|
|
# Note: `write_request` required the payload to be presented as a bytes object.
|
|
peripheral.write_request(service_uuid, characteristic_uuid, str.encode(content))
|
|
|
|
peripheral.disconnect()
|