-2

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?

Chintan Mehta
  • 129
  • 1
  • 9

1 Answers1

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