Hi all,

I asked this question in bluez-users list, but did not get an answer. may be bluez-devel is wrong place to ask such things, but I stuck on problem that looks VERY simple, but I can't find a way to correct  it. it's terrible to know, that 99.99%-percent-done application does not work because of only one send() function....

I'm writing a small code to test HandsFree capabilities between my phone and PC. everything works fine for audio playback (on the PC, I can hear sound file played on the phone and in case of incoming call, I can hear a voice of remote subscriber) but when I try at the same time to send some audio data to the handset via same socket, send() function returns "Invalid argument" error. I tried to create new socket for writing (same way I created listening socket). creation succeeds, but connect fails with "Device or resource busy". can anybody be so kind and explain, how can I read and write data through SCO channel simultaneously ?

thank you and best regards,
kern.

P.S. here is code snippet, that do actual SCO link setup and data transfer:

    int scosock, cli_scosock, snd_fd, nbytes;
    socklen_t addrlen;
    struct sockaddr_sco addr;

    unsigned char buffer[1024];
       
    snd_fd = init_sound();
    while(1)
    {
        if((scosock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO)) < 0)
        {
            perror("could not create SCO channel socket");
            continue;
        }
   
        memset(&addr, 0, sizeof(struct sockaddr_sco));
        addr.sco_family = AF_BLUETOOTH;
        bacpy(&addr.sco_bdaddr, BDADDR_ANY);
        if(bind(scosock, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0)
        {
            perror("could not bind SCO channel to local address");
            close(scosock);
            continue;
        }
      
        listen(scosock, 20);
       
        memset(&addr, 0, sizeof(struct sockaddr_sco));
        addrlen = sizeof(struct sockaddr);
   
        printf("waiting for SCO connection\n");
       
        if(wait4data(scosock) < 0 || (cli_scosock = accept(scosock, (struct sockaddr *)&addr, &addrlen)) < 0)
        {
            printf("could not accept SCO link, socket error, reconnecting\n");
            close(scosock);
            continue;
        }

        printf("accepted voice channel from MT\n");

        while(1)
        {
            memset(buffer, 0, 1024);
            if(wait4data(cli_scosock) < 0 || (nbytes = read(cli_scosock, buffer, 1024)) < 0)
            {
                printf("could not read from SCO link, socket error, reconnecting\n");
                close(cli_scosock);
                close(scosock);
                break;
            }
            write(snd_fd, buffer, nbytes);
            memset(buffer, 0, 1024);
            read(snd_fd, buffer, 48);
            if(send(cli_scosock, buffer, 48, 0) < 0)             << --- send() fails here with "Invalid argument"
            {
                perror("send failed");
            }
        }
    }
}