通过蓝牙连续传输数据

标签 c linux bluetooth

我想生成 0 到 100 之间的随机数,并通过蓝牙将它们从 Raspberry Pi(运行 Linux)连续传输到嵌入式 (x86) PC(也运行 Linux)。我的 C 代码基于以下内容:-

客户端:-

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

服务器端:-

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

虽然这对于单个字符串效果很好,但我无法传输整数。另外,对于连续的数据流,我是否只是让服务器和客户端在 for 循环中运行(例如),还是有更好的方法来做到这一点?

最佳答案

实际上,您想通过 BT RFCOMM 套接字发送整数类型数据,但您的基本示例需要发送string。 您的解决方案是将您的数字转换为字符串(C 语言中的 char 数组)。

您的代码来自Albert S. Huang关于 BT 编程的宝贵著作 Great BT programming tutorial

将这些更改应用到您的代码。

客户端

  // 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");
    }

它发送 73232 到服务器。

服务器

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

服务器成功接收您号码的每一部分。 但正如你所看到的(我将它们写为注释)它们以 LE 形式存储。

73232 => 01 1e 10

(我们可以为每个数字添加前导 0)

提示:您可以使用在线十进制到十六进制转换器。 dec_to_HEX

因为endian,你会以反转的形式捕获它们。 .

此时您已经拥有它们并且可以执行一些其他操作。

正如您在示例中所说,您的号码必须由随机函数生成,并且其返回值(您的随机数)通过 BT 套接字发送。

我的解决方案基于stack answer

关于通过蓝牙连续传输数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28020844/

相关文章:

android - 如何让 Android 5.0+ 上的蓝牙 HID 主机正常工作

android - 三星S8 Android 8.0的蓝牙HCI日志在哪里

c - fflush() 始终返回 0 但将 errno 设置为 11(资源暂时不可用)

ARM cortex m3 中的调用堆栈展开

c - 常用的libc头文件有哪些及其功能

linux - Debian 易错误 : "The following signatures were invalid: NODATA 1 NODATA 2"

c - 使用自定义部分属性时附加符号名称

c - 在 main 中打印返回字符串的(完整)值

c - 如何在linux中控制dcd

c++ - 获取范围内的蓝牙设备