簡體   English   中英

檢查耳機是否已在Android應用程序中連接。 藍牙和有線

[英]Check if headset is connected in Android application. Bluetooth and wired

我正在開發一個簡單的應用程序,我需要跟蹤耳機的連接。 這是代碼的一部分。

在onCreate方法中,有一個broadcastReceiver。 與有線耳機完美搭配。

然后,我為藍牙耳機添加了if-else條件。 如果在啟動應用程序時已經連接了耳機,則表明耳機已連接。 沒關系。 但是,當我關閉藍牙並插入有線耳機時,會收到來自藍牙if-else的Toast:bl耳機disconn。 為什么它沒有趕上耳機插件行動?

public class MainActivity extends AppCompatActivity {

    BroadcastReceiver broadcastReceiver;
    boolean Microphone_Plugged_in = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final String action = intent.getAction();
                int c;
                if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
                    c = intent.getIntExtra("state", -1);
                    if (c == 0) {
                        Microphone_Plugged_in = false;
                        Toast.makeText(getApplicationContext(), "headset is not plugged in", Toast.LENGTH_LONG).show();
                    }
                    if (c == 1) {
                        Microphone_Plugged_in = true;
                        Toast.makeText(getApplicationContext(), "headset is plugged in", Toast.LENGTH_LONG).show();
                    }
                }

                BluetoothAdapter BA;
                BA = BluetoothAdapter.getDefaultAdapter();

                if (BA != null && BluetoothProfile.STATE_CONNECTED == BA.getProfileConnectionState(BluetoothProfile.HEADSET))
                {
                    Microphone_Plugged_in = true;
                    Toast.makeText(getApplicationContext(), "bl headset connected", Toast.LENGTH_LONG).show();
                }
                else {
                    Microphone_Plugged_in = false;
                    Toast.makeText(getApplicationContext(), "bl headset disconn", Toast.LENGTH_LONG).show();
                }


            }
        };
        IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        registerReceiver(broadcastReceiver, receiverFilter);

謝謝

我正在開發一個簡單的應用程序,我需要跟蹤耳機的連接。 這是代碼的一部分。

在onCreate方法中,有一個broadcastReceiver。 與有線耳機完美搭配。

然后,我為藍牙耳機添加了if-else條件。 如果在啟動應用程序時已經連接了耳機,則表明耳機已連接。 沒關系。 但是,當我關閉藍牙並插入有線耳機時,會收到來自藍牙if-else的Toast:bl耳機disconn。 為什么它沒有趕上耳機插件行動?

public class MainActivity extends AppCompatActivity {

    BroadcastReceiver broadcastReceiver;
    boolean Microphone_Plugged_in = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final String action = intent.getAction();
                int c;
                if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
                    c = intent.getIntExtra("state", -1);
                    if (c == 0) {
                        Microphone_Plugged_in = false;
                        Toast.makeText(getApplicationContext(), "headset is not plugged in", Toast.LENGTH_LONG).show();
                    }
                    if (c == 1) {
                        Microphone_Plugged_in = true;
                        Toast.makeText(getApplicationContext(), "headset is plugged in", Toast.LENGTH_LONG).show();
                    }
                }

                BluetoothAdapter BA;
                BA = BluetoothAdapter.getDefaultAdapter();

                if (BA != null && BluetoothProfile.STATE_CONNECTED == BA.getProfileConnectionState(BluetoothProfile.HEADSET))
                {
                    Microphone_Plugged_in = true;
                    Toast.makeText(getApplicationContext(), "bl headset connected", Toast.LENGTH_LONG).show();
                }
                else {
                    Microphone_Plugged_in = false;
                    Toast.makeText(getApplicationContext(), "bl headset disconn", Toast.LENGTH_LONG).show();
                }


            }
        };
        IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        registerReceiver(broadcastReceiver, receiverFilter);

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM