简体   繁体   中英

Android 12 turn bluetooth on

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??

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?

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.

Use the user-facing method which is future-proof and works on all API levels.

if (bluetoothAdapter?.isEnabled == false) {
  val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
  startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}

See Set up Bluetooth for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM