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.

 

Krupa

Tech Expert

A forward-thinking developer with skills like designing, building integrating, testing, and supporting android mobile applications. Also known for writing efficient, maintainable and reusable code.

Get in touch for a detailed discussion.

Hear From Our 100+ Customers
coma

Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.

author
ADDIE WOOTTEN
CEO, SMILINGMIND
coma

We had very close go live timeline and MindBowser team got us live a month before.

author
Shaz Khan
CEO, BuyNow WorldWide
coma

They were a very responsive team! Extremely easy to communicate and work with!

author
Kristen M.
Founder & CEO, TotTech
coma

We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.

author
Chacko Thomas
Co-Founder, TEAM8s
coma

Mindbowser is one of the reasons that our app is successful. These guys have been a great team.

author
Dave Dubier
Founder & CEO, MangoMirror
coma

Mindbowser was very helpful with explaining the development process and started quickly on the project.

author
Hieu Le
Executive Director of Product Development, Innovation Lab
coma

The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.

author
Alex Gobel
Co-Founder, Vesica
coma

Mindbowser is professional, efficient and thorough. 

author
MacKenzie R
Consultant at XPRIZE
coma

Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.

author
Laurie Mastrogiani
Founder, S.T.A.R.S of Wellness
coma

MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app.” “They had put together fantastic wireframes for us.

author
Bennet Gillogly
Co-Founder, Flat Earth
coma

They're very tech-savvy, yet humble.

author
Uma Nidmarty
CEO, GS Advisorate, Inc.
coma

Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.

author
Katie Taylor
Founder, Child Life On Call
coma

As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.

author
Radhika Kotwal
Founder of Courtyardly
coma

The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!

author
Michael Wright
Chief Executive Officer, SDOH2Health LLC
coma

They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.

author
David Cain
CEO, thirty2give
coma

We are a small non-profit on a budget and they were able to deliver their work at our prescribed budgets. Their team always met their objectives and I'm very happy with the end result. Thank you, Mindbowser team!!

author
Bart Mendel
Founder, Mindworks
coma

Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.

author
George Hodulik
CEO, Stealth Startup, Ex-Google