13
votes

From my android application I want to stream audio to any available speaker like A2DP speaker ,docking speakers or car speaker.

for doing this I want to use Bluetooth connection and stream over Bluetooth connection...

I saw following links

On android how to reroute audio from a2dp

setBluetoothA2dpOn (boolean on)

Android 2.3: How do I switch from SCO to A2DP for a capable Bluetooth speaker?

But everywhere I found the same .. i.e.

public void setBluetoothA2dpOn (boolean on)

This method is deprecated.
Do not use. 

Please guide me on how to do this..Or suggest if it can be done using some other way.. Any help/suggestion is appreciated.

EDIT :

PERMISSION in manifest :

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

CODE :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    layout = (RelativeLayout) findViewById(R.id.layout);
    text = (TextView) findViewById(R.id.editText1);
    scoSwitch = (ToggleButton) findViewById(R.id.switch1);
    try {
        mp1 = MediaPlayer.create(this, R.raw.jc_cm);
        mp2 = MediaPlayer.create(this, R.raw.rp);
        amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        //amanager.setBluetoothA2dpOn(true);
                } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void onSCOswitch(View view){
    if (scoSwitch.isChecked()){
        amanager.setBluetoothScoOn(true);
        amanager.startBluetoothSco();
        Log.d("Bluetooth", "SCO on");
        amanager.setMode(AudioManager.MODE_IN_COMMUNICATION);
    }
    else{
        amanager.stopBluetoothSco();
        amanager.setBluetoothScoOn(false);
        amanager.setBluetoothA2dpOn(true);
        Log.d("Bluetooth", "SCO off");
        amanager.setMode(AudioManager.MODE_NORMAL);
    }
} 
2
What problems do you face while using this documentation .. developer.android.com/reference/android/bluetooth/…Ronnie
@userSeven7s : the problem i am facing is I am unable to detect that other device is also A2DP or not and so problem buffering audioShruti
paste some code you tried with...Ronnie
@userSeven7s :please see edited questionShruti
Can you share example code for this ?Hardik Joshi

2 Answers

3
votes
1
votes

Here is an implementation of Bluetooth A2DP in a Service. You can refer this code and build your own.

A2DP Bluetooth Service