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:
startActivityForResult doesn't work with singleInstance. Is your activity configured as single instance ? (android:launchMode="singleInstance")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With