Mark Noon
Mark Noon

Reputation: 13

Can't get Bluetooth SCO connection to work on android

so i'm currently developing an android service which implements the HFP profile for later use with a gui , i was able to successfully and easily implement the RFCOMM part where the AT commands like ATA(accep call) are sent , but i am stuck with accepting the audio SCO Connection on the app . so basically im testing with an Iphone AG Role and an android tablet HF Role which runs my app , to open the SCO connection i have tried calling AudioMAnager.startBluetoothSco(); without any luck , and even made a bluetooth SCO socket server in C using the ndk , which listens for a connection. but the actual problem is that the Iphone doesnt seem to try to connect with the sco socket , so i dumped the trafic from the android tablet and saw that when the Iphone requests the SCO connection the android hci automatically responds with Reject of reason: Connection Rejected due to Limited Resources (0x0d) , no matter what i do, maybe im missing something? any ideas? thanks. forgot to mention that both devices are paired using the os settings app and the connection is established by connecting to the android tablet from the native settings app of ios.

AudioHandler.java

public class AudioHandler implements Runnable{
static
{
    System.loadLibrary("libsco");
}

@Override
public void run()
{
    AudioManager amanager = (AudioManager) Common.APPCONTEXT.getSystemService(Context.AUDIO_SERVICE);

    amanager.setMode(AudioManager.MODE_IN_COMMUNICATION);
    amanager.startBluetoothSco();
    amanager.setBluetoothScoOn(true);


    if(amanager.isBluetoothScoOn())
    {
        int status= this.SCOINIT();
        Log.d("SCO","SCOFinished");
    }


    /*while(Common.isCallActive)
    {
        play(stream.readbytes());
    }
        this.SCOCLOSE();
        amanager.stopBSCO();
    */



}

private native int SCOINIT();}

libsco.c

void init() {
struct sockaddr_sco addr = {0}, remoteadress = {0};

int SCOServer, SCOClient;


SCOServer = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);

if (SCOServer < 0) {
    __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "SCO socket create failed.");
    print("SCO socket create failed.");
}

addr.sco_family = AF_BLUETOOTH;
bacpy(&addr.sco_bdaddr, BDADDR_ANY);

if (bind(SCOServer, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    print("failed to bind sco.");

}

if (listen(SCOServer, 1)) {
    print("Listening Failed!!");
} else {
    print("Listening for SCO connection.");

}

socklen_t addrlength = sizeof(remoteadress);

SCOClient = accept(SCOServer, (struct sockaddr *) &remoteadress, &addrlength);


if (SCOClient < 0) {
    print("Accept Failed!!");
    close(SCOClient);

} else {
    print("Conected.\n");
    close(SCOClient);
}


close(SCOServer);

}

everything runs without errors and i can see the "LIstening for SCO Connection " line, but it never accepts because the android hci rejects the connection before anything can be done... screenshot from wireshark

Upvotes: 0

Views: 2346

Answers (0)

Related Questions