BLE Issues In Android 7 And Above

I had worked with many BLE scanning applications in the previous Android versions. There were no limitations for the scanning time out or scanning in the background.

When I upgraded the old APK with the latest Android versions, I faced issues like my application not scanning for a long time in the background, even in the foreground, and BLE scanning stopped after some time.

This article explains BLE issues in android 7 and above and how to solve them.

1. BLE Scanning Issue

During my implementation, I faced one issue, like BLE scanning working properly on all devices except Samsung phones with android version 8 or above. In other cases, I could scan in Samsung with older android versions like android 6 or below.

As of Android 8, unfiltered Bluetooth scans are blocked when the screen is turned off. The new 8.1 operating system code simply verifies any scans active when the screen has at least one scan filter. If those conditions are met, the scan results are delivered as in Android 8.0.x and earlier.

To resolve this issue, you can set a filter with any unique identifier like CompanyId or ManufactureId of your BLE device. And it can also scan only those devices which uniquely identify you have set with filters so that it’ll ignore other random BLE electronic devices.

private BluetoothLeScanner mBluetoothScanner;

private List<ScanFilter> filters;

filters = new ArrayList<>();

settings = new ScanSettings.Builder().setScanMode(

       ScanSettings.SCAN_MODE_LOW_LATENCY).build();

mBluetoothScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();

ScanFilter.Builder builder = new ScanFilter.Builder();

builder.setManufacturerData(“your company id (0x020f

)”, new byte[] {});

ScanFilter filter = builder.build();

filters.add(filter);

mBluetoothScanner.startScan(filters, settings, getInstance()._scanCallback);

Jayu Rewards Is A BLE Based Next Generation Digital Rewards System

2. BLE Scanning Will Stop While App Is Running In The Background From Android 8

Even in the background, I wanted to continue my functionality, and I needed to show local notifications when some conditions were met. But from Android 8, I could not get BLE packets while the app was in the background. Then I read about Doze mode and foreground service, which helped me to understand why this was happening and how I could solve this problem.

If your app runs in the background, then the user cannot interact with activity, and the app Standby allows the system to determine that an app is idle. The system makes this determination when the user does not touch the app for a certain period, and the app goes to doze mode.

In this type of scenario, the App has some process running in the foreground (either as an activity or foreground service or in use by another activity or foreground service).

If your app runs in the background, then activity interaction is impossible. So There is only one option, that is foreground service. You should start the foreground service while the app goes in the background and again stop while the app comes to the foreground.

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {

   public void onCreate() {

       super.onCreate();

       registerActivityLifecycleCallbacks(this);

   }

   @Override

   public void onActivityResumed(@NonNull Activity activity) {

       if (activity instanceof YourActivity) {

           Intent intent = new Intent(this, ForegroundService.class);

           stopService(intent);

       }

   }

   @Override

   public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

       if (activity instanceof YourActivity) {

           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

               Intent intent = new Intent(this, ForegroundService.class);

               ContextCompat.startForegroundService(this, intent);

           }

       }

   }

   @Override

   public void onActivityDestroyed(@NonNull Activity activity) {

       if (activity instanceof YourActivity) {

           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

               Intent intent = new Intent(this, ForegroundService.class);

               stopService(intent);

           }

       }

   }

}

public class ForegroundService extends Service {

   @Override

   public void onCreate() {

       super.onCreate();

       createNotification();

   }

   private void createNotification() {

       VulcanUtil.getInstance().setChannelLowImportance(getApplicationContext());

       Notification notification = new NotificationCompat.Builder(this, Constants.CHANNEL_ID_FOREGROUND)

               .setContentTitle(getApplicationContext().getResources().getString(R.string.app_name))

               .setSmallIcon(R.mipmap.ic_notification)

               .setOngoing(true)

               .setCategory(NotificationCompat.CATEGORY_SERVICE)

               .setPriority(Notification.PRIORITY_MIN)

               .build();

       startForeground(101, notification);

   }

   @Override

   public void onDestroy() {

       stopForeground(true);

       stopSelf();

       super.onDestroy();

   }




   @Nullable

   @Override

   public IBinder onBind(Intent intent) {

       return null;

   }

}

Hire BLE App Developers!

A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. The notification cannot be dismissed unless the service is stopped or removed from the foreground.

You can check foreground service here:

3. BLE Scanning Will Stop After 30  Mins Even If App Is In Foreground

It might be possible that the user is using the application for so long, even more than 30 minutes. But if the user is active onAndroid 7 or above, then BLE scanning will stop automatically, and the user can not see the update after 30 minutes.

Android 7+ will block scanning that lasts 30 minutes or more without stopping. So if you have a betweenScanPeriod of 0 and can for 30 minutes, the operating system will cut off your scan on Android 7 or above.

In Beacon Android, version 2.0-2.9 has the above problem with 30 minutes scanning period, but in version 2.10, they have resolved this issue and automatically restarted the scanning. So if you are using the Beacon library, you just need to update the library version.

If you are not using the Beacon Android library, you must manage it yourself. You can set a handler for 30 minutes to stop scanning and again start scanning after every 30 minutes.

coma

Conclusion

That’s a wrap for BLE issues in android. So, if you are updating your app with the latest versions, then check all the cases like you are testing your app with the latest and older versions. You are checking your app in the foreground and background with all the possible cases, like the screen on/off.

 

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?