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.
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);
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; } }
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:
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.
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.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowWe had very close go live timeline and MindBowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Our CISO was extremely impressed by Mindbowser’s work. It is pretty rare to see this kind of clean security report so early in the company’s journey. Huge Thank you for the disciplined approach here.
Founder, TrestleIQ
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
The flexibility and capacity of the Mindbower staff has been impressive.
CEO, ProofPilot
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
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.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
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.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
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.
Founder, Courtyardly
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork
We cannot express enough how pleased we are with the entire Mindbowser team. From our first call, they focused on our vision and ensured things were handled as per requirement. Communication was so easy and flexible to our schedule. I’m...
Founder, SafeZone Safety Systems