I am trying to get my Activity to enable Bluetooth with the Android 2.0.1 SDK, I am using some code straight from the documentation here: http://developer.android.com/guide/topics/wireless/bluetooth.html
Which is:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
I get an error at the REQUEST_ENABLE_BT part where Eclipse says it cannot be resolved. What am I doing wrong?
The REQUEST_ENABLE_BT part is a request code that you should handle in your onActivityResult method. In that method you'll be notified whether or not enabling Bluetooth was successful.
In that code snippet they didn't show the definition of it, but it's just a constant integer so you can set it to any value you like.
See the documentation for startActivityForResult for more info about getting results back from activity launches.
Related
With the following code you can pop up a window with bluetooth acces request:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
The output of the code is the following image:
Is it possible to change the message (so not "An app wants to turn on Bluetooth", but something like "Hey can you please turn on Bluetooth?"
You should create your own dialog with your own message and then call
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.enable();
And do not forget to declare the BLUETOOTH_ADMIN permission in your manifest.
Anyway, this operation is discouraged... The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.
No, this is build-in Android functionality, so you would have to make a custom dialog which asks the question the way you want.
Then you would enable or disable Bluetooth programmatically based on what the users choice was.
Hello we are using bluetooth in Android.
When we turn on bluetooth it give us default dialog "An app wants to turn on Bluetooth" with Deny and Allow buttons. With this message a blue line is shown behind that dialog which is nothing but a text "Bluetooth Permission". Can we hide or remove this message behind the dialog. I have attached screenshots of the page.
Here is the code Snippet
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
I am trying to use BluetoothAdapter with ACTION_REQUEST_DISCOVERABLE to discover my phone to other users according to this:
testIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(testIntent);
I get the dialog box of Bluetooth Permission Request, but for some reason I can't see it on my activity. I can see it only when I exit this program. I want to override the menu in that dialog.
Can I do it , if so , how can I change it ? And why can't I see it on the activity I am in it ?
I have a requirement where I want to develop a hybrid application and list the Bluetooth devices, scan and connect, share files etc.
So I want to add BluetoothAdapter mAdapter value into extras and pass it into onActivity result. Like enableIntent.putExtra("", mAdapter); and use it in onActivityResult by getExtra(...). Is there a way to do so??..
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
((Activity)context).startActivityForResult(enableIntent, RequestCodes.BLUETOOTH_ENABLE_CODE);
}
Why don't you use Application class to store that data and use wherever you need.
Check this post for how to use Application class.
I have encountered strange issue. From Activity onStart() I request Bluetooth activation and 120s discoverability via intent:
Intent activateBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(activateBTIntent, BT_ACTIVATE_INTENT);
I use it no matter if the Bluetooth is already activated or not. Android documentation says that Bluetooth will be activated if it was not, and that works fine. In both cases I get system Alert dialog
When I rotate the screen I observe flickering. Press on Yes/No removes one dialog, but there is still another one below. Performing screen rotation I can get a pile of Alert dialogs, and have to press Yes/No on each to get rid of them.
Described issue is present only if Bluetooth was not already started when intent was sent, otherwise it works correctly. Tried on different 2.2 phones, and issue is present on all. Looks to me like Android system issue.
Has anybody encountered it also, and maybe have some useful hint how to avoid this?
Thanks and regards.
This is a bug in the Settings app that causes this. The RequestPermissionActivity is duplicating its instance of RequestPermissionHelperActivity on rotations.
I was facing the same problem. After spending lots of time with it, i have found one solution.
You can avoid the duplication of the 'Bluetooth activation Alert dialog' by using
FLAG_ACTIVITY_NO_HISTORY flag for activateBTIntent. But setting this flag will make startActivityForResult unusable.
The code -
Intent activateBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
activateBTIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(activateBTIntent);
Using FLAG_ACTIVITY_NO_HISTORY with activateBTIntent will remove the 'Bluetooth activation Alert dialog' activity from the history stack. Thus in this case, when orientation is changed, the 'Bluetooth activation Alert dialog' activity will be finished and newel created(NOT DUPLICATED):
Reference
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY
(Your post doesn't nearly have enough info, so I'm assuming here...)
I'm not too sure about the startActivityForResult.
You shouldn't do showDialog() in onCreate() if you use a managed AlertDialog. If the Bluetooth thingy does that, could you try not running the above code from onCreate()?
The issue has been reported (google issue tracker) but it is already open after 6 years (checked with Android 7.1.2).
Unfortunately the flag FLAG_ACTIVITY_NO_HISTORY does not work anymore. The only quick workaround I found until Android team fix this (if they ever will) is to lock activity screen orientation change, just for the time the dialog is displayed:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
to be called just before
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
and then use
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
super.onActivityResult(requestCode, resultCode, data);
}
on intent result.
It's a temporary remedy but usually better than showing this possible issue during BT activation.