Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing audio to Bluetooth Headset (non-A2DP) on Android

I have a non-A2DP single ear BT headset (Plantronics 510) and would like to use it with my Android HTC Magic to listen to low quality audio like podcasts/audio books.

After much googling I found that only phone call audio can be routed to the non-A2DP BT headsets. (I would like to know if you have found a ready solution to route all kinds of audio to non-A2DP BT headsets)

So I figured, somehow programmatically I can channel the audio to the stream that carries phone call audio. This way I will fool the phone to carry my mp3 audio to my BT headset. I wrote following simple code.

import android.content.*; import android.app.Activity; import android.os.Bundle; import android.media.*; import java.io.*; import android.util.Log;  public class BTAudioActivity extends Activity {     private static final String TAG = "BTAudioActivity";      private MediaPlayer mPlayer = null;     private AudioManager amanager = null;      @Override     public void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);         amanager.setBluetoothScoOn(true);         amanager.setMode(AudioManager.MODE_IN_CALL);          mPlayer = new MediaPlayer();          try {             mPlayer.setDataSource(new FileInputStream(                 "/sdcard/sample.mp3").getFD());              mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);              mPlayer.prepare();              mPlayer.start();         } catch(Exception e) {             Log.e(TAG, e.toString());         }     }      @Override     public void onDestroy()     {         mPlayer.stop();         amanager.setMode(AudioManager.MODE_NORMAL);         amanager.setBluetoothScoOn(false);         super.onDestroy();     } } 

As you can see I tried combinations of various methods that I thought will fool the phone to believe my audio is a phone call:

  • Using MediaPlayer's setAudioStreamType(STREAM_VOICE_CALL)
  • using AudioManager's setBluetoothScoOn(true)
  • using AudioManager's setMode(MODE_IN_CALL)

But none of the above worked. If I remove the AudioManager calls in the above code, the audio plays from speaker and if I replace them as shown above then the audio stops coming from speakers, but it doesn't come through the BT headset. So this might be a partial success.

I have checked that the BT headset works alright with phone calls.

There must be a reason for Android not supporting this. But I can't let go of the feeling that it is not possible to programmatically reroute the audio. Any ideas?

P.S. above code needs following permission

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

like image 636
Jayesh Avatar asked Jan 27 '10 05:01

Jayesh


People also ask

How do I change audio output to Bluetooth headphones?

Tap the small button at the top right of the player notification tile. In the media player pop-up, you'll see a list of connected audio devices. Tap the one you want to switch to.


2 Answers

This thread may be long dead but for those who might be trying the same thing, some notes from the AudioManager docs may be useful. It looks like the missing element is the startBluetoothSco() command but there are restrictions on the use of this channel. From the Android Dev site here:

public void startBluetoothSco () Since: API Level 8 Start bluetooth SCO audio connection.

Requires Permission: MODIFY_AUDIO_SETTINGS.

This method can be used by applications wanting to send and received audio to/from a bluetooth SCO headset while the phone is not in call.

As the SCO connection establishment can take several seconds, applications should not rely on the connection to be available when the method returns but instead register to receive the intent ACTION_SCO_AUDIO_STATE_CHANGED and wait for the state to be SCO_AUDIO_STATE_CONNECTED.

As the connection is not guaranteed to succeed, applications must wait for this intent with a timeout.

When finished with the SCO connection or if the establishment times out, the application must call stopBluetoothSco() to clear the request and turn down the bluetooth connection.

Even if a SCO connection is established, the following restrictions apply on audio output streams so that they can be routed to SCO headset: - the stream type must be STREAM_VOICE_CALL - the format must be mono - the sampling must be 16kHz or 8kHz

The following restrictions apply on input streams: - the format must be mono - the sampling must be 8kHz

Note that the phone application always has the priority on the usage of the SCO connection for telephony. If this method is called while the phone is in call it will be ignored. Similarly, if a call is received or sent while an application is using the SCO connection, the connection will be lost for the application and NOT returned automatically when the call ends.

See Also stopBluetoothSco() ACTION_SCO_AUDIO_STATE_CHANGED

Note that I have not tested this, I'm just passing along a lead I found in researching a similar project. I think Jayesh was close to the solution and the restrictions above may have been what was keeping it from working.

like image 119
Vic Avatar answered Sep 22 '22 23:09

Vic


To turn on:

 localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);  localAudioManager.setMode(0);  localAudioManager.setBluetoothScoOn(true);  localAudioManager.startBluetoothSco();  localAudioManager.setMode(AudioManager.MODE_IN_CALL); 

To turn off:

 localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);  localAudioManager.setBluetoothScoOn(false);  localAudioManager.stopBluetoothSco();  localAudioManager.setMode(AudioManager.MODE_NORMAL); 

I took it from here

like image 45
Uriel Frankel Avatar answered Sep 22 '22 23:09

Uriel Frankel