Hello everyone! π
In this article, I will explain how to enable Bluetooth on an Android device using Java. This method is compatible with both newer and older Android devices.
Bluetooth Permission π±π
Target Android 12 or Higher
Declare the Bluetooth Connect permission in the manifest file and request at runtime.
<manifest>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
</manifest>
requestPermissions(new String[]{android.Manifest.permission.BLUETOOTH_CONNECT},100);
Target Android 11 or Lower
Declare the Bluetooth permission in the manifest file.
<uses-permission android:name="android.permission.BLUETOOTH"/>
Request To Enable Bluetooth πΆ
ActivityResultLauncher
First, declare and initialize the ActivityResultLauncher to handle the result of enabling Bluetooth.
Note: Register the ActivityResultLauncher inside the onCreate() lifecycle method.
// Declare the ActivityResultLauncher
ActivityResultLauncher<Intent>β¦