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

发布于 2024-12-21 03:39:38 字数 117 浏览 0 评论 0原文

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

How to get Bluetooth Headset connection state (connected/disconnected) using android api level 8 on application start-up?
Is there any sticky broadcast intent available in android2.2?
Is there any API to fetch initial Bluetooth Headset state?
Is there any workaround available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如若梦似彩虹 2024-12-28 03:39:38

我想出了解决方案:

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:

        }
}
};

I figured out the solution:

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:

        }
}
};
Bonjour°[大白 2024-12-28 03:39:38

来自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

from 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
违心° 2024-12-28 03:39:38

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

声明以下意图过滤器

        <intent-filter >
            <action android:name="android.bluetooth.headset.action.AUDIO_STATE_CHANGED" />
        </intent-filter>

并在您的接收器中的 onReceive 检查:

if ("android.bluetooth.headset.action.AUDIO_STATE_CHANGED".equals(intent.getAction())) {
  headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);
}

并保存int 作为静态变量。您可以随时访问它,了解 BT 音频是否已连接 (1) / 已断开连接 (0)。不漂亮,但完成了工作。

另请查看:
https://github.com/android/platform_frameworks_base /blob/gingerbread/core/java/android/bluetooth/BluetoothHeadset.java

This won't really work on application startup, but as intent-filters are registered automatically, you might have a valid value, once your app gets started up:

Declare the following intent-filter

        <intent-filter >
            <action android:name="android.bluetooth.headset.action.AUDIO_STATE_CHANGED" />
        </intent-filter>

and in your Receiver in onReceive check for:

if ("android.bluetooth.headset.action.AUDIO_STATE_CHANGED".equals(intent.getAction())) {
  headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);
}

and save the int as a static variable. Access it anytime you want to know if BT audio is connected(1) / disconnected(0). Not pretty, but gets the job done.

Also check out:
https://github.com/android/platform_frameworks_base/blob/gingerbread/core/java/android/bluetooth/BluetoothHeadset.java

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文