63 lines
1.6 KiB
Plaintext
Executable File
63 lines
1.6 KiB
Plaintext
Executable File
#include <iostream>
|
|
#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 };
|
|
struct hci_dev_info di;
|
|
char buf[1024] = { 0 };
|
|
int s, client, bytes_read;
|
|
socklen_t opt = sizeof(rem_addr);
|
|
char address[18] = "XX:XX:XX:XX:XX:XX";
|
|
// allocate socket
|
|
if(argc < 2)
|
|
{
|
|
cout << "Usage:\n<0/1> BTdevice" << endl;
|
|
exit(0);
|
|
}
|
|
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
|
{
|
|
perror("socket");
|
|
}
|
|
if(hci_devinfo(0, &di) < 0)
|
|
{
|
|
perror("HCI device info failed");
|
|
exit(1);
|
|
}
|
|
cout << "Local device: " << batostr(&di.bdaddr)) << endl;
|
|
laddr.rc_family = AF_BLUETOOTH;
|
|
laddr.rc_bdaddr = di.bdaddr;
|
|
laddr.rc_channel = 2;
|
|
|
|
// bind socket to port 1 of the first available
|
|
// local bluetooth adapter
|
|
bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
|
|
{
|
|
perror("bind");
|
|
exit(1);
|
|
}
|
|
// put socket into listening mode
|
|
listen(s, 1);
|
|
|
|
// accept one connection
|
|
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
|
|
|
|
ba2str( &rem_addr.rc_bdaddr, buf );
|
|
uuuuustderr, "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;
|
|
}
|