Android all sound to bluetooth

Android: Playing sound over Sco Bluetooth headset

For the past few days I have been trying to play any sound over my sco bluetooth headset from my android phone. My final goal with this project is to eventually make a garage door opener, but first I need to be able to play sound over the headset. Here is the basis of the current code I’m using:

==Manifest== ==Code== audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); audioManager.setMode(AudioManager.MODE_IN_CALL); audioManager.startBluetoothSco(); audioManager.setBluetoothScoOn(true); short[] soundData = new short [8000*20]; for (int iii = 0; iii < 20*8000; iii++) < soundData[iii] = 32767; iii++; soundData[iii] = -32768; >audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, soundData.length * Short.SIZE, AudioTrack.MODE_STATIC); audioTrack.write(soundData, 0, soundData.length); audioTrack.play(); 

Before I run this, I pair my bluetooth headset to my phone and have it connected. I have verified it works by calling my voicemail. When I run my code however, no sound comes from anywhere. Here are the effects of the different lines of code: When I’m just running my app:

 audioManager.setMode(AudioManager.MODE_IN_CALL); 
 audioManager.startBluetoothSco(); audioManager.setBluetoothScoOn(true); 

These two lines make the sound stop coming out of the front speaker and make my headset click and hiss like it’s turned on but there’s no output.

AudioManager.STREAM_VOICE_CALL 

This is part of my call to the AudioTrack constructor, but it makes quite a difference. Since this is set to STREAM_VOICE_CALL the sound comes out of the front speaker, if I set this to STREAM_MUSIC, the sound comes out the back speaker instead. When I open my app during a call:

 audioManager.setMode(AudioManager.MODE_IN_CALL); 

During a call, this line has no effect because MODE_IN_CALL was already set. But what’s different however is that my sound is mixed with the phone call, whereas normally it doesn’t play at all.

 audioManager.startBluetoothSco(); audioManager.setBluetoothScoOn(true); 

These, with their counterpart off halves, control where the audio is coming from. If I turn these off, my sound and the telephone call come from the front speaker, with these on, the telephone call comes from my headset and my sound is lost. As to why my code is not working, I honestly have no idea. I believe I have fulfilled the checklist for using startBluetoothSco().

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 

So, does anyone have an idea of what I am doing wrong? There was one time that I managed to get my sound to play through the headset, but it was only a short tone when I forgot to stop() my AudioTrack so I have to assume it was a glitch.

4 Answers 4

I found the solution on this page. You have to call audioManager.setMode(AudioManager.MODE_IN_CALL); only after the socket is connected, i.e., you received AudioManager.SCO_AUDIO_STATE_CONNECTED . I could hear the TTS on my Spica running android 2.2.2.

Читайте также:  Pioneer vsx 821 bluetooth

Edit: Here is my (old) implementation:

public class BluetoothNotificationReceiver extends BroadcastReceiver < /** * */ public BluetoothNotificationReceiver(Handler h) < super(); bnrHandler = h; >/* (non-Javadoc) * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent) */ @Override public void onReceive(Context arg0, Intent arg1) < String action = arg1.getAction(); if (action.equalsIgnoreCase(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED)) < int l_state = arg1.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1); Log.d("bnr", "Audio SCO: " + AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED); switch(l_state) < case AudioManager.SCO_AUDIO_STATE_CONNECTED: < Log.i("bnr", "SCO_AUDIO_STATE_CONNECTED"); >break; case AudioManager.SCO_AUDIO_STATE_DISCONNECTED: < Log.e("bnr", "SCO_AUDIO_STATE_DISCONNECTED"); >break; default: Log.e("bnr", "unknown state received:"+l_state); > > else Log.e("bnr", "onReceive:action mt24">
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this answer
)">edited Nov 1, 2014 at 14:14
answered Jul 5, 2012 at 16:50
0
Add a comment|
0

Try setting the audiomanager to AudioManager.MODE_NORMAL. Thos worked for me.

)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this answer
answered Feb 7, 2012 at 8:30
2
    1
    I'm pretty sure when I was working on this, I set it to MODE_NORMAL and the sound simply played out of the primary speaker rather than the cellphone earpiece. I could be wrong though, but it's working now.
    – thepenguin77
    Mar 30, 2012 at 4:58
    It does not work with Bluetooth if audio manager enabled with mode type MODE_NORMAL.
    – ॐ Rakesh Kumar
    Oct 10, 2018 at 4:41
Add a comment|
0

Add to manifest:

Источник

Bluetooth sound output on Android

I just noticed, using the HTC Dream that using a bluetooth headset was really easy for phone calls, but nothing seemed to be available for videos / music sound output. V1.0 limitations are not at fault since when you play a music and start a call, you can hear the music in the headset for a second. So it's technically possible, just not available in the settings. Does anybody knows how to redirect programatically the sound output to the blutooth headset ? I'd like to code a little app that let you listen to your music while looking like a total idiot with a big device hanging to your hear 🙂 N.B : this question is a copy of the one I asked in Google group.

1 Answer 1

Prior to Android 1.5, Bluetooth support was limited to the headset/handsfree profile. No "audio" other than the phone conversation could be redirected over Bluetooth.

Having installed Cupcake on my G1, I have now used Bluetooth stereo audio with my Motorola S9 headphones perfectly. The whole stack is even smart enough that they automatically pause the music/video, switch over to the headset profile when a call comes in and then back when the call finishes.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Receive audio via Bluetooth in Android

I want to create an Android application that is capable of receiving an audio stream. I thought of using the A2DP profile, but is seems as if Android doesn't support A2DP sink. Looks like there are a lot of people that's searching for a solution for this problem. But what about receiving an ordinary bit stream, and then convert the data into audio in the application? I was thinking of receiving an PCM or Mp3 data stream via the RFCOMM (SPP Bluetooth profile), and then play it using AudioTrack. First, how do I receive a bit stream on my Android phone via the RFCOMM? And is it possible to receive a bit stream via RFCOMM as a PCM or Mp3 stream? Second, if it isn't possible to receive a bit stream via RFCOMM as a PCM or Mp3 stream, how do I convert the received bit stream into audio? Third, how do I convert the received data into audio AND play the audio simultaneously, in "real time"? Can I just use onDataReceived? To be clear, I'm not interested of using the A2DP profile! I want to stream the data via the RFCOMM (SPP Bluetooth profile). The received data stream will be in PCM or Mp3. I thought of writing my own app, but if anyone knows of an app to solve this I'd be glad to hear about it! I'm using Android 2.3 Gingerbread. /Johnny

5 Answers 5

No. Trying to write an Android application that handles this will not be the solution. At least if you want to use A2DP Sink role.

The fact is that Android, as you mentioned it, does not implement the API calls to BlueZ (the bluetooth stack Android uses till Jelly Bean 4.1) regarding A2DP sink capabilities. You have to implement them yourself. I will try to guide you, as I was also interested in doing this my self in the near past.

Your bluetooth-enabled Android device is advertising itself as an A2DP source device by default. You have to change this first, so nearby devices may recognize your device as a sink. To do this, you must modify the audio.conf file (usally located in /etc/bluetooth/) and make sure the Enable key exists and the value Source is attached to this key, so you will get something like :

Reboot, nearby devices should now recognize your device as an A2DP sink .

Now you will have to interact with BlueZ to react appropriately when an A2DP source device will start to stream audio to your phone.

Android and BlueZ are talking to each other via D-BUS . In fact, Android connects to the DBUS_SYSTEM channel and listens to every BlueZ advertisement, such as events, file descriptors .

I remember having successfully bound my self using a native application to this d-bus channel and got access to the various events BlueZ was posting. This is relatively easy to achieve using as reference, the BlueZ API available here. If you go this way, you will have to build a native application (C/C++) and compile it for your platform. You must be able to do this using the Android NDK .

If you find it difficult to use D-BUS , you can try this Java library I just found that handles the communication to D-BUS for you : http://jbluez.sourceforge.net/. I have never used it but it is worth a try in my opinion.

What you really have to do is find out when an A2DP source device is paired to your phone and when he starts to stream music. You can retrieve these events through D-BUS. Once somebody will try to stream music, you need to tell BlueZ that your native application is going to handle it. There is a pretty good document that explains the flow of events that you should handle to do this. This document is accessible here. The part you're interested in comes on page 7. The sink application in the given example is PulseAudio but it could be your application as well.

BlueZ will forward you a UNIX socket when you will call the org.bluez.MediaTransport.Acquire method. Reading on this socket will give you the data that are currently streamed by the remote device. But I remember having been told by a guy working on the BlueZ stack that the data read on this socket are not PCM pure audio, but encoded audio content instead. The data are generally encoded in a format called SBC (Low Complexity Subband Coding).

Decoding SBC is not very difficult, you can find a decoder right here.

The ultimate step would be to forward the PCM audio to your speakers.

To prevent you from getting stuck and in order to test your application in an easier manner, you can use the d-bus binary that should be available on your Android system. He is located in /system/bin.

Quick tests you can make before doing anything of the above might be :

Get Devices list :

dbus-send --system --dest=org.bluez --print-reply / org.bluez.Manager.GetProperties

This returns an array of adapters with their paths. Once you have these path(s) you can retrieve the list of all the bluetooth devices paired with your adapter(s).

Get paired devices :

dbus-send --system --print-reply --dest=org.bluez /org/bluez//hci0 org.bluez.Adapter.GetProperties

This gives you the list of paired devices whithin the Devices array field.

Once you have the list of devices paired to your Bluetooth Adapter, you can know if it is connected to the AudioSource interface.

Get the devices connected to the AudioSource interface :

dbus-send --system --print-reply --dest=org.bluez /org/bluez//hci0/dev_XX_XX_XX_XX_XX_XX org.bluez.AudioSource.GetProperties org.bluez.Manager.GetProperties

Источник

Оцените статью
Adblock
detector