My c-program should listen for incoming Bluetooth connections. I read the example at http://people.csail.mit.edu/albert/bluez-intro/x502.html, so I have the following code:
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth.h>
#include <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);
printf("socket: %d \n", s);
// 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));
// put socket into listening mode
printf("put socket into listening mode \n");
listen(s, 1);
// accept one connection
printf("accept one connection \n");
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
ba2str( &rem_addr.rc_bdaddr, buf );
fprintf(stderr, "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);
}
printf("close connection");
// close connection
close(client);
close(s);
return 0;
}
But instead of waiting for incoming connection requests, it´s connecting to the address 00:00:00:00:00:00 immediately (see Linux terminal output here). So my program starts, setting the socket in listening mode, accepting connection from 00:00... and closing the connection again. I don´t know what the 00:00... adress is standing for but its definitely not the address of the computer I want to connect to.
Btw, the Bluetooth adapter is working fine, scanning for devices works and it´s also possible to connect to other computers with default Bluetooth commands in the Terminal. But I need to do this in a c-program to analyze the incoming messages.
Thanks for your help