You need to programmatically open Bluetooth and discover devices in Android

I need to open bluetooth through my code without asking the user to turn it on, and then find all the devices in the range and return these devices, and then close the Bluetooth connection.

How to do it?

+6
source share
1 answer

I guess this 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 turning on Bluetooth without a user request, here is what doc says:

Bluetooth should not be turned on without the express consent of the user. if you want to enable Bluetooth to create a wireless connection, you should use the ACTION_REQUEST_ENABLE Intent, which will cause a dialog that asks the user for permission to turn on Bluetooth. The enable () method is provided only for applications that enable the user interface to change system settings, such as the "power manager".

But if you really want to turn on the Bluetooth Bluetooth device without asking the user. thereafter:

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

for your manifest use this code in your application

 BluetoothAdapter.enable() 
+10
source

Source: https://habr.com/ru/post/888693/


All Articles