Home > Blockchain >  Android 12 turn bluetooth on
Android 12 turn bluetooth on

Time:08-05

So.. i got all new permissions added in manifest file,

<uses-permission
    android:name="android.permission.BLUETOOTH_ADMIN"
    android:maxSdkVersion="30" />

<uses-permission android:name="android.permission.BLUETOOTH"
    android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
    android:maxSdkVersion="30" />
<!-- Needed only if your app looks for Bluetooth devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>

//Added an array with (maybe more than what i need) permissions
private static String[] PERMISSIONS_BLUETOOTH = {
        Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
        Manifest.permission.BLUETOOTH_SCAN,
        Manifest.permission.BLUETOOTH_CONNECT,
        Manifest.permission.BLUETOOTH_ADVERTISE,
        Manifest.permission.BLUETOOTH,
        Manifest.permission.BLUETOOTH_PRIVILEGED
};

//checking permission with user:
int permission1 = ActivityCompat.checkSelfPermission(this, 
     Manifest.permission.BLUETOOTH);

if (permission1 != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this,PERMISSIONS_BLUETOOTH,1);
}

// got the results in 
@Override
public void onRequestPermissionsResult(.....)
 

I got this:

  • android.permission.ACCESS_COARSE_LOCATION-0
  • android.permission.ACCESS_LOCATION_EXTRA_COMMANDS-0
  • android.permission.BLUETOOTH_SCAN-0
  • android.permission.BLUETOOTH_CONNECT-0
  • android.permission.BLUETOOTH_ADVERTISE--1
  • android.permission.BLUETOOTH--1
  • android.permission.BLUETOOTH_PRIVILEGED--1

So i try to enable bluetooth.. as i have permissions now. with..

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (!mBluetoothAdapter.isEnabled()) {
        mBluetoothAdapter.enable();
    }

But.. no doesnt do anything.. not error, not a log.. and of course.. it doesnt turn bluetooth on.... Am i missing something??

CodePudding user response:

follow this: https://developer.android.com/guide/topics/connectivity/bluetooth/permissions

For your legacy Bluetooth-related permission declarations, set android:maxSdkVersion to 30. This app compatibility step helps the system grant your app only the Bluetooth permissions that it needs when installed on devices that run Android 12 or higher.

can you share your gradle module ?

CodePudding user response:

The use of the BluetoothAdapter.enable() function is highly regulated (for safety reasons) and most likely your application lacks one of the necessary requirements.

From an application point of view, it is better to ask the user to switch on Bluetooth via the system settings if this is required by your application and should not already be switched on.

  • Related