now i can send bluetooth messages from linux to galileo.
- follow the steps https://github.com/tokoro10g/galileo-makefile, then you can compile c programme without compile
the yoto damn thing or cross compiler.
2. the server run on galileo.(need some of bluez include file bluetooth.h l2cap.h)
server.c
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/socket.h>
#include <bluetooth.h>
#include <l2cap.h>
#include <time.h>
#define RWRR (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
#define BUFFSIZE 1024
void puintobg(){
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0)
exit(EXIT_SUCCESS);
/* Change the file mode mask */
umask(0);
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
exit(EXIT_FAILURE);
/* Change the current working directory */
if ((chdir("/")) < 0)
exit(EXIT_FAILURE);
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
int main(int argc, char **argv)
{
puintobg();
time_t timep;
struct sockaddr_l2 loc_addr = { 0 }, rem_addr = { 0 };
struct sockaddr_l2 addr;
socklen_t optlen;
char buf[BUFFSIZE] = { 0 };
char buftmp[BUFFSIZE]={0};
char str[18];
int s, client, bytes_read;
socklen_t opt = sizeof(rem_addr);
// allocate socket
s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
// bind socket to port 0x1001 of the first available
// bluetooth adapter
loc_addr.l2_family = AF_BLUETOOTH;
loc_addr.l2_bdaddr = *BDADDR_ANY;
loc_addr.l2_psm = htobs(0x1001);
if (bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0) {
//perror("Can't bind socket");
goto error;
}
/* Get local address */
memset(&addr, 0, sizeof(addr));
optlen = sizeof(addr);
if (getsockname(s, (struct sockaddr *) &addr, &optlen) < 0) {
//perror("Can't get local address");
goto error;
}
ba2str(&addr.l2_bdaddr, str);
//printf("localaddr %s ...\n", str);
// put socket into listening mode
listen(s, 1);
while(1){
// accept one connection
client = accept(s, (struct sockaddr *)&rem_addr, &opt);
ba2str( &rem_addr.l2_bdaddr, buf );
sprintf(buftmp, "accepted connection from %s ", buf);
memset(buf, 0, sizeof(buf));
// read data from the client
bytes_read = read(client, buf, sizeof(buf));
if( bytes_read > 0 ) {
int fd=0;;
//printf("received [%s]\n", buf);
if((fd = open("btContent.log", O_WRONLY | O_CREAT | O_APPEND, RWRR)) < 0)
goto error;
write(fd,buftmp,strlen(buftmp));
write(fd, "[",1);
write(fd, buf, bytes_read);
write(fd, "]",1);
time(&timep);
write(fd,ctime(&timep),24);
write(fd, "\n",1);
//goto error;
close(fd);
}
close(client);
}
error:
close(client);
close(s);
exit(1);
}
3.client.c runs on linux with usb bluetooth device
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include "bluetooth.h"
#include "l2cap.h"
int main(int argc, char **argv)
{
struct sockaddr_l2 addr = { 0 };
int s, status;
char *message;
char dest[18];
if(argc < 2)
{
fprintf(stderr, "usage: %s <bt_addr> <bt_message>\n", argv[0]);
exit(2);
}
if(argc==3){
strncpy(dest, argv[1], 18);
message=argv[2];
}
else{
strncpy(dest,"00:15:00:96:7F:93",18);
message=argv[1];
}
// allocate a socket
s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
// set the connection parameters (who to connect to)
addr.l2_family = AF_BLUETOOTH;
addr.l2_psm = htobs(0x1001);
str2ba( dest, &addr.l2_bdaddr );
// connect to server
status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
// send a message
if( status == 0 ) {
status = write(s, message, strlen(message));
}
if( status < 0 ) perror("uh oh");
close(s);
}
4.of course do not forget run :
hciconfig hci0 up
hciconfig hci0 piscan
on galileo.