调用hci_inquiry时,如何判断及控制响应类型为inquiry_info等三种类型?
Alright, let's break this down clearly—working with Bluetooth HCI inquiry APIs can feel opaque at first, but once you grasp how the flags parameter drives the response structure, it's straightforward.
How to Identify the Current Response Type
First off: the response type returned by hci_inquiry is directly tied to the flags value you passed when calling the function. That's the most reliable way to know what you're dealing with. But if you're debugging code you didn't write, or need to validate dynamically, here's how to check:
Check the
flagsparameter used in the call
This is the gold standard. The flags explicitly tell the Bluetooth stack what kind of data to return:- No special flags (or
0): You're getting the baseinquiry_infostructure. HCIINQUIRYRSSIflag present: The response isinquiry_info_with_rssi.HCIINQUIRYEXTENDEDflag present: You're receivinginquiry_info_with_rssi_and_pscan_mode.
- No special flags (or
Inspect the memory structure (for edge cases)
If you don't have access to the originalflagsvalue, you can compare the size of the returned structure against the known sizes of each type (note: sizes may vary slightly by platform, so use definitions from your Bluetooth stack headers likebluetooth.h):sizeof(inquiry_info): Base structure (contains BD address, scan modes, clock offset).sizeof(inquiry_info_with_rssi): Base size + 1 byte for RSSI value.sizeof(inquiry_info_with_rssi_and_pscan_mode): RSSI structure size + additional fields for page scan mode and extended clock offset data.
How to Force a Specific Response Type
You control the response type entirely via the flags parameter in the hci_inquiry call. Here's how to target each structure:
1. Return inquiry_info (basic device info)
Call hci_inquiry with no special flags (or 0). This returns the minimal set of data about nearby Bluetooth devices:
#include <bluetooth/bluetooth.h> #include <bluetooth/hci.h> #include <bluetooth/hci_lib.h> int main() { inquiry_info *ii; int num_rsp = hci_inquiry(0, 8, 10, NULL, &ii, 0); if (num_rsp < 0) { perror("hci_inquiry failed"); return 1; } char addr[19] = {0}; for (int i = 0; i < num_rsp; i++) { ba2str(&ii[i].bdaddr, addr); printf("Device: %s\n", addr); } free(ii); return 0; }
2. Return inquiry_info_with_rssi (basic info + signal strength)
Add the HCIINQUIRYRSSI flag to the call. You'll need to cast the returned pointer to the extended structure to access the RSSI value:
inquiry_info_with_rssi *ii_rssi; int num_rsp = hci_inquiry(0, 8, 10, NULL, (inquiry_info **)&ii_rssi, HCIINQUIRYRSSI); if (num_rsp < 0) { perror("hci_inquiry failed"); return 1; } char addr[19] = {0}; for (int i = 0; i < num_rsp; i++) { ba2str(&ii_rssi[i].info.bdaddr, addr); printf("Device: %s | RSSI: %d dBm\n", addr, ii_rssi[i].rssi); } free(ii_rssi);
3. Return inquiry_info_with_rssi_and_pscan_mode (full extended data)
Use the HCIINQUIRYEXTENDED flag. This returns the most detailed set of data, including page scan mode and extended clock offset:
inquiry_info_with_rssi_and_pscan_mode *ii_ext; int num_rsp = hci_inquiry(0, 8, 10, NULL, (inquiry_info **)&ii_ext, HCIINQUIRYEXTENDED); if (num_rsp < 0) { perror("hci_inquiry failed"); return 1; } char addr[19] = {0}; for (int i = 0; i < num_rsp; i++) { ba2str(&ii_ext[i].rssi_info.info.bdaddr, addr); printf("Device: %s | RSSI: %d dBm | Page Scan Mode: %d\n", addr, ii_ext[i].rssi_info.rssi, ii_ext[i].pscan_mode); } free(ii_ext);
Important Notes
- Adapter Compatibility: Not all Bluetooth adapters support extended inquiries (the
HCIINQUIRYEXTENDEDflag). You can check if your adapter supports this by callinghci_read_local_featuresand verifying the relevant feature bit. - Memory Management: Always free the returned pointer after use—
hci_inquiryallocates memory dynamically for the response structures.
Hope that clears up how to control and identify the response types from hci_inquiry! If you hit adapter-specific issues or need more context, feel free to follow up.
内容的提问来源于stack exchange,提问作者Georg Bisseling

