author | Markus Bröker<broeker.markus@googlemail.com> |
Fri, 20 Oct 2017 06:41:27 +0200 | |
changeset 168 | dfb60716880c |
parent 165 | f551b78c3eee |
permissions | -rw-r--r-- |
165
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
1 |
/** |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
2 |
* bluetooth.c |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
3 |
* Copyright (C) 2010 Markus Bröker |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
4 |
*/ |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
5 |
|
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
6 |
#include <stdio.h> |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
7 |
#include <stdlib.h> |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
8 |
#include <unistd.h> |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
9 |
#include <sys/socket.h> |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
10 |
#include <bluetooth/bluetooth.h> |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
11 |
#include <bluetooth/rfcomm.h> |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
12 |
|
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
13 |
int main (int argc, char **argv) |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
14 |
{ |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
15 |
struct sockaddr_rc addr = { 0 }; |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
16 |
int s, status; |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
17 |
// Hard-coded for simplicity |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
18 |
char dest[18] = "58:17:0C:FF:36:D7"; // E10i |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
19 |
|
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
20 |
// allocate a socket |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
21 |
s = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
22 |
|
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
23 |
// set the connection parameters (who to connect to) |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
24 |
addr.rc_family = AF_BLUETOOTH; |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
25 |
addr.rc_channel = (uint8_t) 1; |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
26 |
str2ba (dest, &addr.rc_bdaddr); |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
27 |
|
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
28 |
// connect to server |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
29 |
status = connect (s, (struct sockaddr *)&addr, sizeof (addr)); |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
30 |
|
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
31 |
// send a message |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
32 |
if (status == 0) { |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
33 |
status = write (s, "hello!", 6); |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
34 |
} |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
35 |
|
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
36 |
if (status < 0) |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
37 |
perror ("uh oh"); |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
38 |
|
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
39 |
close (s); |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
40 |
|
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
41 |
return EXIT_SUCCESS; |
f551b78c3eee
a bluetooth and a c++ demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
42 |
} |