Wie um zu Überprüfen, ob bluetooth-headsets angeschlossen sind auf Android?

Überprüfen, ob es irgendwelche bluetooth-headsets conected auf android im moment?

Wie:

  • Wenn es ein headset conected, der sound muss Weg-headset

  • Aber wenn es nicht ein headset, der Ton muss an bleiben, Lautsprecher

  • Muss diese Kontrolle während der Anwendung, weil, wenn die Batterie des Headsets geht aus senden müssen sound Lautsprecher wieder

Lösung unten /

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 (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.startBluetoothSco();
                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.startBluetoothSco();
            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());
        }
    }
}
  • Gelöst!!!!!!!
  • Ich säuberte gerade Ihr code und verallgemeinert es ein bisschen für andere user mit dem gleichen problem. Poste keine unabhängigen Sachen wie "android.bluetooth.headset.Aktion.FORCE_ON" oder "android.bluetooth.headset.Aktion.FORCE_OFF" (die sich auf Ihrem selbst erstellten Aktionen). Auch nicht verwenden Fremdsprache Kommentare, bitte. 🙂 Btw, danke für den code. 🙂
  • Beste und umfassendste Lösung, die ich gefunden!!!
Schreibe einen Kommentar