Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 10 not working with BLE Bluetooth scanning

Tags:

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

like image 844
raj.a Avatar asked Oct 17 '19 09:10

raj.a


People also ask

How do I scan my Android phone with BLE?

To find BLE devices, you use the startScan() method. This method takes a ScanCallback as a parameter. You must implement this callback, because that is how scan results are returned.

What is BLE settings Android?

Android provides built-in platform support for Bluetooth Low Energy (BLE) in the central role and provides APIs that apps can use to discover devices, query for services, and transmit information. Common use cases include the following: Transferring small amounts of data between nearby devices.


2 Answers

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 :)

like image 85
Ruzin Avatar answered Oct 29 '22 02:10

Ruzin


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() 
like image 24
Bubu Avatar answered Oct 29 '22 01:10

Bubu