Android 10 not working with BLE Bluetooth scanning

吃可爱长大的小学妹 提交于 2020-04-17 03:10:55

问题


I am working on BLE Bluetooth scanning is working on all devices except the Android 10. Android 10 is not working, anyone please answer the question for the version 10 issues for scanning BLE Bluetooth


回答1:


To make BLE scanning work on Android apps targeting Android 10 you need to ask the user for

ACCESS_BACKGROUND_LOCATION

along with ACCESS_FINE_LOCATION and also don't forget to add the permission in the manifest:

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

Here's the explanation why:

Android 10 (API level 29) introduces a number of features and behavior changes to better protect users' privacy. These changes extend the transparency and control that users have over their data and the capabilities they give to apps.

Your problem in short:

Access to device location in the background requires permission

To support the additional control that users have over an app's access to location information, Android 10 introduces the ACCESS_BACKGROUND_LOCATION permission.

Unlike the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions, the ACCESS_BACKGROUND_LOCATION permission only affects an app's access to location when it runs in the background. An app is considered to be accessing location in the background unless one of the following conditions is satisfied:

An activity belonging to the app is visible. The app is running a foreground service that has declared a foreground service type of location.

To declare the foreground service type for a service in your app, set your app's targetSdkVersion or compileSdkVersion to 29 or higher. Learn more about how foreground services can continue user-initiated actions that require access to location.

Hope this fixes your problem :)




回答2:


You need to have location enabled to perform a BLE scan on android 10. To check if location is enabled and ask user to enable location service (Kotlin):

val lm = getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (!LocationManagerCompat.isLocationEnabled(lm)) {
    // Start Location Settings Activity, you should explain to the user why he need to enable location before.
    startActivity(Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS))
}

LocationManagerCompat class is available in androidx.appcompat:appcompat:1.1.0 dependency, if you don't use androidx, you can use instead :

lm.isLocationEnabled()


来源:https://stackoverflow.com/questions/58428763/android-10-not-working-with-ble-bluetooth-scanning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!