在Android上,我可以注册一个回叫,告诉我蓝牙是打开还是关闭的吗?

在Android上,我可以注册一个回叫,告诉我蓝牙是打开还是关闭的吗?,android,android-activity,bluetooth,android-lifecycle,Android,Android Activity,Bluetooth,Android Lifecycle,我需要在我的应用程序中知道蓝牙是开还是关。或者如果蓝牙已打开或关闭,例如从操作系统设置下拉菜单。我想我可以在活动的onResume()中这样做。但事实证明,当Android操作系统的设置“下拉菜单”(即用手指从屏幕上沿向下拉动的菜单)打开时,活动并没有暂停 当蓝牙可用或不可用时,我需要更新我的用户界面 我可以注册一个回调(例如,BroadcastReceiver)或任何其他回调,让系统在蓝牙可用性发生变化时通知我吗?这是您需要的意图过滤器: <intent-filter>

我需要在我的应用程序中知道蓝牙是开还是关。或者如果蓝牙已打开或关闭,例如从操作系统设置下拉菜单。我想我可以在活动的
onResume()
中这样做。但事实证明,当Android操作系统的设置“下拉菜单”(即用手指从屏幕上沿向下拉动的菜单)打开时,活动并没有暂停

当蓝牙可用或不可用时,我需要更新我的用户界面


我可以注册一个回调(例如,
BroadcastReceiver
)或任何其他回调,让系统在蓝牙可用性发生变化时通知我吗?

这是您需要的意图过滤器:

<intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>

您可以使用意向过滤器注册接收器:

<receiver
        android:name="com.example.BluetoothReceiver"
        android:enabled="true">
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
    </intent-filter>
</receiver>
或者,如果您想直接在活动中添加:

public class ExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

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

            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);

                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        //Indicates the local Bluetooth adapter is off.
                        break;

                    case BluetoothAdapter.STATE_TURNING_ON:
                        //Indicates the local Bluetooth adapter is turning on. However local clients should wait for STATE_ON before attempting to use the adapter.
                        break;

                    case BluetoothAdapter.STATE_ON:
                        //Indicates the local Bluetooth adapter is on, and ready for use.
                        break;

                    case BluetoothAdapter.STATE_TURNING_OFF:
                        //Indicates the local Bluetooth adapter is turning off. Local clients should immediately attempt graceful disconnection of any remote links.
                        break;
                }
            }
        }
    };
}

您可以找到完整性的完整解决方案,如何将其添加到活动中?@treesareywhere我刚刚用直接添加到为我工作的
活动中的
BroadcastReceiver
的示例更新了我的答案。我的另一个选择是延长时间!!!但这个是救世主。
public class ExampleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

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

            if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);

                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        //Indicates the local Bluetooth adapter is off.
                        break;

                    case BluetoothAdapter.STATE_TURNING_ON:
                        //Indicates the local Bluetooth adapter is turning on. However local clients should wait for STATE_ON before attempting to use the adapter.
                        break;

                    case BluetoothAdapter.STATE_ON:
                        //Indicates the local Bluetooth adapter is on, and ready for use.
                        break;

                    case BluetoothAdapter.STATE_TURNING_OFF:
                        //Indicates the local Bluetooth adapter is turning off. Local clients should immediately attempt graceful disconnection of any remote links.
                        break;
                }
            }
        }
    };
}