简体   繁体   中英

Changing target SDK from 28 to 29 results in BLE scan not working

I am working on Android app which need to be connected to BLE devices. The app works as expected but now when I changes the target SDK to 29 (which was previously API 28) the app stops getting any scan results. At API level 28 when the app starts I check the permission. If not granted I ask for it and then start scanning as usual for BLE devices. Then I get the scan results which include the BLE devices around me. I confirm that by printing log messages. Now when I changed the target SDK to 29 (which is required by play store to update the app) the app stops getting scan results. I am not getting messages in the logs either.

Device which is working with API 28 and not with API 29: Samsung S10 with Android 10.

Permissions I added in the manifest

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

<!-- If your app targets Android 9 or lower, you can declare
     ACCESS_COARSE_LOCATION instead. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

Permission I request from user at runtime:

if(ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED){
        //here code for asking permission from user.
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                REQUEST_ENABLE_LOCATION);
    }

The code where I interact with scan results:

public void onScanResult(int callbackType, ScanResult result) {
    BluetoothDevice device = result.getDevice();
    String deviceName = device.getName();
         if (deviceName != null) {
                    if (checkdevicename(deviceName)){

                        Log.d(TAG, "Connect:" + deviceName);
                        isConnected = true;
                        mDevice = device;

//                             // connecting to device. establishing gatt connection.
                        device.connectGatt(ma,false,mGattCallback);
                        stopScan();

                    } else {
                        Log.d(TAG,"DEVICE NAME =====>"+deviceName);

                    }
//         super.onScanResult(callbackType, result);
}}

It seems the privacy settings got more strict in SDK 29:https://developer.android.com/about/versions/10/privacy/changes for using BLE (Bluetooth Low Energy). So with that knowledge you might need to update your permissions, see Scanning of Bluetooth Low Energy Fails as well.

In a nutshell (according to the privacy version changes in 29) you need to use ACCESS_FINE_LOCATION permission to use certain Bluetooth functions (and also WiFi and some more).

Some telephony, Bluetooth, Wi-Fi APIs require FINE location permission If your app targets Android 10 or higher, it must have the ACCESS_FINE_LOCATION permission in order to use several methods within the Wi-Fi, Wi-Fi Aware, or Bluetooth APIs.

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