Need to programmatically open bluetooth and discover devices in Android - android

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()

Related

Turn on Android LE scanning without asking user for permission

Some surveys and analytics showed that users very often turn down Location Permission request appearing in the mobile app even if this permission is crucial for the app, e.g. the app performs automatic scan for devices in a venue that handle business logic and exchange data with the mobile app. The permission request turn down rate is so high that it becomes problematic for business. Is there any way in Android to avoid this, e.g. to declare some verified devices that can be scanned without permission request? I thought of https://developer.android.com/guide/topics/connectivity/companion-device-pairing but it also means popping up requests for manual work for the user. Is there any way today to do it behind the scenes?
Unfortunately this is not possible for privacy and security reasons. Enabling BLE on Android devices goes hand in hand with the location permission. The only possible workaround for this is potentially if you use classic Bluetooth instead of BLE. I am not 100% sure about this one, but it is mentioned in this article:-
No one can use BLE scan API without having Location Permissions, and,
on most devices, it is useless without active Location Services.
However, Bluetooth Low Energy is an extension to so-called Bluetooth
Classic and interestingly enough Google decided this API does not need
location permissions to be called.
This is an OS requirement which cannot be overcome. You might've already seen this on the Android developer page for BLE:-
In order to use Bluetooth features in your application, you must
declare two permissions. The first of these is BLUETOOTH. You need
this permission to perform any Bluetooth communication, such as
requesting a connection, accepting a connection, and transferring
data.
The other permission that you must declare is ACCESS_FINE_LOCATION.
Your app needs this permission because a Bluetooth scan can be used to
gather information about the location of the user. This information
may come from the user's own devices, as well as Bluetooth beacons in
use at locations such as shops and transit facilities.
You can find more info about this here:-
The Ultimate Guide to Android BLE Development
Location needs to be enabled for BLE on Android
Bluetooth versus Location Permission

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

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.

Connect Bluetooth devices without user interaction in Android programmatically

How to connect Bluetooth devices without user interaction for pairing and connect via secure method too? And also want to know about android.permission.BLUETOOTH_PRIVILEDGE permission.
The only way to achieve this is use the BLUETOOTH_PRIVILEGED permission, but you can't use this permission if your app is a third party app (non-system app). To learn more, see Android API: BLUETOOTH_PRIVILEGED

Restrict Android Device Wifi Connection‏

How to force android to connect specifically defined wireless network
Is it possible?
User should able to connect defined work wi-fi or wifis,
but should not access any other networks like home.
Firstly for what you want to do that?For your application or the device?
If it's your application you can do restrictions about connection type and network etc.
But if you are talking about the device you cant achieve something like that unless you have the control of the whole system which is not possible.
One option would be to create an app or service that would pick up the:
NETWORK_STATE_CHANGED_ACTION Intent
And then in turn use the
WifiManager.getConnectionInfo()
to check whether this is a legitimate network, if not then just disconnect and notify the user.

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