forked from balle/bluetooth-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfcomm-server.c
executable file
·57 lines (44 loc) · 1.1 KB
/
rfcomm-server.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
BlueZ example code to build an rfcomm server.
This code just creates a socket and accepts
connections from a remote bluetooth device.
Programmed by Bastian Ballmann
http://www.geektown.de
Compile with gcc -lbluetooth <executable> <source>
*/
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#define CHANNEL 4
#define QUEUE 10
int main(void)
{
int sock, client, alen;
struct sockaddr_rc addr;
if( (sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0)
{
perror("socket");
exit(1);
}
addr.rc_family = AF_BLUETOOTH;
bacpy(&addr.rc_bdaddr, BDADDR_ANY);
addr.rc_channel = htobs(CHANNEL);
alen = sizeof(addr);
if(bind(sock, (struct sockaddr *)&addr, alen) < 0)
{
perror("bind");
exit(1);
}
listen(sock,QUEUE);
printf("Waiting for connections...\n\n");
while(client = accept(sock, (struct sockaddr *)&addr, &alen))
{
printf("Got a connection attempt!\n");
close(client);
}
close(sock);
return 0;
}