[JAVA] [Android] Always detect ON / OFF switching of Bluetooth headset

Thing you want to do

--Always detect switching when Bluetooth is turned on / off on an Android device ――I want to always detect it, not at the first startup etc. --Call the hoge method when Bluetooth is turned on --Call the foo method when Bluetooth is turned off

Sample project https://github.com/ry0takaha4/android-bluetooth-on-off-sample-app

environment

Premise

--The following Bluetooth Permission is set in AndroidManifest.xml

  <uses-permission android:name="android.permission.BLUETOOTH"/>
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

How to do

Define a listener

// MainActivity.java
  /**
   *Bluetooth connection detection listener
   *Called every time the Bluetooth function of the main unit is turned on / off
   */
  private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
      System.out.println("Bluetooth is turned on.");
      hoge();
      if (profile == BluetoothProfile.HEADSET) {
        mBluetoothHeadset = proxy;
      }
    }

    public void onServiceDisconnected(int profile) {
      System.out.println("Bluetooth is turned off.");
      foo();
      if (profile == BluetoothProfile.HEADSET) {
        mBluetoothHeadset = null;
      }
    }
  };

Register and cancel proxy

// MainActivity.java

  private BluetoothAdapter mBluetoothAdapter;
  private BluetoothProfile mBluetoothHeadset;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Bluetooth enabled check
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter != null) {
      //This time Bluetooth Profile.HEADSET, but anything is fine as long as it is detected
      mBluetoothAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.HEADSET);
    } else {
      System.out.println("Bluetooth is not supported or disabled on this device.");
    }

  }

  @Override
  protected void onDestroy() {
    super.onDestroy();

    //Close profile proxy connection to service
    if (mBluetoothAdapter != null && mBluetoothHeadset != null) {
      mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
    }

  }

knowledge

Do not enable Bluetooth without user consent

Bluetooth should never be enabled without direct user consent. If you want to turn on Bluetooth in order to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that requests user permission to turn on Bluetooth. The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.

Source: https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#enable ()

When you want to enable Bluetooth Do not enable Bluetooth without the user's consent. If you want to enable it, you should use ʻACTION_REQUEST_ENABLE` Intent and display a dialog asking for the user's permission.

Recommended Posts

[Android] Always detect ON / OFF switching of Bluetooth headset
How to detect microphone conflicts on Android
Browse an instance of Tab's View on Android