Android: Playing sound over Sco Bluetooth headset

后端 未结 4 918
萌比男神i
萌比男神i 2021-01-31 12:34

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 do

相关标签:
4条回答
  • 2021-01-31 12:57

    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.

    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="+action);
        }
    }
    

    I don't know if it is still working with the new APIs.

    0 讨论(0)
  • 2021-01-31 12:58

    I actually managed to fix this. The problem was that my pitch that I played over the headset was too high of a frequency. I fixed it by simply doubling up the code that creates the audio file so that it goes HIGH HIGH LOW LOW instead of HIGH LOW HIGH LOW.

    0 讨论(0)
  • 2021-01-31 13:06

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

    0 讨论(0)
  • 2021-01-31 13:13

    Add to manifest:

    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    
    0 讨论(0)
提交回复
热议问题