Skip to content

Commit 73b9e59

Browse files
committed
HCI scan added
1 parent a47ff44 commit 73b9e59

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ INSTALL = /usr/local/bin
77

88
#FLAGS = $(CFLAGS) -Wall -g -DDEBUG -DDEBUG_INFO -DDEBUG_MAIN -DDEBUG_BT -DDEBUG_NET -DDEBUG_BT_DISCOVER -DDEBUG_BTNET -DDEBUG_NET_INFO
99
#FLAGS = $(CFLAGS) -Wall -g -DDEBUG -DDEBUG_INFO -DDEBUG_MAIN -DDEBUG_BT -DDEBUG_NET -DDEBUG_BT_DISCOVER -DDEBUG_BTNET -DDEBUG_BTNET_INFO -DDEBUG_BT_INFO -DDEBUG_BTNET_INFO2
10-
FLAGS = $(CFLAGS) -Wall
10+
FLAGS = $(CFLAGS) -Wall -g -DDEBUG -DDEBUG_INFO -DDEBUG_MAIN -DDEBUG_BT -DDEBUG_NET -DDEBUG_BT_DISCOVER -DDEBUG_BTNET -DDEBUG_BTNET_INFO -DDEBUG_BT_INFO -DDEBUG_BTNET_INFO2
1111

1212
OBJ = main.o rfcomm-server.o rfcomm-client.o rfcomm-register.o
1313
EXEC = rfcomm

rfcomm-client.c

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <sys/socket.h>
44
#include <bluetooth/bluetooth.h>
55
#include <bluetooth/rfcomm.h>
6+
#include <bluetooth/hci.h>
7+
#include <bluetooth/hci_lib.h>
68
#include <errno.h>
79
#include "rfcomm-client.h"
810

@@ -26,9 +28,13 @@ int rfcomm_client(void)
2628
struct sockaddr_rc addr = { 0 };
2729
int s, status;
2830
uint8_t port = 1;
29-
char dest[18] = "C0:18:85:DC:BA:61";
31+
char *dest = {0}; //"C0:18:85:DC:BA:61";
3032
int res;
3133

34+
printf("Start scanning.\n");
35+
dest = (char*)malloc(18 * sizeof(char));
36+
dest[17] = 0;
37+
dest = hciscan(dest);
3238
// allocate a socket
3339
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
3440

@@ -41,7 +47,8 @@ int rfcomm_client(void)
4147

4248
// connect to server
4349
status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
44-
printf("Connected.\n");
50+
ba2str(&addr.rc_bdaddr, dest);
51+
printf("Connected to address %s.\n", dest);
4552

4653
// send a message
4754
if( status == 0 ) {
@@ -54,3 +61,50 @@ int rfcomm_client(void)
5461
close(s);
5562
return 0;
5663
}
64+
65+
char* hciscan(char *dest_addr)
66+
{
67+
inquiry_info *devices = NULL;
68+
int max_rsp, num_rsp;
69+
int adapter_id, sock, len, flags;
70+
int i;
71+
char addr[19] = {0};
72+
char name[248] = {0};
73+
adapter_id = hci_get_route(NULL);
74+
sock = hci_open_dev(adapter_id);
75+
if (adapter_id < 0 || sock < 0)
76+
{
77+
perror("opening socket");
78+
exit(1);
79+
}
80+
81+
dest_addr[0] = 0;
82+
len = 8;
83+
max_rsp = 255;
84+
flags = IREQ_CACHE_FLUSH;
85+
devices =(inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
86+
87+
num_rsp = hci_inquiry(adapter_id, len, max_rsp, NULL, &devices, flags);
88+
if (num_rsp < 0) perror("hci_inquiry");
89+
90+
while(dest_addr[0] == 0)
91+
{
92+
printf("Select device to send data:");
93+
for (i = 0; i < num_rsp; i++)
94+
{
95+
ba2str(&(devices + i)->bdaddr, addr);
96+
memset(name, 0, sizeof(name));
97+
if (0 != hci_read_remote_name(sock, &(devices + i)->bdaddr, sizeof(name), name, 0))
98+
{
99+
// allocate a socket
100+
strcpy(name, "[unknown]");
101+
}
102+
printf("%d: %s %s\n", i, addr, name);
103+
}
104+
scanf("%d", &i);
105+
if (i >= 0 && i < num_rsp) strncpy(dest_addr, addr, 18);
106+
}
107+
108+
free(devices);
109+
return dest_addr;
110+
}

rfcomm-client.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
int dynamic_bind_rc(int sock, struct sockaddr_rc *sockaddr, uint8_t *port);
55

66
int rfcomm_client(void);
7+
8+
char* hciscan(char *dest_addr);

0 commit comments

Comments
 (0)