我正在尝试实现一项功能,该功能检查是否启用了蓝牙。我不断得到:无法解析符号“ REQUEST_ENABLE_BLUETOOTH”。我是Java的初学者,刚开始在Android Studio中从事实习项目。

package achal.xylotron.com.bluespot;

import android.app.Activity;
import android.os.Bundle;
import android.bluetooth.*;
import android.content.Intent;

public class BlueChat extends Activity
{

    BluetoothAdapter bluetoothAdapter;

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

        setContentView(R.layout.blue_chat);

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if (!bluetoothAdapter.isEnabled())
        {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BLUETOOTH);
        }
    }
}

最佳答案

初始化REQUEST_ENABLE_BLUETOOTH int如下

package achal.xylotron.com.bluespot;

import android.app.Activity;
import android.os.Bundle;
import android.bluetooth.*;
import android.content.Intent;

public class BlueChat extends Activity
{

    BluetoothAdapter bluetoothAdapter;
    int REQUEST_ENABLE_BLUETOOTH = 0;

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

        setContentView(R.layout.blue_chat);

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if (!bluetoothAdapter.isEnabled())
        {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BLUETOOTH);
        }
    }
}

关于android - 无法解析符号“REQUEST_ENABLE_BLUETOOTH”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50948225/

10-10 16:10