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 th开发者_开发知识库en 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()
精彩评论