// Written by Yevgeniy Medynskiy (eugenem@gatech.edu) // Date modified: December 2006 // // No copyright. No warranty. Distributed as-is. // // http://www.gvu.gatech.edu/ccg/resources/wearableRFID.html #include #include #include #include #include #include #include #include #include #include #include #include // //// Change to your device's Bluetooth address // char *bluetooth_device = "00:13:E0:38:8B:A1"; uint8_t bluetooth_channel = 1; int DATA_AVAILABLE; // When data is available on the serial port, this will get called //void signal_handler_IO(int status) { // DATA_AVAILABLE = 1; //} int QUIT = 0; // Set hint for the program to clean up and quit void signal_handler_INT(int status) { QUIT = 1; } int open_bluetooth_rfcomm() { struct sockaddr_rc addr = {0}; int s, status; // Allocate a socket s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); // Set connection params addr.rc_family = AF_BLUETOOTH; addr.rc_channel = bluetooth_channel; str2ba(bluetooth_device, &addr.rc_bdaddr); // Connect to device status = connect(s, (struct sockaddr *)&addr, sizeof(addr)); if(status != 0) { perror("Unable to connect to bluetooth device"); return -1; } else { // fcntl(s, F_SETFL, FNDELAY); // Non-blocking return s; } } int main(int argc, char **argv) { int status = 0; char buf[255] = "\0"; // Setup handler for SIGINT (Ctrl-C) to close port and cleanup struct sigaction saio_int; saio_int.sa_handler = signal_handler_INT; sigemptyset(&saio_int.sa_mask); saio_int.sa_flags = 0; saio_int.sa_restorer = NULL; sigaction(SIGINT, &saio_int, NULL); // Non-blocking stdin fcntl(0, F_SETFL, O_NONBLOCK); printf("Connect to bluetooth device..."); int s = open_bluetooth_rfcomm(); if(s == -1) { printf("failed.\n"); return 1; } printf("done.\n"); // Setup handler for SIGIO on the bluetooth device // struct sigaction saio; // saio.sa_handler = signal_handler_IO; // sigemptyset(&saio.sa_mask); // saio.sa_flags = 0; // saio.sa_restorer = NULL; // sigaction(SIGIO, &saio, NULL); // fcntl(s, F_SETOWN, getpid()); // fcntl(s, F_SETFL, FASYNC); // //// Read command and request for acknowledgement. // char cmd[8]; cmd[0] = 0x72; cmd[1] = 0x65; cmd[2] = 0x33; cmd[3] = 0x36; cmd[4] = 0x34; cmd[5] = 0x02; cmd[6] = 0x07; cmd[7] = 0x01; status = write(s, cmd, 8); //printf("%d\n", status); cmd[0] = 0x61; cmd[1] = 0x63; cmd[2] = 0x6B; cmd[3] = 0x6E; cmd[4] = 0x77; cmd[5] = 0x6C; cmd[6] = 0x67; cmd[7] = 0x65; status = write(s, cmd, 8); //printf("%d\n", status); if(status <= 0) printf("Unable to write data\n"); printf("Running!\n"); while(!QUIT) { DATA_AVAILABLE = 1; if(DATA_AVAILABLE) { DATA_AVAILABLE = 0; // Read from the bluetooth connection status = read(s, buf, 255); if(status == 0) { printf ("0 "); fflush(stdout); } if(status < 0) { perror("Unable to read from bluetooth connection"); } if(status > 0) { // Echo locally write(0, buf, status); write(0, "\n", 1); } } } printf("Closing bluetooth connection..."); close(s); printf("done.\n"); return 0; }