Need to programmatically open bluetooth and discover devices in Android

后端 未结 1 1964
难免孤独
难免孤独 2021-01-01 01:22

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

相关标签:
1条回答
  • 2021-01-01 01:36

    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()
    
    0 讨论(0)
提交回复
热议问题