Hello,
I'm facing a problem in pc-ble-driver-py. I adjusted the uuid_count, but the code still fails at the service discovery line:
ble_adapter.service_discovery(new_conn).
Attached is the code that searches for devices by MAC address, connects to the corresponding MAC address, initiates service discovery, and prints the data received after enabling notifications. I have two MAC addresses: one for a heart rate sensor and another for an HMI module I'm working on. Service discovery works perfectly with the heart rate sensor but fail with the HMI module. Could you please help me in identifying where I might be going wrong?
import sys import time from queue import Queue, Empty from pc_ble_driver_py.ble_driver import * from pc_ble_driver_py.ble_adapter import * from pc_ble_driver_py.observers import * # Constants SERIAL_PORT = 'COM4' BAUD_RATE = 1000000 # MAC_ADDRESS = 'F2:3A:48:56:0B:72' # mac address for heart rate sensor MAC_ADDRESS = 'C6:C8:B9:35:FC:5D' # mac address for connected hmi # Observer class to handle BLE events class MyBLEDriverObserver(BLEDriverObserver, BLEAdapterObserver): def __init__(self, adapter): super(MyBLEDriverObserver, self).__init__() self.found_device = False self.conn_handle = None self.conn_queue = Queue() self.ble_adapter = adapter self.ble_adapter.observer_register(self) self.ble_adapter.driver.observer_register(self) self.ble_adapter.default_mtu = 250 def on_gap_evt_adv_report(self, ble_driver, conn_handle, peer_addr, rssi, adv_type, adv_data): address_string = ":".join("{0:02X}".format(b) for b in peer_addr.addr) print(f"Found device with address: {address_string}") if address_string == MAC_ADDRESS: print("Attempting to connect to device...") try: self.found_device = True self.ble_adapter.connect(peer_addr, tag=1) print(f"Connection initiated to {address_string}") except Exception as e: print(f"Failed to connect: {str(e)}") def on_gap_evt_connected(self, ble_driver, conn_handle, peer_addr, role, conn_params): address_string = ":".join("{0:02X}".format(b) for b in peer_addr.addr) print(f"Connected to {address_string}") self.conn_handle = conn_handle self.conn_queue.put(conn_handle) def on_gap_evt_disconnected(self, ble_driver, conn_handle, reason): print(f"Disconnected, reason: {reason}") self.conn_handle = None def on_gap_evt_timeout(self, ble_driver, conn_handle, src): print("Connection timed out") def on_notification(self, ble_adapter, conn_handle, uuid, data): if len(data) > 32: data = "({}...)".format(data[0:10]) print("Connection: {}, {} = {}".format(conn_handle, uuid, data)) def disconnect(self, conn_handle): if conn_handle is not None: self.ble_adapter.disconnect(conn_handle) print(f"Disconnected from {MAC_ADDRESS}") def main(): UUID_serial_number = "8E580002-3B28-4145-9213-F0646C15AB5E" UUID_transmission_control = "6A430001-50EF-47EB-81B3-3FD7644155EC" UUID_HeartRate_BATTERY_LEVEL = BLEUUID(BLEUUID.Standard.battery_level) # Initialize the BLE driver ble_driver = BLEDriver(serial_port=SERIAL_PORT, baud_rate=BAUD_RATE, log_severity_level="trace") ble_adapter = BLEAdapter(ble_driver) observer = MyBLEDriverObserver(ble_adapter) ble_adapter.driver.open() # Configuring GATT settings (MTU size) gatt_cfg = BLEConfigConnGatt() gatt_cfg.att_mtu = ble_adapter.default_mtu gatt_cfg.tag = 1 ble_adapter.driver.ble_cfg_set(BLEConfig.conn_gatt, gatt_cfg) ble_adapter.driver.ble_enable() # Start scanning for peripherals scan_duration = 5 scan_params = BLEGapScanParams(interval_ms=200, window_ms=150, timeout_s=scan_duration) ble_adapter.driver.ble_gap_scan_start(scan_params=scan_params) print("Scanning...") # Wait for device discovery try: new_conn = observer.conn_queue.get(timeout=scan_duration) print("a = ", new_conn) a = ble_adapter.service_discovery(new_conn) print("Device Connected") # ble_adapter.enable_notification(new_conn, BLEUUID(UUID_serial_number)) # ble_adapter.enable_notification(new_conn, UUID_transmission_control) # ble_adapter.enable_notification(new_conn, UUID_HeartRate_BATTERY_LEVEL) except Empty: print("No Device found.") return # Keep the program running to handle BLE events if new_conn is not None: try: while True: time.sleep(1) except KeyboardInterrupt: print("Keyboard Interrupt!!!") print("Disconnecting...") observer.disconnect(observer.conn_handle) ble_adapter.driver.close() if __name__ == "__main__": main()