diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | control.py | 5 | ||||
| -rw-r--r-- | main.py | 2 | ||||
| -rw-r--r-- | rpi.c | 50 |
4 files changed, 54 insertions, 5 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..880e92f --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +main: rpi.c + cc rpi.c -Wall -lbluetooth -o rpi @@ -17,15 +17,12 @@ def get_input() -> str: pi = "B8:27:EB:D1:A5:A9" sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) -# sock.bind(("", bluetooth.PORT_ANY)) - -# port = sock.getsockname()[1] print("Attempting to connect to %s..." % pi) sock.connect((pi, 1)) -print("Connected") +print("Connected to %s." % pi) input = "0" @@ -30,7 +30,7 @@ print("Waiting for connection on RFCOMM channel %d..." % port) client_sock, client_info = server_sock.accept() -print("Connected with %d" % client_info) +print("Connected to %s." % client_info[0]) while client_sock: read = client_sock.recv(1024) @@ -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 = *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; +} |
