If an Android app wants to access bluetooth, does it have to explicitly ask the user to switch bluetooth on? Could the user authorise the app to switch it on (and off) whenever it wants?
Android documentation for Bluetooth says that you can switch Bluetooth on/off without any requests for user. All you need is to add android.permission.BLUETOOTH at manifest. But it would be a nice to ask user before switching bluetooth on becouse of battery usage.
I haven't kept up with the bluetooth APIs, but last I was aware turning on bluetooth required explicit action from the user under normal circumstances. It would certainly be possible in your own copy of android to make the permissions work that way though. There might also be ways to do it if the device is rooted but I wouldn't know.
You should explicitly ask the user to enable Bluetooth by way of the following Intent:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
An alert will then appear allowing the user to respond to the request. There is a BluetoothAdapter function enable(), but the documentation explicitly discourages using it except in specific circumstances.
Related
While working with the Blutooth low energy I need to mantain the Bluetooth adapter on.
Recently I've realized that it can be turned on in two different ways.
First way:
BluetoothAdapter.getDefaultAdapter().enable();
First time I tryed this function I thought this required root or some kind of privileges to work.
Actually I find out none of them is necessary and this function works fine on different devices.
(Samsung S4 Lollipop, Asus Zenpad 8" Marshmellow, Meizu M2 Note Lollipop)
Second way:
enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, ENABLE_BLUETOOTH_INTENT);
By using the intent you can ask the user to turn on the Bluetooth. This one works fine too but you have to listen for intent result. So this require more code and the user can decide to not turn on the bluetooth.
Do they works fine in every situation? Is there a "better" one? What is the real difference between them?
As per documentation:
Bluetooth should never be enabled without direct user consent. If you want to turn on Bluetooth in order to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that requests user permission to turn on Bluetooth. The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.
So basically the reason to use Intent is just a good UX. I advise to use Intent because of that reason.
In order to use BLE in your Application for receiving and transmitting data among device we must require blutooth enabled.
Another point is that device suport BLE or not.
Bluetooth adapter required for doing bluetooth related stuff.
If blutooth is not enabled on device , then ask for user to enable it by using intent and listen for intent. So this more code required.
If user do not turn on bluetooth we can't do BLE related things.
So, require bluetooth adapter and bluetooth on device and enable method provides user interface for changing system settings.
I can set a variavle when the app initialises to thell me if the Device Bluetooth is enabled.
There doesn't seem to be a method to enable Bluetooth from within the app, so I'm showing a notifier "Enable Bluetooth in Device Settings", but I'd really like to be able to disable Bluetooth when the app closes (if it was originally disabled - hence the variable) to conserve battery life.
Can this be done?
Thanks
This does not appear possible using AppInventor at this time.
It does appear that you have the ability to directly turn on Bluetooth, however, using the Android Bluetooth intent and the AI2 Activity Starter component. See (http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html) for reference and (http://www.whatakuai.com/bluetooth-and-app-inventor/) for implementation.
Sadly there does not appear to be a public Intent you can use to disable the bluetooth radio using the ActivityStarter component.
use the application Bluetooth Auto Off
it dissable the bluetooth after there is no comunication for selected time
https://play.google.com/store/apps/details?id=com.mst.btautodisable
I would like to beable to automatically turn on discoverability on an android device without the user being prompted with a security dialog.
I'm developing two applications, one for an Android TV box, one for my smartphone. I want to be able to control the android TV box using my smartphone, however I want to do this without needing to turn on discoverability manually on the TV box as that would require a remote of some kind completely defeating the point of the application.
Is there another way to enable discoverability through API which I could use - preferably via a service.
thanks,
Nathan.
Wow, 4 years go.
As an answer to my younger self I would think that what you were trying to do is not possible as you said so yourself, it's a matter of security (You yourself reference a "sercuity dialog").
If an app could turn on Bluetooth whenever it wished and had full covert access to the entire Bluetooth hardware, then the app could potentially with malicious intent damage parts of the Android OS and delete or worse; distribute the User's personal information.
What you would be looking at instead is implementing the system that computer peripheral manufactures implement with wireless keyboard and mice, that is to extract the Bluetooth connectivity to a separate hardware layer (A usb dongle) and via USB implement a human interface device, thus in turn filtering the Bluetooth connection to allow only the sending of HID input information to the device.
To myself 4 years ago,
Nathan.
A bit late, but you need to do this with an intent, from the documentation on Android Bluetooth:
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
http://developer.android.com/guide/topics/connectivity/bluetooth.html#EnablingDiscoverability
I'm writing a app that requires bluetooth. I periodically check if I need to synchronize some data.
W.r.t battery usage which is better ?
Having bluetooth always on and using it when needed for communication.
OR
Switching it on when needed and switching it off.
I believe you shouldn't force the BT state on the users, for whatever reasons they might prefer to keep it on or off. The only thing you application can do is to ask the user to switch BT on whenever you need it (using intent BluetoothAdapter.ACTION_REQUEST_ENABLE), and if you ask every 30mn or so it is rapidly going to be annoying.
Remember the enable() method in BluetoothAdapter should not be used without user consent (from sdk reference):
Bluetooth should never be enabled without direct user consent. If you want to turn on Bluetooth in order to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will raise a dialog that requests user permission to turn on Bluetooth. The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.
I think it really depends on the time interval between your checks. If the checks are at large intervals, it's better to switch on/off, otherwise, if they are often it probably won't make much difference to keep in on.
Add a button to "turn on bluetooth and sync", when it's over give the user feedback and then request to turn it off.
I need to open up bluetooth through my code without asking the user to enable it and then discover all the devices in range and return those devices and then close bluetooth connection.
How to do it?
I suppose here is exactly what you need.
https://developer.android.com/guide/topics/connectivity/bluetooth.html#FindingDevices
https://developer.android.com/guide/topics/connectivity/bluetooth.html#DiscoveringDevices
About enabling the Bluetooth without asking the user, here is what the doc says:
Bluetooth should never be enabled without direct user consent. If you
want to turn on Bluetooth in order to
create a wireless connection, you
should use the ACTION_REQUEST_ENABLE
Intent, which will raise a dialog that
requests user permission to turn on
Bluetooth. The enable() method is
provided only for applications that
include a user interface for changing
system settings, such as a "power
manager" app.
But if you really want to Enable device bluetooth without asking the user. after add this:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
permission to your Manifest use this code in your App
BluetoothAdapter.enable()