我是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;
}
}
答案 0 :(得分:0)
究竟是什么问题?您是否遇到空指针异常?
如果是,请尝试添加此条件
if(mAdp == null){
// this means that your device does not support bluetooth
}