1

I have written an app to turn on bluetooth using voice.(Just called the intent and used the Action.request.enable method). Its working fine..

Now I want to turn off bluetooth. I tried using bluetoothadapter.disable() but the whole application crashes. Please help me.. This is my code

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter.isEnabled())
               {
                   mBluetoothAdapter.disable();
               }
Raja
  • 11
  • 3

2 Answers2

0

Paste some logcat information will be helpful.

I found that Android doesn't provide startActivityForResult to popup a window to remind user to turn off Bluetooth.So you must call the BluetoothAdapter.disable() method to turn off Bluetooth.

1.When you use startActivityForResult with intent to remind user to turn on Bluetooth,it's working.It means that you are already with the declaration of the permission

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

2.When you use the BluetoothAdapter.disable() method to turn off Bluetooth, the app crashed.I think that maybe you miss the the declaration of the permission

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

for BluetoothAdapter.disable().

NOTE:

BluetoothAdapter.getDefaultAdapter() needs:
<uses-permission android:name="android.permission.BLUETOOTH" />

BluetoothAdapter.disable() needs:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
ifeegoo
  • 7,054
  • 3
  • 33
  • 38
0

try this for disable

Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 1);
startActivity(discoverableIntent);

for more help :: this

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133