AltBeacon / android-beacon-library

Allows Android apps to interact with BLE beacons
Apache License 2.0
2.84k stars 836 forks source link

TargetSdk 34 (android 14) Beacon foreground service no longer working due to new Google restriction #1179

Closed rkostrab closed 7 months ago

rkostrab commented 7 months ago

After upgrading targetSdk from 33 to 34. App has began to crash. I checked logs and saw the error (below).

Expected behavior

When I start ranging beacons it should start ranging.

Actual behavior

When starting Beacon foreground service, app crashing. Log below

java.lang.RuntimeException: Unable to create service org.altbeacon.beacon.service.BeaconService: android.app.MissingForegroundServiceTypeException: Starting FGS without a type  callerApp=ProcessRecord{a4d7f35 2231:com.example.myapp/u0a672} targetSDK=34

Steps to reproduce this behavior

public void startRangingBeacons(String uuid) {
        beaconManager.addRangeNotifier(this);
        try {
            beaconManager.startRangingBeacons(new Region(uuid, null, null, null));
            NotificationManager notificationManager = (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(NOTIFICATION_ID, createNotification("Running"));
        } catch (Exception e) {
            onRangeListener.onRangeBeacons(new ArrayList<>(), e.getMessage());
        }
    }

Mobile device model and OS version

Google Pixel 6 Pro (android 14)

Android Beacon Library version

2.19

Here's docs about this issue: https://developer.android.com/about/versions/14/changes/fgs-types-required

rkostrab commented 7 months ago

New version of this library resolved the issue.