diff options
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | c-bluetooth/Makefile | 6 | ||||
| -rw-r--r-- | c-bluetooth/connect.c | 47 | ||||
| -rw-r--r-- | c-bluetooth/scan.c | 47 | ||||
| -rw-r--r-- | shell.nix | 1 |
5 files changed, 102 insertions, 0 deletions
@@ -36,6 +36,7 @@ running it. | [zig-embed](./zig-embed) | zig-0.13.0 | Embedding external resources in compiled binary. | | [zig-struct-bin](./zig-struct-bin) | zig-0.13.0 | Save a struct into binary file and then reading it back. | | [zig-elf](./zig-elf) | zig-0.14.0 | Read execution header of Elf64 format in Zig. | +| [c-bluetooth](./c-bluetooth) | clang-17 | Scans for all Bluetooth devices. | ## License diff --git a/c-bluetooth/Makefile b/c-bluetooth/Makefile new file mode 100644 index 0000000..c11dbcf --- /dev/null +++ b/c-bluetooth/Makefile @@ -0,0 +1,6 @@ +scan: scan.c + cc -o scan scan.c -lbluetooth + +provision: + sudo apt-get install libbluetooth-dev + diff --git a/c-bluetooth/connect.c b/c-bluetooth/connect.c new file mode 100644 index 0000000..17fe433 --- /dev/null +++ b/c-bluetooth/connect.c @@ -0,0 +1,47 @@ +#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) { + inquiry_info *ii = NULL; + int max_rsp, num_rsp; + int dev_id, sock, len, flags; + int i; + char addr[19] = { 0 }; + char name[248] = { 0 }; + + dev_id = hci_get_route(NULL); + sock = hci_open_dev( dev_id ); + if (dev_id < 0 || sock < 0) { + perror("opening socket"); + exit(1); + } + + len = 8; + max_rsp = 255; + flags = IREQ_CACHE_FLUSH; + ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info)); + + num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); + if( num_rsp < 0 ) { + perror("hci_inquiry"); + } + + for (i = 0; i < num_rsp; i++) { + ba2str(&(ii+i)->bdaddr, addr); + memset(name, 0, sizeof(name)); + if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0) { + strcpy(name, "[unknown]"); + } + printf("%s %s\n", addr, name); + } + + free(ii); + close(sock); + return 0; +} + diff --git a/c-bluetooth/scan.c b/c-bluetooth/scan.c new file mode 100644 index 0000000..30cfe36 --- /dev/null +++ b/c-bluetooth/scan.c @@ -0,0 +1,47 @@ +#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) { + inquiry_info *ii = NULL; + int max_rsp, num_rsp; + int dev_id, sock, len, flags; + int i; + char addr[19] = { 0 }; + char name[248] = { 0 }; + + dev_id = hci_get_route(NULL); + sock = hci_open_dev( dev_id ); + if (dev_id < 0 || sock < 0) { + perror("opening socket"); + exit(1); + } + + len = 8; + max_rsp = 255; + flags = IREQ_CACHE_FLUSH; + ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info)); + + num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); + if(num_rsp < 0) { + perror("hci_inquiry"); + } + + for (i = 0; i < num_rsp; i++) { + ba2str(&(ii+i)->bdaddr, addr); + memset(name, 0, sizeof(name)); + if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0) { + strcpy(name, "[unknown]"); + } + printf("%s %s\n", addr, name); + } + + free(ii); + close(sock); + return 0; +} + @@ -15,6 +15,7 @@ lldb # Dev libraries and deps. + bluez xorg.libX11 xorg.libX11.dev ]; |
