Quantcast
Viewing all articles
Browse latest Browse all 3348

Android Bluetooth機器(HSP/HFP)のボタンイベントの受け取り方法

Bluetoothで接続したヘッドセット(HSP/HFP)のボタンイベントの取得方法をやっと見つけたのでメモ

ActivityとBroadcastReceiver で各ボタンの操作を受け取る事ができる事がわかったのですがBluetooth 機器毎に ボタンの機能は異なるようで、手持ちの機器だと下のintent.actionが関連付いていました。

LBT-HS10PCBKImage may be NSFW.
Clik here to view.

android.intent.action.VOICE_COMMAND

LBT-SPCB01AVImage may be NSFW.
Clik here to view.

android.intent.action.MEDIA_BUTTON

参考コード

AndroidManifest.xml

Activityの設定
intent-filter android.intent.action.VOICE_COMMAND と launchMode singleTop をActivityに設定する。

    <activity android:name=".HomeActivity"
              android:label="@string/app_name"
              android:launchMode="singleTop">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <intent-filter>
        <action android:name="android.intent.action.VOICE_COMMAND" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    </activity>

BluetoothControlReceiverの設定
intent-filter に android.intent.action.MEDIA_BUTTONを設定する。

    <receiver android:name=".bluetooth.BluetoothControlReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

Activity

public class HomeActivity extends Activity
{
    private static String TAG = "HomeActivity";

    /** VOICE_COMMANDからActivityを起動されたとき
     *
     * @param intent
     */
    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        Log.d(TAG, "onNewIntent()");
        //TODO: android.intent.action.VOICE_COMMAND で起動された
    }
...
}

BluetoothControlReceiver

public class BluetoothControlReceiver extends BroadcastReceiver {
    private static String TAG = "BluetoothControlReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive");
        String action = intent.getAction();
        Toast.makeText(context, "onReceive action:"+action, Toast.LENGTH_SHORT).show();
        if(action.equals(Intent.ACTION_MEDIA_BUTTON)){
            //TODO: MEDIA_BUTTON を押された
        }
    }
}

参考リンク
Androidからイヤフォンやヘルス機器とBluetooth通信するには
BluetoothHeadsetについての情報


Viewing all articles
Browse latest Browse all 3348

Trending Articles