-
Notifications
You must be signed in to change notification settings - Fork 1
/
l2cap-server-page80.c
48 lines (35 loc) · 1.09 KB
/
l2cap-server-page80.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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>
int main(){
struct sockaddr_l2 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_SEQPACKET, BTPROTO_L2CAP);
//bind socket to port 0x1001 of the first available bluetooth adapter.
loc_addr.l2_family = AF_BLUETOOTH;
loc_addr.l2_bdaddr = *BDADDR_ANY;
loc_addr.l2_psm = htobs(0x1001);
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.l2_bdaddr, buf);
fprintf(stderr, "accepted connection from %s\n", buf);
//read data from client
memset(buf, 0, sizeof(buf));
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;
}