我想通过bluez API在C中编写程序
我使用这个网站进行教程:
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int main(int argc, char **argv)
{
int dev_id = hci_get_route(NULL);
int res_scan=NULL;
int count;
inquiry_info *device_info=NULL;
res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
printf("%i\n",res_scan);
for(count = 0;count < res_scan;count++)
{
char *name;
printf("count Before : %i\n",count);
ba2str(&(device_info+count)->bdaddr,&name);
printf("count After : %i\n",count);
printf("%s\n",&name);
}
}
Run Code Online (Sandbox Code Playgroud)
和控制台:
2
count Before : 0
count After : 1111833143
00:17:EB:5D:1B:86
Run Code Online (Sandbox Code Playgroud)
为什么count
值后ba2str(&(device_info+count)->bdaddr,&name);
获得随机值?
在该来源我链接这个问题不会发生!?
代替
char *name;
...
printf("%s\n",&name);
Run Code Online (Sandbox Code Playgroud)
使用
char name[248] = { 0 };
...
printf("%s\n",name);
Run Code Online (Sandbox Code Playgroud)