Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BluetoothAdapter.ACTION_REQUEST_ENABLE calls onActivityResult immediately

According to the developer guides, a request can be made to enable Bluetooth as such:

http://developer.android.com/guide/topics/connectivity/bluetooth.html#SettingUp

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

In my implementation this creates the request properly and the user is presented with the request dialog.

My problem is that onActivityResult() is immediately called such that before the dialog even appears I have the result. The returned requestCode matches the one set (9001 in this case) and the resultCode is always 0 (Activity.RESULT_CANCELED).

I believe this to be a bug in 4.3, unfortunately I only have 4.3 devices. Reading the descriptions and the source it appears that onActivityResult() should not be called until the user interacts with the dialog so can anyone confirm this is a bug or explain what I am doing wrong?

Tested on the following with the same result:

  • New Nexus 7
  • Nexus 7
  • Nexus 4
  • Galaxy Nexus
like image 537
ian.shaun.thomas Avatar asked Aug 08 '13 01:08

ian.shaun.thomas


2 Answers

startActivityForResult doesn't work with singleInstance. Is your activity configured as single instance ? (android:launchMode="singleInstance")

like image 93
Eduard Avatar answered Sep 19 '22 13:09

Eduard


Testing on 4.1.1 it looks like the guide is just plain wrong. Calling startActivityForResult does not work as expected for this intent and onActivityResult is called before the user is even given a chance to interact with the dialog.

As such, the reliable way to know when the user has enabled bluetooth is to listen for the associated ACTION_STATE_CHANGED event.

like image 35
ian.shaun.thomas Avatar answered Sep 20 '22 13:09

ian.shaun.thomas