c - 无法使用 C 中 API 库 header 中定义的结构

标签 c linux struct bluetooth bluez

我正在尝试在 Linux 和 C(不是 C++)中进行蓝牙编程。

我需要使用在 header 中定义的结构;但是每次我尝试声明我需要的特定结构的变量时,Eclipse 都会提示无法解析类型。

这是为什么?我该如何解决?

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h> //Header where needed struct is defined
#include <bluetooth/hci_lib.h>

extern struct hci_dev_info; //Just something I tried. Eclipse displays the definition for this struct (the one I need) when moused over. It doesn't seem to know what the struct is within main()

void log_me (FILE * file, char *error_message, int error_code)
{
    fprintf(file, "%s", error_message);
    exit(error_code);
}


int main (void)
{
        FILE *bluetooth_info_file;
        //On line 88 of hci.i the device flags are actually defined

        int devices [16];   //Will store the device numbers
        int device_count;
        int fd;             //File descriptor
        int return_value;
        hci_dev_info *da; //bd_addr (bluetooth address struct)

        bluetooth_info_file = fopen("BluetoothAddresses+Names.txt", "wa");
        if(bluetooth_info_file != NULL)
        {
            fprintf(bluetooth_info_file, "Bluetooth adapters on system:\n");
        } else
        {
            printf("Error opening file!");
            exit(-1);
        }
        device_count = hci_get_devs(devices);

        if (device_count < 0) {
            log_me(bluetooth_info_file, "Unable to get device list!", -1);
        }

        if(device_count == 0)
        {
            log_me(bluetooth_info_file, "No devices exist on the system.", 0);
        }

        for(int i = 0; i < device_count; i++)
        {
            fd = hci_open_id(devices[i]);
            if(fd < 0)
            {
                log_me(bluetooth_info_file, "Unable to open device!", -1);
            }

            return_value = gci_get_attr(fd, &da);

            if(return_value < 0)
            {
                log_me(bluetooth_info_file, "Unable to get device attributes!!", -1);
            }

            fprintf(bluetooth_info_file, "Bluetooth device name: %s\t Address: %s\n", da->name, bda2str(da->bda));


            /*This is the proper spelling and name of the HCI_STATE_UP as mentioned in the sample.
HCI_DEV_UP (and other device events) are defined on line 1474 in hci.h
*/
            if(!(/*Incomplete code*/
                    ))
                log_me(bluetooth_info_file, "Device is down.\n", 0);
            }

            return_value = HCI_WriteScanEnable(fd, 0x02);
            if(return_value)
            {
                log_me("Unable to set scan mode!");
            }
        }


        free( ii );
        close( sock );
        fclose(bluetooth_info_file);
        return 0;
}

请记住,这是无效/不完整的代码。我在网上使用的样本已有十多年历史,不再相关,但我正努力让它发挥作用。

最佳答案

我发现了问题。没有任何问题,将其归咎于仓促和未能看到显而易见的事情。

这一行:

hci_dev_info *da

应该是:

struct hci_dev_info *da

如果您这样做,所有结构成员都可以访问。

同时删除行:

extern struct hci_dev_info; //Just something I tried. Eclipse displays the definition for this struct (the one I need) when moused over. It doesn't seem to know what the struct is within main()

因为这是不必要的。

关于c - 无法使用 C 中 API 库 header 中定义的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40198194/

相关文章:

c - 通过 GDAL GDALAutoCreateWarpedVRT 在 C 中重新投影

c - 从文件中读取 2 个字节并转换为 int 会给出错误的输出

arrays - 将制表符分隔的字符串拆分为 bash 中的数组

c - 使用 winsock 发送结构

clock_gettime 返回一些非常大的值

python 2.7 : Using subprocess and a For Loop to output results

c++ - 如何让我的用户级应用程序通过管道与根级应用程序通信?

json - 类型 '[String:Any]' 在 Swift 中没有下标成员

C - 如何使用指向结构的指针复制结构

c - 网络设备中断后谁要求调度?