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 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.

  • Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    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
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    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? 

  • Yes, it does return a status. You can see that from looking at the implementation. What is self.nus_tx? Is it the right UUID?

  • Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    def __init__(self, adapter):
    super(NusCollector, self).__init__()
    self.first = 1
    self.adapter = adapter
    self.conn_q = Queue()
    self.adapter.observer_register(self)
    self.adapter.driver.observer_register(self)
    self.adapter.default_mtu = 247
    self.nus_base = BLEUUIDBase([
    0x00, 0x00, 0xff, 0xf0, 0x00 ,0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb
    ])
    self.nus_tx = BLEUUID(0xfff6, self.nus_base)
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    self.nus_tx is the UUID of 0XFFF6 which is the notify one

Reply
  • Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    def __init__(self, adapter):
    super(NusCollector, self).__init__()
    self.first = 1
    self.adapter = adapter
    self.conn_q = Queue()
    self.adapter.observer_register(self)
    self.adapter.driver.observer_register(self)
    self.adapter.default_mtu = 247
    self.nus_base = BLEUUIDBase([
    0x00, 0x00, 0xff, 0xf0, 0x00 ,0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb
    ])
    self.nus_tx = BLEUUID(0xfff6, self.nus_base)
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    self.nus_tx is the UUID of 0XFFF6 which is the notify one

Children
  • Is the UUID Base right? In the screenshot it looks like you are using only 16 bit UUIDs, but here you are adding a base (which is probably what you should do as this is a custom service). But in any case, in order to work, this needs to match.

  • self.nus_base = BLEUUIDBase([
    0x00, 0x00, 0xff, 0xf0, 0x00 ,0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb
    ])
    self.nus_tx = BLEUUID(0xfff6, self.nus_base)
    I think BLEUUID is to mix "0xfff6" and "self.nus_base",so self.nus_tx become[0x00, 0x00, 0xff, 0xf6, 0x00 ,0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb]?
    I also got the return form enable_notification() ,and get "None" so i think maybe my UUID is wrong
    this is the UUID I want to receive data
  • Hello is there anything I can do for it?

  • I don't immediately see an issue here. I suggest you take a look at this post, which includes a working script that use NUS, and enables notifications (obviously you need to change the UUID and UUID base for your custom service).

  • OK thank you for reply