6

I want to check if bluetooth is enabled in a device using an Android application. I used the .isEnabled method. But there is an error. I found out (by commenting lines) that the error is in .isEnabled method. Can you pls help me to figure this out?

final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String status = "Bluetooth"; if(bluetooth != null) { if (bluetooth.isEnabled()) { String mydeviceaddress = bluetooth.getAddress(); String mydevicename = bluetooth.getName(); status = ("Address "+ mydeviceaddress + " Name" + mydevicename); Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show(); } else { status = ("Bluetooth not enabled"); Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show(); } } else { Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show(); } } } 

4 Answers 4

22

This has worked best for me:

/** * Check for Bluetooth. * * @return true if Bluetooth is available. */ public boolean isBluetoothAvailable() { final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); return bluetoothAdapter != null && bluetoothAdapter.isEnabled() && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Just one thing, why is that method taking a Context object, when is no really using it at all?
@hmartinezd Good eye. It is now fixed.
6

Try this.

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { // Device does not support Bluetooth } else { if (!bluetoothAdapter.isEnabled()) { // Bluetooth is not enabled } } 

in your AndroidManifest.xml File add

<uses-permission android:name="android.permission.BLUETOOTH" /> 

4 Comments

@Saku what are the errors ? update your post and put error details.
A 'Force Close' appears. Since the emulator does not support Bluetooth i tried in an actual tab. So i can't give precise exceptions or error messages
@Saku Write Log File and track error massages. then you can sea errors occurred.
If bluetooth permission is only needed for this detection, don't forget to set it NOT required: <uses-feature android:name="android.hardware.bluetooth" android:required="false" />
3

Jared Burrows answer seems like the correct one, however I had to add one addition before it started working. I had to check the Bluetooth state.

public static boolean isBluetoothAvailable() { final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); return (bluetoothAdapter != null && bluetoothAdapter.isEnabled() && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON); } 

Comments

0

why not just:

... return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.