LoginSignup
3
1

More than 5 years have passed since last update.

【Android】BluetoothのヘッドセットのON/OFFの切り替えを常に検知する

Last updated at Posted at 2018-02-23

やりたいこと

  • Android端末でBluetoothをON/OFFしたときの切り替えを常に検知する
    • 初回起動時等ではなく、常に検知したい
  • BluetoothがONになったときhogeメソッドを呼び出す
  • BluetoothがOFFになったときfooメソッドを呼び出す

サンプルプロジェクト
https://github.com/ry0takaha4/android-bluetooth-on-off-sample-app

環境

  • Android 5.0

前提

  • AndroidManifest.xmlで以下のBluetoothに関するPermissionを設定している
  <uses-permission android:name="android.permission.BLUETOOTH"/>
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

どうやるか

リスナーを定義する

// MainActivity.java
  /**
   * Bluetooth接続検知リスナー
   * 本体のBluetooth機能をオン・オフするたびに呼ばれる
   */
  private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
      System.out.println("BluetoothがONになりました。");
      hoge();
      if (profile == BluetoothProfile.HEADSET) {
        mBluetoothHeadset = proxy;
      }
    }

    public void onServiceDisconnected(int profile) {
      System.out.println("BluetoothがOFFになりました。");
      foo();
      if (profile == BluetoothProfile.HEADSET) {
        mBluetoothHeadset = null;
      }
    }
  };

proxyの登録、解除処理を行う

// MainActivity.java

  private BluetoothAdapter mBluetoothAdapter;
  private BluetoothProfile mBluetoothHeadset;

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

    // Bluetooth 有効チェック
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter != null) {
      // 今回はBluetoothProfile.HEADSETだが、検知するだけであればなんでも良い
      mBluetoothAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.HEADSET);
    } else {
      System.out.println("この端末ではBluetoothはサポートされていないか、無効になっています。");
    }

  }

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

    // サービスへのプロファイルプロキシの接続を閉じる
    if (mBluetoothAdapter != null && mBluetoothHeadset != null) {
      mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
    }

  }

知識

ユーザーの同意無しにBluetoothを有効にしてはいけない

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.

引用元:https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#enable()

Bluetoothを有効にさせたいときユーザの同意なしにBluetoothを有効にしてはいけない。
有効にさせたい場合は ACTION_REQUEST_ENABLE Intentを使用し、ユーザの許可を求めるダイアログを表示するべき

※BluetoothAdapter.enable()メソッドを使うとユーザの同意無しにBluetoothをONにすることができるけど、
それは「パワーマネージャー」アプリなど、システム設定を変更するためのユーザインターフェイスを含むアプリケーションとか用の処理らしい

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1