0

I would like to generate random numbers between 0 and 100 and transfer them continuously over bluetooth from a Raspberry Pi (running Linux) to an embedded (x86) PC (also running Linux). My C code is based on the following: -

Client Side: -

#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;
    char dest[18] = "01:23:45:67:89:AB";

    // 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 = (uint8_t) 1;
    str2ba( dest, &addr.rc_bdaddr );

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

    // send a message
    if( status == 0 ) {
        status = write(s, "hello!", 6);
    }

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

    close(s);
    return 0;
}

Server side: -

#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 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);

    // 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
    listen(s, 1);

    // accept one connection
    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);
    }

    // close connection
    close(client);
    close(s);
    return 0;
}

While this works well for a single string, I am unable to transfer integers. Also, for a continuous data stream, do I just have the server and client running in a for loop (for example) or is there a better way to do it?

ranger3
  • 23
  • 3
  • 2
    - About being "unable to transfer integers": can you show the code that doesn't work instead of the one with the string, that does work? – Oren Kishon Jan 19 '15 at 09:05
  • 2
    did you take care of the different endianess of the systems when you try to send an int? Do you receive garbage or nothing? – mch Jan 19 '15 at 09:15

1 Answers1

0

Actually you want to send an Integer type data over BT RFCOMM socket but your base example requires string to be send. Your solution is simply convert your number to string (char array in C language).

Your code comes from Albert S. Huang invaluable works on BT programming Great BT programming tutorial

Apply these changes to your code.

client

  // send
char integer[4];                  // buffer
*((int*)integer) = 73232;         // 73232 is data which I want to send.
//send( cs, integer, 4, 0 );        // send it

    // send a message to server
    if( status == 0 ) {
        status = write(s, integer, 4);
        if (status == 4){
            printf("Send data to server done\n");
        }
    }
    else 
        if( status < 0 ){ 
            perror("send message to server Failed\n");
    }

Its send 73232 to server.

server

char integer [4];

bytes_read = read(client, integer, 4);

/*if( bytes_read > 0 ) {
    printf("received [%s]\n\n", buf);
}*/

printf("int: %x\n", integer[0]);//10
printf("int: %x\n", integer[1]);//1e
printf("int: %x\n", integer[2]);//1
printf("int: %x\n", integer[3]);//0

And server successfully receive every chunks of your number. But as you see (I write them as comment ) they are store in LE form.

73232 = > 01 1e 10

(we can add a leading 0 digit to every numbers)

Hint: you can use an on line decimal to hex converter. dec_to_HEX

you catches them in revers form because of endian.

And in this point you have them and can do some other operations.

As you said in example, your number must generated by a random function and its return value (your random) is send over BT socket.

my solution based on stack answer

Community
  • 1
  • 1
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31