You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用hci_inquiry时,如何判断及控制响应类型为inquiry_info等三种类型?

Working with hci_inquiry: Identifying Response Types & Controlling Output

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 flags parameter 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 base inquiry_info structure.
    • HCIINQUIRYRSSI flag present: The response is inquiry_info_with_rssi.
    • HCIINQUIRYEXTENDED flag present: You're receiving inquiry_info_with_rssi_and_pscan_mode.
  • Inspect the memory structure (for edge cases)
    If you don't have access to the original flags value, 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 like bluetooth.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 HCIINQUIRYEXTENDED flag). You can check if your adapter supports this by calling hci_read_local_features and verifying the relevant feature bit.
  • Memory Management: Always free the returned pointer after use—hci_inquiry allocates 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 09:57:42