如何在Android中打开/关闭蓝牙?

时间:2012-10-05 17:33:10

标签: android bluetooth

我一直试图通过以下代码获取蓝牙在Android中的状态: 我在BluetoothAdapter方法中实例化onCreate() ...并像这样调用blue.isEnabled()

if(blue.isEnabled()){
....
}

但是我一直收到这个错误..是的,我已经宣布了Bluetooth

的所有权限

错误记录

10-05 21:48:16.881: E/AndroidRuntime(538): FATAL EXCEPTION: main
10-05 21:48:16.881: E/AndroidRuntime(538): java.lang.NullPointerException
10-05 21:48:16.881: E/AndroidRuntime(538):  at com.emazdoor.remind.me.main.onClick(main.java:58)
10-05 21:48:16.881: E/AndroidRuntime(538):  at android.view.View.performClick(View.java:2485)
10-05 21:48:16.881: E/AndroidRuntime(538):  at android.view.View$PerformClick.run(View.java:9080)
10-05 21:48:16.881: E/AndroidRuntime(538):  at android.os.Handler.handleCallback(Handler.java:587)
10-05 21:48:16.881: E/AndroidRuntime(538):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-05 21:48:16.881: E/AndroidRuntime(538):  at android.os.Looper.loop(Looper.java:123)
10-05 21:48:16.881: E/AndroidRuntime(538):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-05 21:48:16.881: E/AndroidRuntime(538):  at java.lang.reflect.Method.invokeNative(Native Method)
10-05 21:48:16.881: E/AndroidRuntime(538):  at java.lang.reflect.Method.invoke(Method.java:507)
10-05 21:48:16.881: E/AndroidRuntime(538):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-05 21:48:16.881: E/AndroidRuntime(538):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-05 21:48:16.881: E/AndroidRuntime(538):  at dalvik.system.NativeStart.main(Native Method)

请帮忙!感谢

完整代码

public class main extends Activity implements OnClickListener{

    Button CallButton, SMSButton, BirthdayButton, BusinessButton; 
    BluetoothAdapter b1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        b1 = BluetoothAdapter.getDefaultAdapter();

        Initialiser();

    }

    private void Initialiser() {

        CallButton = (Button) findViewById(R.id.CallButton);
        SMSButton = (Button) findViewById(R.id.SMSButton);
        BirthdayButton = (Button) findViewById(R.id.BirthdayButton);
        BusinessButton = (Button) findViewById(R.id.BusinessButton);


        CallButton.setOnClickListener(this);
        SMSButton.setOnClickListener(this);
        BirthdayButton.setOnClickListener(this);
        BusinessButton.setOnClickListener(this);


    }


    public void onClick(View arg0) {
    switch(arg0.getId()){
    case R.id.CallButton:
        startActivity(new Intent(this, CallRemind.class));
        break;
    case R.id.SMSButton:
        Toast.makeText(this, "SMS Button", Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, SMSReminder.class));
        break;
    case R.id.BirthdayButton:
        Toast.makeText(this, "Birthday Button", Toast.LENGTH_LONG).show();

        b1.enable();
        break;
    case R.id.BusinessButton:
        Toast.makeText(this, "Business Button", Toast.LENGTH_LONG).show();
        break;
    }


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.layout_main, menu);
        return true;
    }



}

1 个答案:

答案 0 :(得分:1)

来自BluetoothAdapter.getDefaultAdapter()文档:

Returns
the default local adapter, or null if Bluetooth is not supported on this hardware platform

您是否在模拟器上检查代码? 或者可能在没有蓝牙的手机上?