0

我想在两个蓝牙设备之间使用 L2CAP 协议发送消息。这是我的客户代码:

void process_bluetooth () {

    int bl_buff_it = 0; // global buffer iterator

    // Code of: https://people.csail.mit.edu/albert/bluez-intro/x559.html
    struct sockaddr_l2 addr = { 0 };
    int s, status;
    char dest[18] = "24:0A:64:28:71:E6";

    // Socket creation
    s = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);

    // Connection parameters
    addr.l2_family = AF_BLUETOOTH;
    addr.l2_psm = htobs(0x1001);
    str2ba( dest, &addr.l2_bdaddr );

    // Establishing connection
    status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
    if (status < 0) exit(0);

    // Message buffer
    int tam_bluetooth_buff = DATA_BLUETOOTH_BUFFER * (SIZE_DATA + 1); // DATA_BLUETOOTH_BUFFER = 128, SIZE_DATA = 3
    char bluetooth_buff[tam_bluetooth_buff];

    strcpy(bluetooth_buff, "");

    int msg_to_send = 15, msg_num = 0;
    
    char value_string[SIZE_DATA];

    while (msg_num < msg_to_send) {

        // Checking if data available in global data buffer
        if (bl_check_buff[bl_buff_it] == '1') {

            bl_check_buff[bl_buff_it] = '0';

            // Add data to buffer
            sprintf(value_string, "%u%u%u ", bl_data_buff[bl_buff_it].bytes[0], bl_data_buff[bl_buff_it].bytes[1], bl_data_buff[bl_buff_it].bytes[2]); // uint8_t bytes[3]
            strcat(bluetooth_buff, value_string);

            bl_buff_it = (bl_buff_it + 1) % NUM_ELEMENTS; // NUM_ELEMENTS = huge number

            if (bl_buff_it % DATA_BLUETOOTH_BUFFER == 0 && bl_buff_it != 0) {

                // Message sending
                if(write(s, bluetooth_buff, tam_bluetooth_buff) < 0) {
                    int errnum = errno;
                    fprintf(stderr, "Value of errno: %d\n", errno);
                    perror("Error printed by perror");
                    fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
                    exit(0);    
                }
                msg_num++;

                strcpy(bluetooth_buff, "");
            }
        }
    }

    close(s);
}

我的服务器代码与https://people.csail.mit.edu/albert/bluez-intro/x559.html中出现的完全相同,但缓冲区大小为 65535。

当我尝试增加bluetooth_buff的长度 -tam_bluetooth_buff变量 - 我得到这个错误

Value of errno: 90
Error printed by perror: Message too long
Error opening file: Message too long

DATA_BLUETOOTH_BUFFER请注意,例如,如果是 128 而不是 256,则一切正常。

请帮忙!!问候

4

0 回答 0