小编Mil*_*ill的帖子

如何通过点击请求Android用户启用蓝牙?

来自http://developer.android.com/guide/topics/connectivity/bluetooth.html我知道我需要以下内容来请求用户启用他的BT:

if (!mBluetoothAdapter.isEnabled()) 
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
Run Code Online (Sandbox Code Playgroud)

但问题是如何在课堂上使用它?为什么我的代码会在点击此活动的按钮时崩溃:

public class Opponents extends Activity 
{
      private final static int REQUEST_ENABLE_BT=1;
      BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

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

        final Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() 
        {
          public void onClick(View view)
          {
            if(!mBluetoothAdapter.isEnabled())
            {
              Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
              startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
          }
        });
}
Run Code Online (Sandbox Code Playgroud)

android bluetooth

8
推荐指数
2
解决办法
1万
查看次数

标签 统计

android ×1

bluetooth ×1