2 * Copyright 2010 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3 * All rights reserved. Distributed under the terms of the MIT License.
11 #include <sys/socket.h>
13 #include <bluetooth/bluetooth.h>
14 #include <bluetooth/bdaddrUtils.h>
15 #include <bluetooth/L2CAP/btL2CAP.h>
18 CreateL2capSocket(const bdaddr_t
* bdaddr
, struct sockaddr_l2cap
& l2sa
, uint16 psm
)
24 /* Create the socket. */
25 printf("Creating socket ...\n");
27 sock
= socket(PF_BLUETOOTH
, SOCK_STREAM
, BLUETOOTH_PROTO_L2CAP
);
33 /* Bind a name to the socket. */
34 printf("Binding socket ...\n");
36 size
= sizeof(struct sockaddr_l2cap
);
38 l2sa
.l2cap_family
= PF_BLUETOOTH
;
39 l2sa
.l2cap_len
= size
;
40 memcpy(&l2sa
.l2cap_bdaddr
, bdaddr
, sizeof(bdaddr_t
));
43 if (bind(sock
, (struct sockaddr
*)&l2sa
, size
) < 0) {
48 printf("Connecting socket for %s\n", bdaddrUtils::ToString(*bdaddr
));
49 if ((error
= connect(sock
, (struct sockaddr
*)&l2sa
, sizeof(l2sa
))) < 0) {
54 printf("Return status of the connection is %ld \n", error
);
59 int main(int argc
, char **argv
)
61 struct sockaddr_l2cap l2sa
;
67 printf("I need a bdaddr!\nUsage:\n\t%s bluetooth_address [psm]\n",
74 printf("PSM requested %d\n", psm
);
78 printf("WARNING: PSM requested is not pair\n");
80 const bdaddr_t bdaddr
= bdaddrUtils::FromString(argv
[1]);
82 int sock
= CreateL2capSocket(&bdaddr
, l2sa
, psm
);
84 while (strcmp(string
,"bye") != 0) {
86 fscanf(stdin
, "%s", string
);
87 len
= sendto(sock
, string
, strlen(string
) + 1 /*\0*/, 0,
88 (struct sockaddr
*) &l2sa
, sizeof(struct sockaddr_l2cap
));
91 printf("Sent %ld bytes\n", len
);
93 // len = send(sock, string + 1 /*\0*/, strlen(string), 0);
94 // recvfrom(sock, buff, 4096-1, 0, (struct sockaddr *) &l2, &len);
97 printf("Transmission done ... (press key to close socket)\n");
100 printf("Closing ... \n");