Created
May 26, 2018 09:14
-
-
Save parthitce/9e8be3abb5a3ca76b8a1f8b8d6d36d12 to your computer and use it in GitHub Desktop.
hci_list.c: Finds the list of available Bluetooth controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* hci_list.c : Find the list of bluetooth controller present as | |
* - If more the one controller found, prints the UP-RUNNING | |
* - If none is enabled, it prints the first found controller | |
* - If none found, prints nothing | |
* Compile: gcc -o ./hci_list ./hci_list.c -lbluetooth | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <bluetooth/bluetooth.h> | |
#include <bluetooth/hci.h> | |
#include <bluetooth/hci_lib.h> | |
static int hci_devlist(struct hci_dev_info **di, int *num) | |
{ | |
int i; | |
if((*di = malloc(HCI_MAX_DEV * sizeof(**di))) == NULL) | |
return -1; | |
for(i = *num = 0; i < HCI_MAX_DEV; i++) | |
if (hci_devinfo(i, &(*di)[*num]) == 0) | |
(*num)++; | |
return 0; | |
} | |
int main(void) | |
{ | |
int i; | |
int hci_devs_num; | |
struct hci_dev_info *hci_devs; | |
if(hci_devlist(&hci_devs, &hci_devs_num)) { | |
printf("Couldn't enumerate HCI devices: %s", strerror(errno)); | |
return 1; | |
} | |
for(i = 0; i < hci_devs_num; i++) | |
if(i == 0 || hci_test_bit(HCI_UP, &hci_devs[i].flags)) | |
printf("HCI Name : %s\nAddress : %s\n", hci_devs[i].name, batostr(&hci_devs[i].bdaddr)); | |
free(hci_devs); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It has a problem with
batostr
which one outputs controller address in reverse order. So, main should be edited as
to print controller address in right manner