Confusion around bt_le_scan_start callbacks
Jason Bens <jason.bens@...>
Hi,
It seems as though there are two ways to invoke a callback when a BLE device has been scanned. I can register a callback with bt_le_scan_cb_register, where my callback is the .recv field of the bt_le_scan_cb struct, or I can pass a callback of type bt_le_scan_cb_t to bt_le_scan_start. The parameters passed to these two callbacks are different, however. For bt_le_scan_cb, my callback is passed a struct bt_le_scan_recv_info and a net_buf, whereas a callback of type bt_le_scan_cb_t is passed the address, rssi, advertiser type, and the same net_buf. The first three parameters are a subset of the information in bt_le_scan_recv_info. I can extract the whole bt_le_scan_recv_info struct from the passed net_buf, no problem, but I’m curious why there are two different declarations for this? When should the bt_le_scan_cb_register route be preferred, and when should the callback be passed directly to bt_le_scan_start?
Thanks,
|
|
Hi Jason,
Which API you should pick depends entirely on your use case. There’s no single right answer. The bt_le_scan_cb_register() API was added much later than the original bt_le_scan_start() API, in order to allow applications to register multiple callbacks and to allow extending the information passed to the callback in the future. The original API was kept untouched to retain backwards compatibility, and since you still need some way to start scanning (you can pass NULL as the callback to bt_le_scan_start(), if you want).
Johan
From:
users@... <users@...> on behalf of Jason Bens <jason.bens@...> Hi,
It seems as though there are two ways to invoke a callback when a BLE device has been scanned. I can register a callback with bt_le_scan_cb_register, where my callback is the .recv field of the bt_le_scan_cb struct, or I can pass a callback of type bt_le_scan_cb_t to bt_le_scan_start. The parameters passed to these two callbacks are different, however. For bt_le_scan_cb, my callback is passed a struct bt_le_scan_recv_info and a net_buf, whereas a callback of type bt_le_scan_cb_t is passed the address, rssi, advertiser type, and the same net_buf. The first three parameters are a subset of the information in bt_le_scan_recv_info. I can extract the whole bt_le_scan_recv_info struct from the passed net_buf, no problem, but I’m curious why there are two different declarations for this? When should the bt_le_scan_cb_register route be preferred, and when should the callback be passed directly to bt_le_scan_start?
Thanks,
|
|
Jason Bens <jason.bens@...>
Sounds good. Thanks for the clarification, Johan.
From: users@... <users@...>
On Behalf Of Johan Hedberg
Sent: January 10, 2022 2:45 PM To: Jason Bens <Jason.Bens@...>; users@... Subject: Re: [Zephyr-users] Confusion around bt_le_scan_start callbacks
External Email: Hi Jason,
Which API you should pick depends entirely on your use case. There’s no single right answer. The bt_le_scan_cb_register() API was added much later than the original bt_le_scan_start() API, in order to allow applications to register multiple callbacks and to allow extending the information passed to the callback in the future. The original API was kept untouched to retain backwards compatibility, and since you still need some way to start scanning (you can pass NULL as the callback to bt_le_scan_start(), if you want).
Johan
From:
users@... <users@...> on behalf of Jason Bens <jason.bens@...> Hi,
It seems as though there are two ways to invoke a callback when a BLE device has been scanned. I can register a callback with bt_le_scan_cb_register, where my callback is the .recv field of the bt_le_scan_cb struct, or I can pass a callback of type bt_le_scan_cb_t to bt_le_scan_start. The parameters passed to these two callbacks are different, however. For bt_le_scan_cb, my callback is passed a struct bt_le_scan_recv_info and a net_buf, whereas a callback of type bt_le_scan_cb_t is passed the address, rssi, advertiser type, and the same net_buf. The first three parameters are a subset of the information in bt_le_scan_recv_info. I can extract the whole bt_le_scan_recv_info struct from the passed net_buf, no problem, but I’m curious why there are two different declarations for this? When should the bt_le_scan_cb_register route be preferred, and when should the callback be passed directly to bt_le_scan_start?
Thanks,
|
|