无法使用C中API库头中定义的struct

时间:2016-10-22 23:53:03

标签: c linux struct bluetooth bluez

我正试图在Linux和C(非C ++)上搞乱蓝牙编程。

我需要使用标头中定义的结构;但每次我尝试声明我需要的特定结构的变量时,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;
}

请记住这是非工作/不完整的代码。我在网上使用的样本已经超过十年了,不再具有相关性,但无论如何我都试图让它工作。

1 个答案:

答案 0 :(得分:1)

我发现了问题。没有什么不对,把它归结为匆匆而没有看到明显的。

这一行:

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()

因为没必要。