-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhciscan.c
More file actions
55 lines (48 loc) · 1.25 KB
/
Copy pathhciscan.c
File metadata and controls
55 lines (48 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* hciscan.c
*
* Created on: 6 марта 2016 г.
* Author: bakulev
*/
#include <stdlib.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include "hciscan.h"
void hciscan(bdaddr_t *target_bdaddr)
{
inquiry_info *devices = NULL;
int max_rsp, num_rsp;
int adapter_id, sock, len, flags;
int i;
char addr[19] = {0};
char name[248] = {0};
adapter_id = hci_get_route(NULL);
sock = hci_open_dev(adapter_id);
if (adapter_id < 0 || sock < 0)
{
perror("opening socket");
exit(EXIT_FAILURE);
}
len = 8;
max_rsp = 255;
flags = IREQ_CACHE_FLUSH;
devices =(inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
num_rsp = hci_inquiry(adapter_id, len, max_rsp, NULL, &devices, flags);
if (num_rsp < 0) perror("hci_inquiry");
printf("Select device to send data:\n");
for (i = 0; i < num_rsp; i++)
{
ba2str(&(devices + i)->bdaddr, addr);
memset(name, 0, sizeof(name));
if (0 != hci_read_remote_name(sock, &(devices + i)->bdaddr, sizeof(name), name, 0))
{
// allocate a socket
strcpy(name, "[unknown]");
}
printf("%d: %s %s\n", i, addr, name);
}
do scanf("%d", &i); while (i < 0 || i > num_rsp);
if (i >= 0 && i < num_rsp) bacpy(target_bdaddr, &(devices + i)->bdaddr);
free(devices);
}