0

I am quite new to Java and would appreciate if someone could explain show me how can I implement startActivityForResult(Intent, int) and onActivityResult() in Bluetooth discoverability stated here.

What I want to achieve is: on button Enable BTDisco click (if(bttn.getId() == R.id.bt_disco) my program calls BTDiscoverable() method:

public void BTDiscoverable() {
    Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
BT_ENABLE_TIME);
    startActivityForResult(discoverableIntent, REQUEST_ENABLE_BT);
}

Now, the dialog pops up. If user clicks no, the program should not continue. If user click yes, Enable BTDisco button should become unavailable and another button, lets call it Start, becomes available. I wrote onActivityResult which would make Start button available but I doubt it is done right. Snippet here:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data)
{
    if (requestCode == REQUEST_ENABLE_BT)
    {
        if(resultCode == RESULT_OK)
        {
            ((Button)            findViewById(R.id.bt_server_start)).setEnabled(true);
            BTCountDown = new BTTimer(BT_TIME_BTTIME,BT_TIME_BTTIME);
            BTCountDown.start();
        }
    }
}

startActivityForResult is called in BTDiscoverable method but it does not provide what I wanted. I have followed this but as I am quite new here, I have no idea how to implement this in such problem as mine.

The onActivityResult method is implemented class that extends Activity. There are no errors while compiling. Long story, short: R.id.bt_server_start stays disabled as initially programmed. Could really use some help.

Community
  • 1
  • 1
Lisek
  • 753
  • 2
  • 11
  • 31

1 Answers1

2

Just put this

if(resultCode != RESULT_CANCELED)

Documentation says the following

Your activity will then receive a call to the onActivityResult()) callback, with the result code equal to the duration that the device is discoverable. If the user responded "No" or if an error occurred, the result code will be RESULT_CANCELED.

So if result code is not RESULT_CANCELED, then you are okay to enable your button.

neo
  • 1,952
  • 2
  • 19
  • 39