0

我是 android 新手,我正在开发简单的应用程序,用于在启动主要活动时检查蓝牙连接。下面是我的代码。怎么了...?我只想先检查蓝牙连接。我应该将该方法放在其他地方然后 onCreate() 方法吗?

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.bluetooth.*;
import android.content.*;

public class MainActivity extends Activity {

public void testBT(){
    BluetoothAdapter mAdp = BluetoothAdapter.getDefaultAdapter();

    if(!mAdp.isEnabled())
    {
        Intent btIntent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(btIntent,1);
    }
  }
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    testBT();

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

4

1 回答 1

0

究竟是什么问题?您是否遇到空指针异常?

如果是这样,请尝试添加此条件

if(mAdp == null){
   // this means that your device does not support bluetooth
}
于 2013-08-11T08:51:58.383 回答