Android: Playing sound over Sco Bluetooth headset

后端 未结 4 924
萌比男神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.

提交回复
热议问题