Last active
December 17, 2024 10:27
-
-
Save av1d/b62856224751e817685b477f9deda7bd to your computer and use it in GitHub Desktop.
List Service UUID, Characteristic UUID and Properties of Bluetooth BLE device using bluepy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from bluepy.btle import Scanner, DefaultDelegate, Peripheral | |
# the address of your device. Find it using nRF Connect or in your Bluetooth settings | |
device_address = "00:00:00:00:00:00" | |
# example output: | |
""" | |
Device found with address: 00:00:00:00:00:00 | |
Service UUID: 00001800-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a00-0000-1000-8000-00805f9b34fb, Properties: READ WRITE | |
Service UUID: 00001800-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a01-0000-1000-8000-00805f9b34fb, Properties: READ WRITE | |
Service UUID: 00001800-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a04-0000-1000-8000-00805f9b34fb, Properties: READ | |
Service UUID: 00001800-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002ac9-0000-1000-8000-00805f9b34fb, Properties: READ | |
Service UUID: 00001801-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a05-0000-1000-8000-00805f9b34fb, Properties: READ INDICATE | |
Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a29-0000-1000-8000-00805f9b34fb, Properties: READ | |
Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a24-0000-1000-8000-00805f9b34fb, Properties: READ | |
Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a25-0000-1000-8000-00805f9b34fb, Properties: READ | |
Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a27-0000-1000-8000-00805f9b34fb, Properties: READ | |
Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a26-0000-1000-8000-00805f9b34fb, Properties: READ | |
Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a28-0000-1000-8000-00805f9b34fb, Properties: READ | |
Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a23-0000-1000-8000-00805f9b34fb, Properties: READ | |
Service UUID: 0000180a-0000-1000-8000-00805f9b34fb, Characteristic UUID: 00002a50-0000-1000-8000-00805f9b34fb, Properties: READ | |
Service UUID: 02f00000-0000-0000-0000-00000000fe00, Characteristic UUID: 02f00000-0000-0000-0000-00000000ff03, Properties: READ | |
Service UUID: 02f00000-0000-0000-0000-00000000fe00, Characteristic UUID: 02f00000-0000-0000-0000-00000000ff02, Properties: READ NOTIFY | |
Service UUID: 02f00000-0000-0000-0000-00000000fe00, Characteristic UUID: 02f00000-0000-0000-0000-00000000ff00, Properties: READ | |
Service UUID: 02f00000-0000-0000-0000-00000000fe00, Characteristic UUID: 02f00000-0000-0000-0000-00000000ff01, Properties: WRITE NO RESPONSE WRITE | |
Service UUID: 0000dc00-0000-1000-8000-00805f9b34fb, Characteristic UUID: 0000dc01-0000-1000-8000-00805f9b34fb, Properties: READ NOTIFY | |
Service UUID: 0000dc00-0000-1000-8000-00805f9b34fb, Characteristic UUID: 0000dc02-0000-1000-8000-00805f9b34fb, Properties: READ WRITE NO RESPONSE WRITE NOTIFY | |
""" | |
def connection_error(): | |
raise Exception( | |
"No devices found.\n" | |
+ "Troubleshooting Checklist:\n" | |
+ "- Try again a few times. Sometimes this is the best method.\n" | |
+ "- Have you paired this device to the other device first?\n" | |
+ "- Is Bluetooth enabled and turned on on this device?\n" | |
+ "- Are you running this app as sudo or elevated user? You must.\n" | |
+ "- Make sure no other devices are connected to the device.\n" | |
+ "- Try turning off then on Bluetooth on this device.\n" | |
+ "- If using a health tracker, make sure you're moving it slightly for detection.\n" | |
+ "- If health tracker still not found, place on the charger then remove to reset it.\n" | |
) | |
peripheral.disconnect() | |
sys.exit(1) | |
print("If you're using a health tracking device make sure it is in motion to enable detection. Move it gently...") | |
peripheral = None | |
scanner = Scanner().withDelegate(DefaultDelegate()) | |
try: | |
devices = scanner.scan(10.0) | |
except Exception as e: | |
print(f"Error: {e}\nIs Bluetooth turned on on this device?\nYou must run this app as sudo or privileged user, are you?") | |
peripheral.disconnect() | |
sys.exit(1) | |
if not devices: | |
connection_error() | |
aimed_device = next((dev for dev in devices if dev.addr.lower() == device_address.lower()), None) | |
if not aimed_device: | |
connection_error() | |
print("Device found with address:", aimed_device.addr) | |
try: | |
peripheral = Peripheral(aimed_device) | |
services = peripheral.getServices() | |
except Exception as e: | |
print(f"Error: {e}\nTry turning off Bluetooth then turning it back on.") | |
peripheral.disconnect() | |
sys.exit(1) | |
try: | |
for service in services: | |
characteristics = service.getCharacteristics() | |
for characteristic in characteristics: | |
properties = characteristic.propertiesToString() | |
print(f"Service UUID: {service.uuid}, Characteristic UUID: {characteristic.uuid}, Properties: {properties}") | |
except Exception as e: | |
print(f"Error: {e}\nProbably failed to connect even if device was found. Check troubleshooting or try again.") | |
peripheral.disconnect() | |
sys.exit(1) | |
peripheral.disconnect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
l02f00000-0000-0000-0000-00000000fe00