Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices

1 answer

I am building an Android app that advertises small packets of information as well as scan for information using BLE. The code for advertising is:

   AdvertiseSettings advSettings = new AdvertiseSettings.Builder()                 .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)                 .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)                 .setConnectable(false)                 .build();           AdvertiseData advData = new AdvertiseData.Builder()                 .setIncludeTxPowerLevel(true)                 .addServiceUuid(uuid)                 .build();          AdvertiseData advScanResponse = new AdvertiseData.Builder()                 .setIncludeDeviceName(false)                 .addServiceData(uuid,"1234567890".getBytes( Charset.forName( "UTF-8" ) ))                 .build();          AdvertiseCallback advCallback = new AdvertiseCallback() {             String TAG="status";              @Override             public void onStartFailure(int errorCode) {                 super.onStartFailure(errorCode);                 Log.e(TAG, "Not broadcasting: " + errorCode);                 int statusText;                 switch (errorCode) {                     case ADVERTISE_FAILED_ALREADY_STARTED:                         Log.w(TAG, "ADVERTISE_FAILED_ALREADY_STARTED");                         break;                     case ADVERTISE_FAILED_DATA_TOO_LARGE:                         Log.w(TAG, "ADVERTISE_FAILED_DATA_TOO_LARGE");                         break;                     case ADVERTISE_FAILED_FEATURE_UNSUPPORTED:                         Log.w(TAG, "ADVERTISE_FAILED_FEATURE_UNSUPPORTED");                         break;                     case ADVERTISE_FAILED_INTERNAL_ERROR:                         Log.w(TAG, "ADVERTISE_FAILED_INTERNAL_ERROR");                         break;                     case ADVERTISE_FAILED_TOO_MANY_ADVERTISERS:                         Log.w(TAG, "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS");                         break;                     default:               , "Unhandled error: " + errorCode);                 }                 Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();             }              @Override             public void onStartSuccess(AdvertiseSettings settingsInEffect) {                 super.onStartSuccess(settingsInEffect);                 Log.v(TAG, "Advertising started");                 mText.setText(uuid.toString());                 Toast.makeText(MainActivity.this, "Started", Toast.LENGTH_SHORT).show();             }         };          mBluetoothAdapter.getBluetoothLeAdvertiser()                 .startAdvertising(advSettings, advData, advScanResponse, advCallback); 

and the code for listening is as follows:

mBluetoothLeScanner=mBluetoothAdapter.getBluetoothLeScanner();       ScanCallback mScanCallback = new ScanCallback() {         @Override         public void onScanResult(int callbackType, ScanResult result) {             super.onScanResult(callbackType, result);             if( result == null                     || result.getDevice() == null                      )                 return;              if(result.getScanRecord().getServiceUuids()!=null) {                  ParcelUuid pp=result.getScanRecord().getServiceUuids().get(0);                  StringBuilder builder = new StringBuilder((pp.toString()+"\n"));                  builder.append(new String (result.getScanRecord().getServiceUuids().size()+""));                  mText.setText(builder.toString());             } 

I have tested it on multiple phones and it works fine. However, Android version 9.0 on Galaxy S8+ can't scan unless the location is turned on. I also tried in on Android version 9.0 on Redmi Note 7 and it works perfectly without the need of turning on the location. What is causing my problem?

All answers to this question, which has the identifier 61239306

The best answer:

Not sure about the Android settings on the Redmi Note 7, but for Android 9 devices ACCESS_COARSE_LOCATION needs to be turned on in order for Bluetooth to function. Maybe this is somehow turned on on the Redmi Note 7 regardless of the location settings, whereas on the S8+ this is tied to the location settings.

You can find more information here:-

I hope this helps.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data