2009/06/03

Bluetooth - Programming

Codes modified from Bluetooth Essential for Programmer.
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int main(int argc, char **argv)
{
   struct sockaddr_rc addr = { 0 };
   int s, status, bytes_read;
   char buf[1] = { 0 };
   char dest[18] = "00:0B:0D:6C:36:0B";

   // allocate a socket
   s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

   // set the connection parameters (who to connect to)
   addr.rc_family = AF_BLUETOOTH;
   addr.rc_channel = 1;
   str2ba( dest, &addr.rc_bdaddr );

   // connect to server
   status = connect(s, (struct sockaddr *)&addr, sizeof(addr));

   // send a message
   if( 0 == status ) {

       while(1) {
           // read data from the client
           bytes_read = recv(s, buf, sizeof(buf), 0);
           //if( bytes_read > 0 ) {
               printf("%c", buf[0]);
          // }   
       }
   }

   if( status < 0 ) perror("uh oh");

   close(s);
   return 0;
}
# gcc rfcomm-client.c -lbluetooth # ./a.out $GPGGA,153226.985,2459.7755,N,12128.0203,E,0,00,,32.1,M,15.2,M,,0000*43 $GPRMC,153226.985,V,2459.7755,N,12128.0203,E,,,030609,,,N*7C $GPVTG,,T,,M,,N,,K,N*2C $GPGGA,153227.985,2459.7755,N,12128.0203,E,0,00,,32.1,M,15.2,M,,0000*42 $GPGSA,A,1,,,,,,,,,,,,,,,*1E $GPGSV,3,1,12,11,79,010,17,07,68,190,,08,63,334,,25,46,185,*70 $GPGSV,3,2,12,17,27,267,,28,20,334,,19,20,025,,32,10,117,*7D $GPGSV,3,3,12,22,08,029,,15,08,331,16,20,07,144,14,03,03,050,*77 $GPRMC,153227.985,V,2459.7755,N,12128.0203,E,,,030609,,,N*7D

沒有留言: