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!
解决方案
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.