raspberry
This commit is contained in:
1 parent
84dfdf5210
commit
be436085b3
55 files changed
+2195
No files matched your search
@@ -0,0 +1,50 @@
|
||||
#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;
|
||||
socklen_t opt = sizeof(rem_addr);
|
||||
|
||||
// allocate socket
|
||||
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
||||
|
||||
// bind socket to port 1 of the first available
|
||||
// local bluetooth adapter
|
||||
loc_addr.rc_family = AF_BLUETOOTH;
|
||||
loc_addr.rc_bdaddr = "00:00:00:00:00:00";
|
||||
loc_addr.rc_channel = (uint8_t) 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 = read(client, buf, sizeof(buf));
|
||||
if( bytes_read > 0 ) {
|
||||
printf("received [%s]\n", buf);
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
|
||||
|
||||
if( status < 0 ) perror("uh oh");
|
||||
|
||||
// close connection
|
||||
close(client);
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user