I'm new to android studio and y have a problem that I can't solve. I'm trying to enable Bluetooth, so I have done this :
AndroidManifest.xml :
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
MainActivity.java :
private void enableBLT(){
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
dialogNoBlt();
}
if (!adapter.isEnabled()) {
if (ContextCompat.checkSelfPermission(
MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) ==
PackageManager.PERMISSION_GRANTED) {
Intent enableBltIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBltIntent, REQUEST_ENABLE_BT);
} else {
Log.d("blt-", "no permission");
}
} else {
Log.d("blt-", "Not enabled");
}
}
But I still have an error "requires android.permission.BLUETOOTH_CONNECT", and my app just crash. Can you help me please ?
CodePudding user response:
You have to request Manifest.permission.BLUETOOTH_CONNECT
and Manifest.permission.BLUETOOTH_SCAN
permissions at runtime as well. Just declaring in manifest is not sufficient because they are under dangerous permissions category.
docs for requesting permission
Refer the following link of answer for a direct code sample