Search code examples
csocketsubuntubluez

How you use SCO in BLUEZ?


I tried to use SCO from Bluez. I followed the example: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/tools/scotest.c

and my code looks like that:

sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET | SOCK_NONBLOCK, BTPROTO_SCO);
if (sk < 0) {
    syslog(LOG_ERR, "Can't create socket: %s (%d)",
                        strerror(errno), errno);
    return -1;
}


/* Connect to remote device */
memset(&addr, 0, sizeof(addr));
addr.sco_family = AF_BLUETOOTH;
str2ba(svr, &addr.sco_bdaddr);

if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    syslog(LOG_ERR, "Can't connect: %s (%d)",
                        strerror(errno), errno);
}

/* Get connection information */
memset(&conn, 0, sizeof(conn));
optlen = sizeof(conn);

if (getsockopt(sk, SOL_SCO, SCO_CONNINFO, &conn, &optlen) < 0) {
    syslog(LOG_ERR, "Can't get SCO connection information: %s (%d)",
                        strerror(errno), errno);
}


if( recv(sk,buffer, bufferSize,0) < 0) {
    syslog(LOG_ERR, "Can't read: %s (%d)",
                        strerror(errno), errno);
}

Whenever I try to read data I get an error: "Can't read: Function not implemented (38)" When I try to use getsockopt I get an error saying that transport endpoint is not connected.

Don't know how to fix it. I use Ubuntu 20.04 and bluetoothd -v == 5.53


Solution

  • HFP SLC should be established before SCO connection. HFP Service Level Connection (SLC) will be established by oFono and writing SCO data is implemented by pulseaudio.

    First make sure HFP profile is connected then you can try with above code.