-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrfcomm-server.c
More file actions
43 lines (35 loc) · 1.14 KB
/
rfcomm-server.c
File metadata and controls
43 lines (35 loc) · 1.14 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
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
int main (int argc, char** argv)
{
struct sockaddr_rc loc_addr = { 0 } , rem_addr = { 0 } ;
char buf [1024] = { 0 } ;
int s, client, bytes_read ;
unsigned int opt = sizeof (rem_addr);
// allocate socket
s = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// bind socket to port 1 of the first available bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH ;
loc_addr.rc_bdaddr = *BDADDR_ANY ;
loc_addr.rc_channel =1;
bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
// put socket into listening mode
listen(s, 1);
// accept one connection
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
ba2str(&rem_addr.rc_bdaddr, buf);
fprintf(stderr, "accepted connection from %s\n", buf);
memset ( buf ,0, sizeof ( buf));
// read data from the client
bytes_read = recv(client, buf, sizeof(buf), 0);
if (bytes_read > 0) {
printf("received [%s]\n", buf);
}
// close connection
close(client);
close(s);
return 0;
}