/* * write() should not return 0 after * the remote device has terminated the connetion. * This makes glib's io_channel terminate * the running program. * kanchev@in.tum.de */ #include #include #include #include #include #include #include #include int main(int argc, char **argv) { char dummy; int sock, r; struct sockaddr_rc saddr; struct pollfd pollfd; if (argc != 3) { fprintf(stderr, "Usage: %s \n", argv[0]); return -1; } if ((sock = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) { perror("socket()"); return -1; } memset(&saddr, 0, sizeof(struct sockaddr_rc)); str2ba(argv[1], &saddr.rc_bdaddr); saddr.rc_family = AF_BLUETOOTH; saddr.rc_channel = (uint8_t) atoi(argv[2]); printf("Connecting to %s ...\n", argv[1]); if (connect(sock, (struct sockaddr *) &saddr, sizeof(struct sockaddr_rc)) < 0) { close(sock); perror("connect()"); return -1; } pollfd.fd = sock; pollfd.events = POLLOUT; pollfd.revents = 0; if (poll(&pollfd, 1, -1) <= 0) { close(sock); perror("poll()"); return -1; } printf("Turn off the bluetooth device %s now!\nPress enter to continue...", argv[1]); fflush(stdout); read(0, &dummy, 1); r = write(sock, "Hello", 5); printf("write() returned %d\n", r); close(sock); return 0; }