I want my button in android studio to check whether the bluetooth is on or off from the same button and then turn it on or off accordingly from the source code, can anybody help me out here?
Asked
Active
Viewed 5,046 times
-2
-
3What did you try so far? Can you include some code samples? – M B Jan 16 '18 at 17:22
1 Answers
3
Firstly you need to add these permissions
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
and to Enable/Disable Bluetooth programmatically use BluetoothAdapter for example:
btnBluetooth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter.isEnabled()){
adapter.disable();
} else {
adapter.enable();
}
} });
For more information check Bluetooth Docs

TREAF ALSHEMERI
- 409
- 5
- 12
-
How can i add it to the button that when clicked, will turn it on and off? – Chintan Mehta Jan 16 '18 at 18:25
-
-
May I know plz, why location permission needed for this? I saw some other libraries also where they r using location permission for Bluetooth scanning. But not sure yet, why needed? – Ameer Apr 07 '20 at 04:27