Kar
Kar

Reputation: 6365

Switching bluetooth on/off in Android

If an Android app wants to access bluetooth, does it have to explicitly ask the user to switch bluetooth on? Could the user authorise the app to switch it on (and off) whenever it wants?

Upvotes: 1

Views: 538

Answers (3)

Little Endian
Little Endian

Reputation: 832

You should explicitly ask the user to enable Bluetooth by way of the following Intent:

if (!mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

An alert will then appear allowing the user to respond to the request. There is a BluetoothAdapter function enable(), but the documentation explicitly discourages using it except in specific circumstances.

Upvotes: 0

Galogen
Galogen

Reputation: 442

Android documentation for Bluetooth says that you can switch Bluetooth on/off without any requests for user. All you need is to add android.permission.BLUETOOTH at manifest. But it would be a nice to ask user before switching bluetooth on becouse of battery usage.

Upvotes: 3

FoamyGuy
FoamyGuy

Reputation: 46856

I haven't kept up with the bluetooth APIs, but last I was aware turning on bluetooth required explicit action from the user under normal circumstances. It would certainly be possible in your own copy of android to make the permissions work that way though. There might also be ways to do it if the device is rooted but I wouldn't know.

Upvotes: 0

Related Questions