3

I want that my bluetooth to be visible until the app is running. Like for infinite time. I will turn it off when Im done with it.

I don't want it to ask user for duration of visibility as in following method

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

Please help!

Ron
  • 24,175
  • 8
  • 56
  • 97
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

3 Answers3

4

The short answer is that you can't; see this bug report. Trust me, I'm as annoyed about this as you are; it makes ad-hoc networking on Bluetooth very difficult. There are some suggestions on that thread that the limitation is gone in Honeycomb and perhaps will make it into Ice Cream Sandwich. For the moment, there is no way around it, short of installing a custom ROM.

With that said, there are some workarounds. Since Android requires pairing before any Bluetooth socket (server or client) can send data, your app can simply try and connect to already paired devices. Sure, most of the time the connection will fail, but if a paired device is in range the connection will happen. I've implemented this in an application of mine and it seems to work. I'm not sure how this affects battery life, but with enough time between scans you ought to be okay.

I'm hoping that future versions of Android won't have this unnecessary limitation as it prevents some potentially interesting ad-hoc networking applications.

zeitkunst
  • 505
  • 5
  • 11
1

I am confused, because on Android Dev Guide, it is said the following:

[..] By default, the device will become discoverable for 120 seconds. You can define a different duration by adding the EXTRA_DISCOVERABLE_DURATION Intent extra. 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). For example, this snippet sets the duration to 300.[..]

Isnt it the feature we are looking for?

Michael Trend
  • 71
  • 1
  • 4
  • @Michael Trend's post *does* come from the [main Android Bluetooth docs](http://developer.android.com/guide/topics/wireless/bluetooth.html), but if you click through you get to [this definition](http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#ACTION_REQUEST_DISCOVERABLE) which clearly states the limit as 300s. I'm guessing that this is an inconsistency due to the removal of this limit in Honeycomb. And tests by lots of people in this [bug report](http://code.google.com/p/android/issues/detail?id=6348) have found the limit. – zeitkunst Jul 31 '11 at 00:47
-1

There's a reason that you can't set the bluetooth to be always on - the battery would drain really fast. I suggest you think of another way around your issue.

Martyn
  • 16,432
  • 24
  • 71
  • 104
  • what type of else solution, this is requirement and I have to do this, then it doesn't means that discoverable be for 300 seconds, then a break then again 300 seconds, but i don't want any alert or any yes/no permission to the user – Chatar Veer Suthar Jul 28 '11 at 10:07
  • You're trying to circumvent the way that bluetooth security works, and without writing the code that governs it, you're out of luck. You need to ask yourself why you need the bluetooth always on, and if there's another method to achieve what you want. – Martyn Jul 28 '11 at 10:14
  • What is that method, is this about you are talking setDiscoverableTimeout(int timeout) – Chatar Veer Suthar Jul 28 '11 at 10:17
  • it is requirement of app, I have developed an app for appStore, named as BLU Connect, and i want same for android, but facing problem in blutooth, – Chatar Veer Suthar Jul 28 '11 at 10:19