From 85635af1852b29e33ee56535d71bdf20d700b77e Mon Sep 17 00:00:00 2001 From: Edvin Date: Tue, 8 Apr 2025 10:32:57 +0200 Subject: Added C code for the Raspberry pi due to compatiblity issues between Bluez versions --- rpi.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 rpi.c (limited to 'rpi.c') diff --git a/rpi.c b/rpi.c new file mode 100644 index 0000000..3f6fa08 --- /dev/null +++ b/rpi.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include + +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 = *BDADDR_ANY; + loc_addr.rc_channel = (uint8_t) 1; + bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); + + // get local address + // ba2str( &loc_addr.rc_bdaddr, buf ); + // fprintf(stdout, "local %s\n", buf); + + // put socket into listening mode + fprintf(stdout, "Waiting on connection on RFCOMM channel %i...\n", loc_addr.rc_channel); + listen(s, loc_addr.rc_channel); + + // accept one connection + client = accept(s, (struct sockaddr *)&rem_addr, &opt); + ba2str( &rem_addr.rc_bdaddr, buf ); + fprintf(stdout, "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); + } + + // close connection + close(client); + close(s); + return 0; +} -- cgit v1.2.3