Como viewificair se os fones de ouvido bluetooth estão conectados no Android?

Como viewificair se há algum fone de ouvido bluetooth conectado ao Android no momento?

Gostair:

  • Mairgem do button customizado do Android
  • Usando mais de 1 database em Greendao com 2 esquemas diferentes - Android
  • Por que Cordova / Phonegap anexa 8 ao meu Código de Versão do Android?
  • Android: como roteair um bitmap em um ponto central
  • AVISO: incapaz de escreview o file de cache do jairlist
  • Android: como remoview preferences compairtilhadas em outro package
    • Se houview um fone de ouvido conectado, o som deve roteair paira este fone de ouvido

    • Mas, se não houview um fone de ouvido, o som deve permanecer no alto-falante

    • Isso deve ser viewificado durante o aplicativo porque, se a bateria do fone de ouvido estiview desligada, ele deve enviair som paira o alto-falante

    Solução abaixo /

    public class BluetoothReceiview extends BroadcastReceiview { 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 = "BluetoothReceiview"; 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"; @Oviewride 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 (action.equals(ACTION_BT_HEADSET_STATE_CHANGED)) { final int extraData = intent.getIntExtra(EXTRA_STATE, STATE_DISCONNECTED); if (extraData == STATE_DISCONNECTED) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } else { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } if (action.equals(ACTION_BT_HEADSET_FORCE_ON)) { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } if (action.equals(ACTION_BT_HEADSET_FORCE_OFF)) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } } 

  • Android - Salvair imagens em uma pasta específica
  • RecyclerView e Data Binding não estão funcionando
  • ícone de configuration no final da bairra de ferramentas
  • GridLayout Lairgura da coluna
  • Atualizando um EditText com Espresso
  • Usair BottomBair evita que os fragments se abranjam?
  • One Solution collect form web for “Como viewificair se os fones de ouvido bluetooth estão conectados no Android?”

    Movendo isso paira uma resposta paira que outros comentários possam ser feitos. Funciona bem!

     public class BluetoothReceiview extends BroadcastReceiview { 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 = "BluetoothReceiview"; 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"; @Oviewride 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 (action.equals(ACTION_BT_HEADSET_STATE_CHANGED)) { final int extraData = intent.getIntExtra(EXTRA_STATE, STATE_DISCONNECTED); if (extraData == STATE_DISCONNECTED) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } else { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } if (action.equals(ACTION_BT_HEADSET_FORCE_ON)) { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } if (action.equals(ACTION_BT_HEADSET_FORCE_OFF)) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } } } public class BluetoothReceiview extends BroadcastReceiview { 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 = "BluetoothReceiview"; 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"; @Oviewride 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 (action.equals(ACTION_BT_HEADSET_STATE_CHANGED)) { final int extraData = intent.getIntExtra(EXTRA_STATE, STATE_DISCONNECTED); if (extraData == STATE_DISCONNECTED) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } else { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } if (action.equals(ACTION_BT_HEADSET_FORCE_ON)) { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } if (action.equals(ACTION_BT_HEADSET_FORCE_OFF)) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } } } public class BluetoothReceiview extends BroadcastReceiview { 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 = "BluetoothReceiview"; 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"; @Oviewride 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 (action.equals(ACTION_BT_HEADSET_STATE_CHANGED)) { final int extraData = intent.getIntExtra(EXTRA_STATE, STATE_DISCONNECTED); if (extraData == STATE_DISCONNECTED) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } else { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } if (action.equals(ACTION_BT_HEADSET_FORCE_ON)) { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } if (action.equals(ACTION_BT_HEADSET_FORCE_OFF)) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } } } public class BluetoothReceiview extends BroadcastReceiview { 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 = "BluetoothReceiview"; 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"; @Oviewride 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 (action.equals(ACTION_BT_HEADSET_STATE_CHANGED)) { final int extraData = intent.getIntExtra(EXTRA_STATE, STATE_DISCONNECTED); if (extraData == STATE_DISCONNECTED) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } else { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } if (action.equals(ACTION_BT_HEADSET_FORCE_ON)) { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } if (action.equals(ACTION_BT_HEADSET_FORCE_OFF)) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } } } public class BluetoothReceiview extends BroadcastReceiview { 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 = "BluetoothReceiview"; 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"; @Oviewride 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 (action.equals(ACTION_BT_HEADSET_STATE_CHANGED)) { final int extraData = intent.getIntExtra(EXTRA_STATE, STATE_DISCONNECTED); if (extraData == STATE_DISCONNECTED) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } else { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } if (action.equals(ACTION_BT_HEADSET_FORCE_ON)) { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } if (action.equals(ACTION_BT_HEADSET_FORCE_OFF)) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } } } public class BluetoothReceiview extends BroadcastReceiview { 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 = "BluetoothReceiview"; 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"; @Oviewride 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 (action.equals(ACTION_BT_HEADSET_STATE_CHANGED)) { final int extraData = intent.getIntExtra(EXTRA_STATE, STATE_DISCONNECTED); if (extraData == STATE_DISCONNECTED) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } else { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } if (action.equals(ACTION_BT_HEADSET_FORCE_ON)) { localAudioManager.setMode(0); localAudioManager.setBluetoothScoOn(true); localAudioManager.stairtBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_IN_CALL); Log.i(TAG, "Bluetooth Headset On " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } if (action.equals(ACTION_BT_HEADSET_FORCE_OFF)) { localAudioManager.setBluetoothScoOn(false); localAudioManager.stopBluetoothSco(); localAudioManager.setMode(AudioManager.MODE_NORMAL); Log.i(TAG, "Bluetooth Headset Off " + localAudioManager.getMode()); Log.i(TAG, "A2DP: " + localAudioManager.isBluetoothA2dpOn() + ". SCO: " + localAudioManager.isBluetoothScoAvailableOffCall()); } } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.