Hello,
I'm new to using Bluez. I need to create a simple program to send text over a specific type of port (SOCK_DGRAM, BTPROTO_L2CAP). The code compiles fine, but when I run it i get the following error (from then sendto function).
Send Failed: Invalid argument
What might the problem be? My code is shown below. Thank-you so much for any help that you can offer.
,verber
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include "l2cap_msg_send.h"
int fd;
int send_msg()
{
/* SETTING UP SOCKET */
struct sockaddr_l2 addr;
unsigned int psm = 0x1001;
fd = socket(AF_BLUETOOTH, SOCK_DGRAM, BTPROTO_L2CAP);
if (fd == -1) {
fprintf(stderr, "Could not open socket: %s\n", strerror(errno));
exit(1);
}
addr.l2_family = AF_BLUETOOTH;
bacpy(&addr.l2_bdaddr, BDADDR_ANY);
addr.l2_psm = htobs(psm);
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
exit(1);
}
/* END: SETTING UP SOCKET*/
/* SET UP MSG */
char recv_addr[18] = "00:0A:3A:53:1C:42";
struct sockaddr_l2 recipient;
recipient.l2_family = AF_BLUETOOTH;
str2ba(recv_addr,&(recipient.l2_bdaddr));
recipient.l2_psm = htobs(psm);
char message[] = "57 123 189";
/* END: SET UP MSG */
/* SEND MSG */
int ret;
socklen_t len;
len=sizeof(recipient.l2_bdaddr);
size_t msg_len;
msg_len=sizeof(message);
ret = sendto(fd, &message, msg_len,0, (struct sockaddr *) &recipient, len);
printf("%d\n",ret);
if( ret < 0 ) fprintf(stderr,"Send Failed: %s\n",strerror(errno));
/* END: SEND MSG */
// close(fd);
return 0;
}