Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to programmatically open bluetooth and discover devices in 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?

like image 757
coderslay Avatar asked May 21 '11 09:05

coderslay


1 Answers

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()
like image 144
Shaunak Avatar answered Sep 22 '22 01:09

Shaunak