检测对 BluetoothAdapter 所做的状态更改? [英] Detecting state changes made to the BluetoothAdapter?

查看:34
本文介绍了检测对 BluetoothAdapter 所做的状态更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮的应用程序,我用它来打开和关闭 BT.我在那里有以下代码;

I have an app with a button on it that I use to turn BT on and off. I have the following code in there;

public void buttonFlip(View view) {
    flipBT();
    buttonText(view);
}

public void buttonText(View view) {  
    Button buttonText = (Button) findViewById(R.id.button1);
    if (mBluetoothAdapter.isEnabled() || (mBluetoothAdapter.a)) {
        buttonText.setText(R.string.bluetooth_on);  
    } else {
        buttonText.setText(R.string.bluetooth_off);
    }
}

private void flipBT() {
    if (mBluetoothAdapter.isEnabled()) {
        mBluetoothAdapter.disable();    
    } else {
        mBluetoothAdapter.enable();
    }
}

我正在调用按钮 Flip,它翻转 BT 状态,然后调用 ButtonText,它应该更新 UI.但是,我遇到的问题是,BT 需要几秒钟才能打开 - 在这几秒钟内,BT 状态未启用,使我的按钮显示蓝牙关闭,即使它将在 2 秒内打开.

I'm calling button Flip, which flips the BT state, and then calls ButtonText, which should update the UI. However, the issue I'm having is, it takes a few seconds for BT to turn on - and during these seconds, the BT status is not enabled, making my button say Bluetooth off, even if it will be on in 2 seconds.

我在 BluetoothAdapter android 文档中找到了 STATE_CONNECTING 常量,但是......我只是不知道如何使用它,作为一个新手等等.

I found the STATE_CONNECTING constant in the BluetoothAdapter android documentation, but... I simply don't know how to use it, being a newbie and all.

所以,我有两个问题:

  1. 有没有办法动态地将 UI 元素(例如按钮或图像)绑定到 BT 状态,以便当 BT 状态改变时,按钮也会随之改变?
  2. 否则,我想按下按钮并获得正确的状态(我希望它说 BT 打开,即使它只是连接,因为它将在 2 秒内打开).我该怎么做?

推荐答案

您需要注册一个 BroadcastReceiver 来监听 BluetoothAdapter 状态的任何变化:

You will want to register a BroadcastReceiver to listen for any changes in the state of the BluetoothAdapter:

作为 Activity 中的私有实例变量(或在单独的类文件中......无论你喜欢哪个):

As a private instance variable in your Activity (or in a separate class file... whichever one you prefer):

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                                 BluetoothAdapter.ERROR);
            switch (state) {
            case BluetoothAdapter.STATE_OFF:
                setButtonText("Bluetooth off");
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                setButtonText("Turning Bluetooth off...");
                break;
            case BluetoothAdapter.STATE_ON:
                setButtonText("Bluetooth on");
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                setButtonText("Turning Bluetooth on...");
                break;
            }
        }
    }
};

请注意,这里假设您的 Activity 实现了一个方法 setButtonText(String text),它将相应地更改 Button 的文本.

Note that this assumes that your Activity implements a method setButtonText(String text) that will change the Button's text accordingly.

然后在你的 Activity 中,注册和注销 BroadcastReceiver 如下,

And then in your Activity, register and unregister the BroadcastReceiver as follows,

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* ... */

    // Register for broadcasts on BluetoothAdapter state change
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mReceiver, filter);
}

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

    /* ... */

    // Unregister broadcast listeners
    unregisterReceiver(mReceiver);
}

这篇关于检测对 BluetoothAdapter 所做的状态更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆