Android BLE difference between BlutoothAdapter.enable(); and call an intent - android

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.

Related

How to detect if user paired with a BLE device using the Android Bluetooth settings?

My app allows users to pair with specific BLE devices fine, but if my app is put in the background and the user pairs (or unpairs) with the BLE device using the Android bluetooth settings, my app is not aware of this. I've tried using ACTION_PAIRING_REQUEST, but my receiver never receives it. Would someone illustrate how to do this, or point me at example code that does?
Try using ACTION_BOND_STATE_CHANGED instead.
I don't know if that's the case but be aware of Implicit broadcasts restriction. Some broadcasts cannot be registered in AndroidManifest.xml anymore.

App Inventor 2 - Disable the Device Bluetooth when app closes?

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

(Android && Bluetooth) Make device discoverable from service without user interaction?

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

Switching bluetooth on/off in Android

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.

Android : unable to enable bluetooth adapter

I am running an embedded system on android 2.1. The system has a bluetooth chip.
Calling getDefaultAdapter returns an adapter so BT is available
however i'm not able to enable the adapter even when user gives permission.
I also tried using bluetoothadapter.enable() and its still not working.
I've tested the code on a phone and it works,
What could be going wrong?
Do not use without explicit user action to turn on Bluetooth.
Bluetooth should never be enabled without direct user consent.
See the documentation.

Categories

Resources