我正在尝试在 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/