How to notify the advertising device by pc-ble-driver-py?

Hi,

I try to advertise nrf52840 dongle on pc to build a connection with nrf5340 uart service. Although I try to modify the advertising program by advertising.py and heart_rate_collector.py, I still can't succeed in notifying the uart handle. Here is my code, which results in 'cccd not found'.

class HRCollector(BLEDriverObserver, BLEAdapterObserver):
    def __init__(self, adapter):
        super(HRCollector, self).__init__()
        self.adapter = adapter
        self.conn_q = Queue()
        self.adapter.observer_register(self)
        self.adapter.driver.observer_register(self)
        self.adapter.default_mtu = 250
        from pc_ble_driver_py.ble_driver import BLEUUIDBase
        self.nus_base = BLEUUIDBase([
            0x6e, 0x40, 0x00, 0x00, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5, 0x0e, 0x24, 0xdc, 0xca, 0x9e
        ])

    def open(self):

        self.adapter.driver.open()
        if config.__conn_ic_id__.upper() == "NRF51":
            self.adapter.driver.ble_enable(
                BLEEnableParams(
                    vs_uuid_count=1,
                    service_changed=0,
                    periph_conn_count=0,
                    central_conn_count=1,
                    central_sec_count=0,
                )
            )
        elif config.__conn_ic_id__.upper() == "NRF52":

            gatt_cfg = BLEConfigConnGatt()
            gatt_cfg.att_mtu = self.adapter.default_mtu
            gatt_cfg.tag = CFG_TAG
            self.adapter.driver.ble_cfg_set(BLEConfig.conn_gatt, gatt_cfg)

            self.adapter.driver.ble_enable()
            self.adapter.driver.ble_vs_uuid_add(self.nus_base)

    def connect_and_discover(self):
        scan_duration = 20
        adv_data = BLEAdvData(complete_local_name="nrf52840")
        # adv_data = BLEAdvData(complete_local_name="pc_ble_driver_py")
        self.adapter.driver.ble_gap_adv_data_set(adv_data)

        self.adapter.driver.ble_gap_adv_start()
        scan_duration = 60
        try:
            new_conn = self.conn_q.get(timeout=scan_duration)
            self.adapter.enable_notification(
                new_conn, BLEUUID(Uart_Tx_uuid), uart_tx_handle
            )
            return new_conn
        except Empty:
            print(f"No heart rate collector advertising with name {TARGET_DEV_NAME} found.")
            return None
        return None
 

def main(selected_serial_port):
    print("Serial port used: {}".format(selected_serial_port))
    driver = BLEDriver(
        serial_port=selected_serial_port, auto_flash=False, baud_rate=1000000, log_severity_level="info"
    )
    observer = TimeoutObserver()
    driver.observer_register(observer)
    adapter = BLEAdapter(driver)
    collector = HRCollector(adapter)
    conn = collector.open()
    collector.connect_and_discover()
    observer.wait_for_timeout()
    collector.close()

I find that the db_conns has empty service. Is this way to notify advertising device's handle not correct? Thank you for your help!

Parents
  • Hello,

    Just letting you know that I am looking into it, but I will have to get back to you.

    Regards,

    Elfving

  • Hi Elfving,

    Thank you for your attention. I am still struggling with this problem and try to solve it.

    Regards,

    zhaoyuan

  • Hello Zhaoyuan,

    I am not seeing any service discovery in your code. Maybe that is why its not getting any CCCDs.

    Regards,

    Elfving

  • Hi Elfving,

    It really helps. Thank you for your kindly help! By the way, I try to notify the uart over ble handle but get nothing, which however works in scaning way. Have you met this problem? I am not very familiar with BLE. I would be grateful if you could give me some advice. Here is the logs.

    Regards,

    zhaoyuan

    2022-12-14 23:41:24,604 [4704/LogThread] Successfully opened COM16. Baud rate: 1000000. Flow control: none. Parity: none.
    2022-12-14 23:41:24,604 [4704/LogThread] evt> severity(20) message(Successfully opened COM16. Baud rate: 1000000. Flow control: none. Parity: none.)
    2022-12-14 23:41:24,605 [18004/StatusThread] RpcAppStatus.resetPerformed: Target Reset performed
    2022-12-14 23:41:24,605 [18004/StatusThread] evt> status code(RpcAppStatus.resetPerformed) message(Target Reset performed)
    2022-12-14 23:41:25,850 [18004/StatusThread] RpcAppStatus.connectionActive: Connection active
    2022-12-14 23:41:25,850 [18004/StatusThread] evt> status code(RpcAppStatus.connectionActive) message(Connection active)
    New connection: 3
    2022-12-14 23:41:26,022 [19220/EventThread] Exception: MTU exchange reply failed. Common causes are: missing att_mtu setting in ble_cfg_set, different config tags used in ble_cfg_set and adv_start.
    2022-12-14 23:41:26,023 [19220/EventThread] <FrameSummary file D:\zhaoyuan_data\dsp\brain_usb\pc-ble-driver-py-master\pc_ble_driver_py\ble_driver.py, line 3001 in ble_event_handler_sync>
    2022-12-14 23:41:26,023 [19220/EventThread] <FrameSummary file D:\zhaoyuan_data\dsp\brain_usb\pc-ble-driver-py-master\pc_ble_driver_py\ble_adapter.py, line 800 in on_gatts_evt_exchange_mtu_request>
    2022-12-14 23:41:26,023 [19220/EventThread] 
    2022-12-14 23:41:26,024 [19220/EventThread] evt> on_gattc_evt_prim_srvc_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Service uuid(0x1801) start handle(1) end handle(8)
     Service uuid(0x1800) start handle(9) end handle(13)
    2022-12-14 23:41:26,037 [19220/EventThread] evt> on_gattc_evt_prim_srvc_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Service uuid(0x0001) start handle(14) end handle(19)
    2022-12-14 23:41:26,051 [19220/EventThread] evt> on_gattc_evt_prim_srvc_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(3)
     
    2022-12-14 23:41:26,066 [19220/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Characteristic uuid(0x2A05) properties(BLECharProperties(broadcast=0, read=0, write_wo_resp=0, write=0, notify=0, indicate=1, auth_signed_wr=0)) declaration handle(2) value handle(3)
     Characteristic uuid(0x2B29) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=1, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(5) value handle(6)
    2022-12-14 23:41:26,127 [19220/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Characteristic uuid(0x2B2A) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(7) value handle(8)
    2022-12-14 23:41:26,142 [19220/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(3)
     
    2022-12-14 23:41:26,156 [19220/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Descriptor uuid(0x2A05) handle(3)
     Descriptor uuid(0x2902 (Standard.cccd)) handle(4)
    2022-12-14 23:41:26,217 [19220/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Descriptor uuid(0x2B29) handle(6)
    2022-12-14 23:41:26,277 [19220/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Descriptor uuid(0x2B2A) handle(8)
    2022-12-14 23:41:26,292 [19220/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Characteristic uuid(0x2A00) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(10) value handle(11)
     Characteristic uuid(0x2A01) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(12) value handle(13)
    2022-12-14 23:41:26,306 [19220/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(3)
     
    2022-12-14 23:41:26,367 [19220/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Descriptor uuid(0x2A00) handle(11)
    2022-12-14 23:41:26,427 [19220/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Descriptor uuid(0x2A01) handle(13)
    2022-12-14 23:41:26,441 [19220/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Characteristic uuid(0x0003) properties(BLECharProperties(broadcast=0, read=0, write_wo_resp=0, write=0, notify=1, indicate=0, auth_signed_wr=0)) declaration handle(15) value handle(16)
    2022-12-14 23:41:26,456 [19220/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Characteristic uuid(0x0002) properties(BLECharProperties(broadcast=0, read=0, write_wo_resp=1, write=1, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(18) value handle(19)
    2022-12-14 23:41:26,516 [19220/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(3)
     
    2022-12-14 23:41:26,576 [19220/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Descriptor uuid(0x0003) handle(16)
    2022-12-14 23:41:26,591 [19220/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Descriptor uuid(0x2902 (Standard.cccd)) handle(17)
    2022-12-14 23:41:26,606 [19220/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(3)
     Descriptor uuid(0x0002) handle(19)
    2022-12-14 23:41:26,666 [19220/EventThread] evt> on_gattc_evt_write_rsp conn(3)
     status(BLEGattStatusCode.success)
     error_handle(0)
     attr_handle(17)
     write_op(BLEGattWriteOperation.write_req)
     offset(0)
     data([])
    2022-12-14 23:41:36,675 [4704/LogThread] serial port COM16 closed.
    2022-12-14 23:41:36,675 [4704/LogThread] evt> severity(20) message(serial port COM16 closed.)
    2022-12-14 23:41:36,675 [16984/MainThread] close result 0
    2022-12-14 23:41:36,675 [16984/MainThread] Stopping workers
    2022-12-14 23:41:37,681 [16984/MainThread] Workers stopped
    
    Process finished with exit code 0
    

  • zhaoyuan said:
    Have you met this problem?

    No, I haven't personally.

    zhaoyuan said:
    I am not very familiar with BLE.
    zhaoyuan said:
    I would be grateful if you could give me some advice

    In that case I would recommend you to rather work on something a bit newer and easier than pc-ble-driver. The lack of documentation can make it really difficult to work with.

    What are you trying to achieve? Maybe the NUS sample has all the features you need?

    Regards,

    Elfving

Reply
  • zhaoyuan said:
    Have you met this problem?

    No, I haven't personally.

    zhaoyuan said:
    I am not very familiar with BLE.
    zhaoyuan said:
    I would be grateful if you could give me some advice

    In that case I would recommend you to rather work on something a bit newer and easier than pc-ble-driver. The lack of documentation can make it really difficult to work with.

    What are you trying to achieve? Maybe the NUS sample has all the features you need?

    Regards,

    Elfving

Children
Related