错误检查如果蓝牙Android中启用(REQUEST_ENABLE_BT不能被解析为一个变量)蓝牙、变量、错误、Android

2023-09-05 05:23:17 作者:前不凸后不翘的性感女人

我试图做到这一点:

    BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
    if (bt == null){
        //Does not support Bluetooth
        status.setText("Your device does not support Bluetooth");
    }else{
        //Magic starts. Let's check if it's enabled
        if (!bt.isEnabled()){
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }   
    }

不过,我得到这个错误:

But I get this error:

REQUEST_ENABLE_BT不能被解析为一个变量

REQUEST_ENABLE_BT cannot be resolved to a variable

我该如何解决这个问题?

How can I fix it?

推荐答案

REQUEST_ENABLE_BT 为您提供一个请求code。这真的只是一个数字,你为 onActivityResult 。这将是请求code onActivityResult (第一个参数)活动时的回报。你可以把你想,只要​​它是在返回的方法是一致的任何数字。

REQUEST_ENABLE_BT is a request code that you provide. It's really just a number that you provide for onActivityResult. It will be the requestCode (first parameter) of onActivityResult when the activity returns. You could put any number you want as long as it's consistent in the return method.

在换句话说,把这样一行在活动的顶部:

In other words, put a line like this in the top of your Activity:

私人最终静态INT REQUEST_ENABLE_BT = 1;