How to use on_notification fuction to get notification

I am a python beginner, I read a lot of related questions, but they all ask us to look heart_rate_collector.py , but heart_rate_collector.py did't not have any example, i really don't understand how to used it.

def on_notification(self, ble_adapter, conn_handle, uuid, data):
data should be the thing i want to get,is that right?
 

Parents
  • Hi,

    The pc-ble-driver-py is a python wrapper for the pc-ble-driver, and the heart_rate_collector.py is the included example that demonstrate how to use it. In other words, heart_rate_collector.py is the example.

    I did not understand the question other than that, though. What is it you are wondering about? As you mention on_notification, this is a function that is called whenever a notification is received, and it prints the connection handle, the UUID that got notified and the data that was notified (i.e. the received data).

  • How do I get notification from (uuid:0xfff6) and read data.

    I think "on_notification" can only show the data what i gave it(hightlight).

    on_notification(self, ble_adapter, conn_handle, uuid, data)

    So my question is how to get the 'data'?

    I have use "self.adapter.enable_notification(new_conn, self.nus_tx)"

    self.nus_tx = BLEUUID(0xfff6, self.nus_base)
  • I have connect my device and do service discovery ,I have seen heart_rate_collector.py  example, but it did not use on_notification() to print notify data .

    This is my LOG 

    Serial port used: COM23
    2023-01-05 21:59:16,769 [10572/LogThread] Successfully opened COM23. Baud rate: 1000000. Flow control: none. Parity: none.
    2023-01-05 21:59:16,770 [10572/LogThread] evt> severity(20) message(Successfully opened COM23. Baud rate: 1000000. Flow control: none. Parity: none.)
    2023-01-05 21:59:16,770 [4240/StatusThread] RpcAppStatus.resetPerformed: Target Reset performed
    2023-01-05 21:59:16,771 [4240/StatusThread] evt> status code(RpcAppStatus.resetPerformed) message(Target Reset performed)
    2023-01-05 21:59:18,022 [4240/StatusThread] RpcAppStatus.connectionActive: Connection active
    2023-01-05 21:59:18,022 [4240/StatusThread] evt> status code(RpcAppStatus.connectionActive) message(Connection active)
    2023-01-05 21:59:18,034 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(47:7B:24:D2:9C:24)
     rssi(-52)
     adv_type(BLEGapAdvType.non_connectable_undirected)
    2023-01-05 21:59:18,039 [12928/EventThread] Invalid advertising data: [2, 1, 6, 26, 255, 76, 0, 2, 21, 176, 145, 34, 101, 85, 217, 1, 33, 83, 89, 145, 37, 4, 37, 98, 7, 37, 38, 0, 0, 41, 3]
    2023-01-05 21:59:18,040 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(B0:91:22:65:55:D9)
     rssi(-56)
     adv_type(BLEGapAdvType.connectable_undirected)
    2023-01-05 21:59:18,041 [12928/EventThread] Invalid advertising data: [2, 1, 6, 26, 255, 76, 0, 2, 21, 176, 145, 34, 101, 85, 217, 1, 33, 83, 89, 145, 37, 4, 37, 98, 7, 37, 38, 0, 0, 41, 3]
    2023-01-05 21:59:18,042 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(B0:91:22:65:55:D9)
     rssi(-56)
     adv_type(None)
    Received advertisment report, address: 0xB091226555D9, device_name: UTL_B_Node_55D9
    2023-01-05 21:59:18,043 [12928/EventThread] Invalid advertising data: [2, 1, 6, 26, 255, 76, 0, 2, 21, 176, 145, 34, 101, 88, 144, 1, 33, 83, 88, 148, 37, 4, 38, 103, 7, 37, 38, 0, 0, 41, 3]
    2023-01-05 21:59:18,044 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(B0:91:22:65:58:90)
     rssi(-77)
     adv_type(BLEGapAdvType.connectable_undirected)
    2023-01-05 21:59:18,045 [12928/EventThread] Invalid advertising data: [2, 1, 6, 26, 255, 76, 0, 2, 21, 176, 145, 34, 101, 88, 144, 1, 33, 83, 88, 148, 37, 4, 38, 103, 7, 37, 38, 0, 0, 41, 3]
    2023-01-05 21:59:18,048 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(54:6C:0E:A4:FC:CC)
     rssi(-67)
     adv_type(BLEGapAdvType.connectable_undirected)
    2023-01-05 21:59:18,049 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(54:6C:0E:A4:FC:CC)
     rssi(-67)
     adv_type(None)
    Received advertisment report, address: 0x546C0EA4FCCC, device_name: $FCCC-5815-2674-0701
    2023-01-05 21:59:18,054 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(80:6F:B0:31:DA:C6)
     rssi(-35)
     adv_type(BLEGapAdvType.connectable_undirected)
    2023-01-05 21:59:18,055 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(80:6F:B0:31:DA:C6)
     rssi(-35)
     adv_type(None)
    Received advertisment report, address: 0x806FB031DAC6, device_name: Insole_Right_9
    2023-01-05 21:59:18,059 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(25:3F:87:CB:17:8A)
     rssi(-73)
     adv_type(BLEGapAdvType.non_connectable_undirected)
    2023-01-05 21:59:18,061 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(54:6C:0E:A4:FA:DC)
     rssi(-67)
     adv_type(BLEGapAdvType.connectable_undirected)
    2023-01-05 21:59:18,063 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(54:6C:0E:A4:FA:DC)
     rssi(-67)
     adv_type(None)
    Received advertisment report, address: 0x546C0EA4FADC, device_name: $FADC-5876-2631-0704
    2023-01-05 21:59:18,065 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(F0:5E:CD:2A:BE:9D)
     rssi(-62)
     adv_type(BLEGapAdvType.connectable_undirected)
    2023-01-05 21:59:18,067 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(F0:5E:CD:2A:BD:FB)
     rssi(-37)
     adv_type(BLEGapAdvType.connectable_undirected)
    2023-01-05 21:59:18,068 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(F0:5E:CD:2A:BD:FB)
     rssi(-37)
     adv_type(None)
    Received advertisment report, address: 0xF05ECD2ABDFB, device_name: Safe-Device-BDFB
    2023-01-05 21:59:18,072 [12928/EventThread] evt> adv_report conn(65535)
     peer_addr(6B:D2:F5:FF:A3:EB)
     rssi(-36)
     adv_type(BLEGapAdvType.non_connectable_undirected)
    New connection: 0
    2023-01-05 21:59:18,168 [12928/EventThread] evt> sec_request conn(0)
     bond(1)
     mitm(1)
     lesc(1)
     keypress(0)
    2023-01-05 21:59:18,229 [12928/EventThread] evt> on_gattc_evt_prim_srvc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Service uuid(0x1800) start handle(1) end handle(11)
     Service uuid(0x1801) start handle(12) end handle(12)
     Service uuid(0x180A) start handle(13) end handle(31)
    2023-01-05 21:59:18,289 [12928/EventThread] evt> on_gattc_evt_prim_srvc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Service uuid(0xFFF0) start handle(32) end handle(65535)
    2023-01-05 21:59:18,348 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
     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(2) value handle(3)
     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(4) value handle(5)
     Characteristic uuid(0x2A04) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(6) value handle(7)
    2023-01-05 21:59:18,408 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Characteristic uuid(0x2AA6) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(8) value handle(9)
     Characteristic uuid(0x2AC9) 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)
    2023-01-05 21:59:18,468 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(0)
    
    2023-01-05 21:59:18,528 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A00) handle(3)
    2023-01-05 21:59:18,588 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A01) handle(5)
    2023-01-05 21:59:18,648 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A04) handle(7)
    2023-01-05 21:59:18,708 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2AA6) handle(9)
    2023-01-05 21:59:18,768 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2AC9) handle(11)
    2023-01-05 21:59:18,828 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(0)
    
    2023-01-05 21:59:18,888 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Characteristic uuid(0x2A23) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(14) value handle(15)
     Characteristic uuid(0x2A24) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(16) value handle(17)
     Characteristic uuid(0x2A25) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(18) value handle(19)
    2023-01-05 21:59:18,948 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Characteristic uuid(0x2A26) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(20) value handle(21)
     Characteristic uuid(0x2A27) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(22) value handle(23)
     Characteristic uuid(0x2A28) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(24) value handle(25)
    2023-01-05 21:59:19,009 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Characteristic uuid(0x2A29) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(26) value handle(27)
     Characteristic uuid(0x2A2A) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(28) value handle(29)
     Characteristic uuid(0x2A50) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(30) value handle(31)
    2023-01-05 21:59:19,068 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(0)
    
    2023-01-05 21:59:19,128 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A23) handle(15)
    2023-01-05 21:59:19,188 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A24) handle(17)
    2023-01-05 21:59:19,278 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A25) handle(19)
    2023-01-05 21:59:19,338 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A26) handle(21)
    2023-01-05 21:59:19,398 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A27) handle(23)
    2023-01-05 21:59:19,457 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A28) handle(25)
    2023-01-05 21:59:19,548 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A29) handle(27)
    2023-01-05 21:59:19,608 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A2A) handle(29)
    2023-01-05 21:59:19,668 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0x2A50) handle(31)
    2023-01-05 21:59:19,729 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Characteristic uuid(0xFFF1) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=1, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(33) value handle(34)
     Characteristic uuid(0xFFF2) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(36) value handle(37)
     Characteristic uuid(0xFFF3) properties(BLECharProperties(broadcast=0, read=0, write_wo_resp=0, write=1, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(39) value handle(40)
    2023-01-05 21:59:19,789 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Characteristic uuid(0xFFF4) properties(BLECharProperties(broadcast=0, read=0, write_wo_resp=0, write=0, notify=1, indicate=0, auth_signed_wr=0)) declaration handle(42) value handle(43)
     Characteristic uuid(0xFFF5) properties(BLECharProperties(broadcast=0, read=1, write_wo_resp=0, write=0, notify=0, indicate=0, auth_signed_wr=0)) declaration handle(46) value handle(47)
     Characteristic uuid(0xFFF6) properties(BLECharProperties(broadcast=0, read=0, write_wo_resp=0, write=0, notify=1, indicate=0, auth_signed_wr=0)) declaration handle(49) value handle(50)
    2023-01-05 21:59:19,848 [12928/EventThread] evt> on_gattc_evt_char_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(0)
    
    2023-01-05 21:59:19,908 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0xFFF1) handle(34)
     Descriptor uuid(0x2901) handle(35)
    2023-01-05 21:59:19,968 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0xFFF2) handle(37)
     Descriptor uuid(0x2901) handle(38)
    2023-01-05 21:59:20,029 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0xFFF3) handle(40)
     Descriptor uuid(0x2901) handle(41)
    2023-01-05 21:59:20,089 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0xFFF4) handle(43)
     Descriptor uuid(0x2902 (Standard.cccd)) handle(44)
     Descriptor uuid(0x2901) handle(45)
    2023-01-05 21:59:20,148 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0xFFF5) handle(47)
     Descriptor uuid(0x2901) handle(48)
    2023-01-05 21:59:20,208 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.success) conn(0)
     Descriptor uuid(0xFFF6) handle(50)
     Descriptor uuid(0x2902 (Standard.cccd)) handle(51)
     Descriptor uuid(0x2901) handle(52)
    2023-01-05 21:59:20,268 [12928/EventThread] evt> on_gattc_evt_desc_disc_rsp status(BLEGattStatusCode.attribute_not_found) conn(0)
    
    2023-01-05 21:59:20,329 [12928/EventThread] evt> on_gattc_evt_write_rsp conn(0)
     status(BLEGattStatusCode.success)
     error_handle(0)
     attr_handle(51)
     write_op(BLEGattWriteOperation.write_req)
     offset(0)
     data([])
    ATT MTU updated to 247
    Disconnected: 0 BLEHci.connection_timeout

  • Have you enabled notifications with enable_notification()? If not, you will not get any notifications, and on_notification() will never be called

  • I want to record the notify in 0XFFF6 like image below

    but now I have enable notification(),still can't received the data.

  • I see from the log that you find that characteristic during service discovery. Then the next is to enable notifications using enable_notification with that UUID.

  •     def connect_and_discover(self):
            scan_duration = 5
            params = BLEGapScanParams(interval_ms=200, window_ms=150, timeout_s=scan_duration)
            message = "We are connected!\r\n"
    
            self.adapter.driver.ble_gap_scan_start(scan_params=params)
    
            try:
                new_conn = self.conn_q.get(timeout=scan_duration)
                self.adapter.service_discovery(new_conn)
                self.adapter.enable_notification(new_conn, self.nus_tx)
    
                return new_conn
            except Empty:
                print("No device advertising with name {TARGET_DEV_NAME} found.")
                return None

    Yes ,I have enable notifications using enable_notification in the "def connect_and_discover(self):"

    Is  enable_notification() return anything? or how do I know this function is working? 

Reply
  •     def connect_and_discover(self):
            scan_duration = 5
            params = BLEGapScanParams(interval_ms=200, window_ms=150, timeout_s=scan_duration)
            message = "We are connected!\r\n"
    
            self.adapter.driver.ble_gap_scan_start(scan_params=params)
    
            try:
                new_conn = self.conn_q.get(timeout=scan_duration)
                self.adapter.service_discovery(new_conn)
                self.adapter.enable_notification(new_conn, self.nus_tx)
    
                return new_conn
            except Empty:
                print("No device advertising with name {TARGET_DEV_NAME} found.")
                return None

    Yes ,I have enable notifications using enable_notification in the "def connect_and_discover(self):"

    Is  enable_notification() return anything? or how do I know this function is working? 

Children
Related