小编Art*_*nti的帖子

如何验证Android上是否连接了蓝牙耳机?

如何验证目前是否有任何蓝牙耳机连接到android?

喜欢:

  • 如果有耳机连接,声音必须通过此耳机

  • 但如果没有耳机,声音必须留在扬声器上

  • 这必须在应用期间检查,因为如果耳机的电池熄灭,它必须向扬声器发回声音

解决方案/

public class BluetoothReceiver extends BroadcastReceiver {
    private AudioManager localAudioManager;
    private static final int STATE_DISCONNECTED  = 0x00000000;
    private static final String EXTRA_STATE = "android.bluetooth.headset.extra.STATE";
    private static final String TAG = "BluetoothReceiver";
    private static final String ACTION_BT_HEADSET_STATE_CHANGED  = "android.bluetooth.headset.action.STATE_CHANGED";
    private static final String ACTION_BT_HEADSET_FORCE_ON = "android.bluetooth.headset.action.FORCE_ON";
    private static final String ACTION_BT_HEADSET_FORCE_OFF = "android.bluetooth.headset.action.FORCE_OFF";

    @Override
    public void onReceive(final Context context, final Intent intent) {
        Log.i(TAG,"onReceive - BluetoothBroadcast");
        localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        final String action = intent.getAction();
        if …
Run Code Online (Sandbox Code Playgroud)

audio android bluetooth headset

18
推荐指数
1
解决办法
6695
查看次数

标签 统计

android ×1

audio ×1

bluetooth ×1

headset ×1