1

I have made an app that uses bluetooth

In the oncreate() method it enables bluetooth and sets the device visible for indefinite time

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

    if(!adapter.isEnabled()) {

        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, REQUEST_ENABLE_BT);
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
        startActivity(discoverableIntent);
    }
}

In onDestroy() it disables the bluetooth

protected void onDestroy() {
    // TODO Auto-generated method stub
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if(adapter.isEnabled()) {
        adapter.disable();
    }
    super.onDestroy();
}

But when i enable bluetooth again manually after exiting the app, It is automatically set to discoverable for indefinite time.

How do I set the bluetooth to Undiscoverable before disabling it in the onDestroy() function

Tested on Nexus 5 only

honeysingh
  • 69
  • 1
  • 2
  • 8
  • Can you share the code for onCreate() and onDestroy() – android_Muncher Jun 10 '14 at 03:10
  • @android_Muncher added the code for `onCreate()` and `onDestroy()` – honeysingh Jun 10 '14 at 03:17
  • `discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);` The maximum duration an app can set is 3600 seconds, and a value of 0 means the device is always discoverable. Any value below 0 or above 3600 is automatically set to 120 secs. You want it to be always discoverable ? – android_Muncher Jun 10 '14 at 03:33
  • @android_Muncher I want it to be always discoverable while the app is running – honeysingh Jun 10 '14 at 03:39

2 Answers2

0

This will enable discoverability for 1 second and will save you from being indefinitely discoverable

Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,1);
startActivity(discoverableIntent);
android_Muncher
  • 1,057
  • 7
  • 14
  • thats not what i want. I want it to remain discoverable as long as the app is running – honeysingh Jun 10 '14 at 03:44
  • In that case add the above code to your onDestroy() this would do the trick. – android_Muncher Jun 10 '14 at 04:01
  • The problem is that once you make is discoverable it will stay that way which is happening in your case and there is no trick to make it undiscoverable unless you do this nifty hack around. – android_Muncher Jun 10 '14 at 04:10
  • doesn't work same problem. I think it is because the app exits before that 1 second is completed. How do I make it wait for 1 second and then disable bluetooth. `sleep(1000)` doesnt work – honeysingh Jun 10 '14 at 05:23
  • switch 1 with 300 or any desirable number of seconds. that should do the trick – android_Muncher Jun 10 '14 at 05:39
0
  • There does not seem to be an intent that you can generate to make a device undiscoverable.
  • So, you need to work around this. Starting a new intent for discoverability for one second would do the trick.
  • It would bring down the discoverability time from infinity to one second which is as close as we can get to making it undiscoverable.
  • I understand that it's a hack, but there is no other way provided in the Android documentation.

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

Link to Android documentation for discoverability in bluetooth devices : http://developer.android.com/guide/topics/connectivity/bluetooth.html#EnablingDiscoverability

This is a possible duplicate of the question : Disable Bluetooth discoverable mode on Android

Community
  • 1
  • 1
nnori
  • 127
  • 2
  • 14
  • doesn't work same problem. I think it is because the app exits before that 1 second is completed. How do I make it wait for 1 second and then disable bluetooth. `sleep(1000)` doesnt work – honeysingh Jun 10 '14 at 05:24
  • Try writing that in onPause(). But use necessary flags so that discoverability is switched off only when required and not on all onPause() callbacks. Does this make sense in the context of your application? – nnori Jun 10 '14 at 08:51
  • can u please explain how do i use the necessary flags so that the discoverability is switched off only when the app is stopped by the user – honeysingh Jun 14 '14 at 08:08