|
diff --git a/README.md b/README.md
|
| ... |
| 36 |
| [zig-embed](./zig-embed) | zig-0.13.0 | Embedding external resources in compiled binary. | |
36 |
| [zig-embed](./zig-embed) | zig-0.13.0 | Embedding external resources in compiled binary. | |
| 37 |
| [zig-struct-bin](./zig-struct-bin) | zig-0.13.0 | Save a struct into binary file and then reading it back. | |
37 |
| [zig-struct-bin](./zig-struct-bin) | zig-0.13.0 | Save a struct into binary file and then reading it back. | |
| 38 |
| [zig-elf](./zig-elf) | zig-0.14.0 | Read execution header of Elf64 format in Zig. | |
38 |
| [zig-elf](./zig-elf) | zig-0.14.0 | Read execution header of Elf64 format in Zig. | |
|
|
39 |
| [c-bluetooth](./c-bluetooth) | clang-17 | Scans for all Bluetooth devices. | |
| 39 |
|
40 |
|
| 40 |
## License |
41 |
## License |
| 41 |
|
42 |
|
| ... |
|
diff --git a/c-bluetooth/connect.c b/c-bluetooth/connect.c
|
|
|
1 |
#include <stdio.h> |
|
|
2 |
#include <stdlib.h> |
|
|
3 |
#include <unistd.h> |
|
|
4 |
#include <sys/socket.h> |
|
|
5 |
#include <bluetooth/bluetooth.h> |
|
|
6 |
#include <bluetooth/hci.h> |
|
|
7 |
#include <bluetooth/hci_lib.h> |
|
|
8 |
|
|
|
9 |
int main(int argc, char **argv) { |
|
|
10 |
inquiry_info *ii = NULL; |
|
|
11 |
int max_rsp, num_rsp; |
|
|
12 |
int dev_id, sock, len, flags; |
|
|
13 |
int i; |
|
|
14 |
char addr[19] = { 0 }; |
|
|
15 |
char name[248] = { 0 }; |
|
|
16 |
|
|
|
17 |
dev_id = hci_get_route(NULL); |
|
|
18 |
sock = hci_open_dev( dev_id ); |
|
|
19 |
if (dev_id < 0 || sock < 0) { |
|
|
20 |
perror("opening socket"); |
|
|
21 |
exit(1); |
|
|
22 |
} |
|
|
23 |
|
|
|
24 |
len = 8; |
|
|
25 |
max_rsp = 255; |
|
|
26 |
flags = IREQ_CACHE_FLUSH; |
|
|
27 |
ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info)); |
|
|
28 |
|
|
|
29 |
num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); |
|
|
30 |
if( num_rsp < 0 ) { |
|
|
31 |
perror("hci_inquiry"); |
|
|
32 |
} |
|
|
33 |
|
|
|
34 |
for (i = 0; i < num_rsp; i++) { |
|
|
35 |
ba2str(&(ii+i)->bdaddr, addr); |
|
|
36 |
memset(name, 0, sizeof(name)); |
|
|
37 |
if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0) { |
|
|
38 |
strcpy(name, "[unknown]"); |
|
|
39 |
} |
|
|
40 |
printf("%s %s\n", addr, name); |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
free(ii); |
|
|
44 |
close(sock); |
|
|
45 |
return 0; |
|
|
46 |
} |
|
|
47 |
|
|
diff --git a/c-bluetooth/scan.c b/c-bluetooth/scan.c
|
|
|
1 |
#include <stdio.h> |
|
|
2 |
#include <stdlib.h> |
|
|
3 |
#include <unistd.h> |
|
|
4 |
#include <sys/socket.h> |
|
|
5 |
#include <bluetooth/bluetooth.h> |
|
|
6 |
#include <bluetooth/hci.h> |
|
|
7 |
#include <bluetooth/hci_lib.h> |
|
|
8 |
|
|
|
9 |
int main(int argc, char **argv) { |
|
|
10 |
inquiry_info *ii = NULL; |
|
|
11 |
int max_rsp, num_rsp; |
|
|
12 |
int dev_id, sock, len, flags; |
|
|
13 |
int i; |
|
|
14 |
char addr[19] = { 0 }; |
|
|
15 |
char name[248] = { 0 }; |
|
|
16 |
|
|
|
17 |
dev_id = hci_get_route(NULL); |
|
|
18 |
sock = hci_open_dev( dev_id ); |
|
|
19 |
if (dev_id < 0 || sock < 0) { |
|
|
20 |
perror("opening socket"); |
|
|
21 |
exit(1); |
|
|
22 |
} |
|
|
23 |
|
|
|
24 |
len = 8; |
|
|
25 |
max_rsp = 255; |
|
|
26 |
flags = IREQ_CACHE_FLUSH; |
|
|
27 |
ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info)); |
|
|
28 |
|
|
|
29 |
num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); |
|
|
30 |
if(num_rsp < 0) { |
|
|
31 |
perror("hci_inquiry"); |
|
|
32 |
} |
|
|
33 |
|
|
|
34 |
for (i = 0; i < num_rsp; i++) { |
|
|
35 |
ba2str(&(ii+i)->bdaddr, addr); |
|
|
36 |
memset(name, 0, sizeof(name)); |
|
|
37 |
if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0) { |
|
|
38 |
strcpy(name, "[unknown]"); |
|
|
39 |
} |
|
|
40 |
printf("%s %s\n", addr, name); |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
free(ii); |
|
|
44 |
close(sock); |
|
|
45 |
return 0; |
|
|
46 |
} |
|
|
47 |
|