检查Android中是否启用了蓝牙时出错(REQUEST_ENABLE_BT无法解析为变量)

Ser*_*uan 47 android bluetooth

我正在尝试这样做:

    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);
        }   
    }
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

REQUEST_ENABLE_BT无法解析为变量

我该如何解决?

Dee*_*eeV 105

REQUEST_ENABLE_BT是您提供的请求代码.它实际上只是您提供的一个数字onActivityResult.它将是活动返回时的requestCode(第一个参数)onActivityResult.你可以输入你想要的任何数字,只要它在return方法中是一致的.

换句话说,将这样的行放在Activity的顶部:

private final static int REQUEST_ENABLE_BT = 1;


Sou*_*jan 5

文档说,传递给startActivityForResult()的REQUEST_ENABLE_BT常量是本地定义的整数(必须大于0),系统将在onActivityResult()实现中将其作为requestCode参数传递给您。