Programmatically connect to paired Bluetooth speaker and play audio

前端 未结 4 1638
陌清茗
陌清茗 2020-12-15 22:59

In our app, I\'d like to connect to a previously paired A2DP Bluetooth Speaker and direct audio playback to it, using Android v4.2 or later.

I can successfully creat

4条回答
  •  粉色の甜心
    2020-12-15 23:56

    This one works for me. After receive BluetoothA2dp.STATE_CONNECTED, you can play music as normal.

    public class A2DPActivity extends Activity {
    
    protected static final String TAG = "ZS-A2dp";
    
    Button mBtPlay;
    
    BluetoothAdapter mBtAdapter;
    BluetoothA2dp mA2dpService;
    
    AudioManager mAudioManager;
    MediaPlayer mPlayer;
    
    BroadcastReceiver mReceiver = new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context ctx, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, "receive intent for action : " + action);
            if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) {
                int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
                if (state == BluetoothA2dp.STATE_CONNECTED) {
                    setIsA2dpReady(true);
                    playMusic();
                } else if (state == BluetoothA2dp.STATE_DISCONNECTED) {
                    setIsA2dpReady(false);
                }
            } else if (action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)) {
                int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING);
                if (state == BluetoothA2dp.STATE_PLAYING) {
                    Log.d(TAG, "A2DP start playing");
                    Toast.makeText(A2DPActivity.this, "A2dp is playing", Toast.LENGTH_SHORT).show();
                } else {
                    Log.d(TAG, "A2DP stop playing");
                    Toast.makeText(A2DPActivity.this, "A2dp is stopped", Toast.LENGTH_SHORT).show();
                }
            }
        }
    
    };
    
    boolean mIsA2dpReady = false;
    void setIsA2dpReady(boolean ready) {
        mIsA2dpReady = ready;
        Toast.makeText(this, "A2DP ready ? " + (ready ? "true" : "false"), Toast.LENGTH_SHORT).show();
    }
    
    private ServiceListener mA2dpListener = new ServiceListener() {
    
        @Override
        public void onServiceConnected(int profile, BluetoothProfile a2dp) {
            Log.d(TAG, "a2dp service connected. profile = " + profile);
            if (profile == BluetoothProfile.A2DP) {
                mA2dpService = (BluetoothA2dp) a2dp;
                if (mAudioManager.isBluetoothA2dpOn()) {
                    setIsA2dpReady(true);
                    playMusic();
                } else {
                    Log.d(TAG, "bluetooth a2dp is not on while service connected");
                }
            }
        }
    
        @Override
        public void onServiceDisconnected(int profile) {
            setIsA2dpReady(false);
        }
    
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        setContentView(ll);
    
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        registerReceiver(mReceiver, new IntentFilter(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED));
        registerReceiver(mReceiver, new IntentFilter(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED));
    
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        mBtAdapter.getProfileProxy(this, mA2dpListener , BluetoothProfile.A2DP);
    
    }
    
    @Override
    protected void onDestroy() {
        mBtAdapter.closeProfileProxy(BluetoothProfile.A2DP, mA2dpService);
        releaseMediaPlayer();
        unregisterReceiver(mReceiver);
        super.onDestroy();
    }
    
    @Override
    protected void onPause() {
        releaseMediaPlayer();
        super.onPause();
    }
    
    private void releaseMediaPlayer() {
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
    
    private void playMusic() {
        mPlayer = new MediaPlayer();
        AssetManager assetManager = this.getAssets();
        AssetFileDescriptor fd;
        try {
            fd = assetManager.openFd("Radioactive.mp3");
            Log.d(TAG, "fd = " + fd);
            mPlayer.setDataSource(fd.getFileDescriptor());
            mPlayer.prepare();
            Log.d(TAG, "start play music");
            mPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    }

提交回复
热议问题