77 lines
1.8 KiB
C++
Executable File
77 lines
1.8 KiB
C++
Executable File
/*
|
|
* BlueZ example code to build an rfcomm client.
|
|
* This code just creates a socket and connects
|
|
* to a remote bluetooth device and sends a string.
|
|
* Programmed by Bastian Ballmann
|
|
* http://www.geektown.de
|
|
* Compile with gcc -lbluetooth <executable> <source>
|
|
* */
|
|
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <stdlib.h>
|
|
#include <bluetooth/bluetooth.h>
|
|
#include <bluetooth/rfcomm.h>
|
|
#include <bluetooth/hci.h>
|
|
#include <unistd.h>
|
|
#include <bluetooth/hci_lib.h>
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int sock, bytes_read, bt_channel=1;
|
|
struct sockaddr_rc laddr, raddr;
|
|
struct hci_dev_info di;
|
|
char bt_addr[]="98:D3:31:FD:14:BC";
|
|
if(argc < 3)
|
|
{
|
|
printf("%s <btaddr> <channel>\n", argv[0]);
|
|
exit(0);
|
|
}
|
|
if(hci_devinfo(0, &di) < 0)
|
|
{
|
|
perror("HCI device info failed");
|
|
exit(1);
|
|
}
|
|
printf("Local device %s\n", batostr(&di.bdaddr));
|
|
|
|
laddr.rc_family = AF_BLUETOOTH;
|
|
laddr.rc_bdaddr = di.bdaddr;
|
|
laddr.rc_channel = 0;
|
|
raddr.rc_family = AF_BLUETOOTH;
|
|
str2ba(argv[1],&raddr.rc_bdaddr);
|
|
raddr.rc_channel = atoi(argv[2]);
|
|
if( (sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0)
|
|
{
|
|
perror("socket");
|
|
}
|
|
|
|
if(bind(sock, (struct sockaddr *)&laddr, sizeof(laddr)) < 0)
|
|
{
|
|
perror("bind");
|
|
exit(1);
|
|
}
|
|
printf("Remote device %s\n", argv[1]);
|
|
if(connect(sock, (struct sockaddr *)&raddr, sizeof(raddr)) < 0)
|
|
{
|
|
perror("connect");
|
|
exit(1);
|
|
}
|
|
|
|
printf("Connected.\nRecieving data\n");
|
|
//read data from the client
|
|
while(1)
|
|
{
|
|
char buf[1024]={0};
|
|
bytes_read = read(sock, buf, sizeof(buf));
|
|
if( bytes_read > 0 ) {
|
|
printf("%s\n", buf);
|
|
}
|
|
|
|
}
|
|
printf("Disconnect.\n");
|
|
close(sock);
|
|
return 0;
|
|
}
|