如何使用Android2.2在应用程序启动时获取蓝牙耳机连接状态?

如何使用Android2.2在应用程序启动时获取蓝牙耳机连接状态?,android,bluetooth,headset,Android,Bluetooth,Headset,如何在应用程序启动时使用android api level 8获取蓝牙耳机连接状态(已连接/已断开)? android2.2中是否有任何粘性广播意图? 是否有获取蓝牙耳机初始状态的API? 有什么解决办法吗 我想出了解决办法: private static final String ACTION_BT_HEADSET_STATE_CHANGED = "android.bluetooth.headset.action.STATE_CHANGED"; private static final in

如何在应用程序启动时使用android api level 8获取蓝牙耳机连接状态(已连接/已断开)? android2.2中是否有任何粘性广播意图? 是否有获取蓝牙耳机初始状态的API?
有什么解决办法吗

我想出了解决办法:

private static final String ACTION_BT_HEADSET_STATE_CHANGED  = "android.bluetooth.headset.action.STATE_CHANGED";
private static final int STATE_CONNECTED = 0x00000002; 
private static final int STATE_DISCONNECTED  = 0x00000000;  
private static final String EXTRA_STATE = "android.bluetooth.headset.extra.STATE";

private BroadcastReceiver mBlueToothHeadSetEventReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
    try {
        String action = intent.getAction();

        if(action == null)
            return;

        if(action.equals(ACTION_BT_HEADSET_STATE_CHANGED)){
            int extraData = intent.getIntExtra(EXTRA_STATE  , STATE_DISCONNECTED);
            if(extraData == STATE_CONNECTED ){

                //TODO :

            }else if(extraData == STATE_DISCONNECTED){

                //TODO:
            }
        }
        } catch (Exception e) {

        //TODO:

        }
}
};

这在应用程序启动时不会真正起作用,但由于意图过滤器是自动注册的,一旦应用程序启动,您可能会有一个有效值:

声明以下意图过滤器

        <intent-filter >
            <action android:name="android.bluetooth.headset.action.AUDIO_STATE_CHANGED" />
        </intent-filter>
并将int保存为静态变量。只要您想知道BT音频是否已连接(1)/已断开连接(0),请随时访问它。虽然不漂亮,但是完成了任务

另外,请查看:
来自packages/apps/Phone/src/com/android/Phone/PhoneGlobals.java:1449

        } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
            mBluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
                                                      BluetoothHeadset.STATE_DISCONNECTED);
            if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_STATE_CHANGED_ACTION");
            if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetState);
            updateBluetoothIndication(true);  // Also update any visible UI if necessary

在我的设备(Motorola Milestone 2又名Droid 2)上,此意图(android.bluetooth.headset.action.STATE_CHANGED)似乎不粘性,这意味着在应用程序启动时,为此意图注册BroadcastReceiver时,即使耳机处于连接状态,它也始终返回NULL。我还为android.media.SCO_AUDIO_STATE_注册了一个过滤器,它会在启动时报告音频状态,这当然是不够的,因为在大多数情况下,这将被SCO_AUDIO_STATE_断开,并不意味着没有连接耳机。这是一种特定于设备的行为吗?依我看,这不会有帮助。如果存在活动的音频连接,则会显示。如果已连接耳机,则不会自动建立音频连接。但是,您应该在自己获取音频连接时(通过入侵startBluetoothSco())最新注册此事件,因为这样您就可以在音频通道准备好使用时对连接的音频状态做出反应,启动您需要执行的任何操作。
        } else if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
            mBluetoothHeadsetState = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE,
                                                      BluetoothHeadset.STATE_DISCONNECTED);
            if (VDBG) Log.d(LOG_TAG, "mReceiver: HEADSET_STATE_CHANGED_ACTION");
            if (VDBG) Log.d(LOG_TAG, "==> new state: " + mBluetoothHeadsetState);
            updateBluetoothIndication(true);  // Also update any visible UI if necessary