Skip to content

Commit

Permalink
BLUETOOTH_CONNECT が不要な Intent に切り替える
Browse files Browse the repository at this point in the history
  • Loading branch information
tnoho committed Feb 16, 2024
1 parent a9f67bc commit 9135f43
Showing 1 changed file with 27 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothHeadset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;

Expand All @@ -44,37 +44,19 @@ public void onReceive(Context context, Intent intent) {
if (action == null) {
return;
}
/*
* ヘッドセットプロファイルの状態変化
* 他のオーディオデバイスを使用中に Bluetooth をオンにすると発火します
*/
Log.d(TAG, "BluetoothHeadsetBroadcastReceiver.onReceive: "
+ "a=" + action);
if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
if (action.equals(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)) {
final int state =
intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,
AudioManager.SCO_AUDIO_STATE_DISCONNECTED);
Log.d(TAG, "BluetoothHeadsetBroadcastReceiver.onReceive: "
+ "a=ACTION_CONNECTION_STATE_CHANGED, "
+ "s=" + SoraBluetoothManager.stateToString(state) + ", "
+ "a=ACTION_SCO_AUDIO_STATE_UPDATED, "
+ "s=" + SoraAudioManager2.stateToString(state) + ", "
+ "sb=" + isInitialStickyBroadcast());
if (state == BluetoothHeadset.STATE_CONNECTED) {
// Bluetooth ヘッドセットとが接続された
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
// SCO 接続された
updateAudioDeviceState();
} else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
// おそらく Bluetooth が通話中に切られた
updateAudioDeviceState();
}
} else if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
/* SCO の状態変化 */
final int state = intent.getIntExtra(
BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
Log.d(TAG, "BluetoothHeadsetBroadcastReceiver.onReceive: "
+ "a=ACTION_AUDIO_STATE_CHANGED, "
+ "s=" + SoraBluetoothManager.stateToString(state) + ", "
+ "sb=" + isInitialStickyBroadcast());
if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
updateAudioDeviceState();
} else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
} else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
// SCO が切られた
updateAudioDeviceState();
}
}
Expand Down Expand Up @@ -115,10 +97,8 @@ public void start(OnChangeRouteObserver observer) {

// HEADSET プロファイルの状態変化を bluetoothHeadsetReceiver で取れるようにする
IntentFilter filter = new IntentFilter();
// bluetooth ヘッドセットの接続状態変化を取得する
filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
// bluetooth ヘッドセットのオーディオの状態変化を取得する
filter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
// bluetooth SCO の状態変化を取得する
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
context.registerReceiver(bluetoothHeadsetReceiver, filter);

// 初期化を行った状態でデバイスの設定を行う
Expand Down Expand Up @@ -242,4 +222,19 @@ void updateAudioDeviceState() {
}
}
}

static String stateToString(int state) {
switch (state) {
case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
return "DISCONNECTED";
case AudioManager.SCO_AUDIO_STATE_CONNECTED:
return "CONNECTED";
case AudioManager.SCO_AUDIO_STATE_CONNECTING:
return "CONNECTING";
case AudioManager.SCO_AUDIO_STATE_ERROR:
return "ERROR";
default:
return "INVALID";
}
}
}

0 comments on commit 9135f43

Please sign in to comment.