AltBeacon / android-beacon-library

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

didRangeBeaconsInRegion is not bringing the beacon collection after certain interval( like 1hr) #1087

Open rakathal opened 2 years ago

rakathal commented 2 years ago

Expected behavior

Once the Beacon Manager starts, the app should run continuously in the background all the time, until it is manually stopped. Which means if the beacons are nearby the phone, we should receive beacon information all the time and should run until the battery die or manually stopped.

Call to didRangeBeaconsInRegion should happen every second and bring beacon information every second if the phone is nearby.

Actual behavior

didRangeBeaconsInRegion is happening every sec, however after certain interval like( 1hr or 8hr or etc, nothing particular), empty beacon information is getting sent. Eg:

Steps to reproduce this behavior

Code setup:

public void onCreate() {
        super.onCreate();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_ID)
                .setContentTitle("Service is running")
                .setContentText("")
                .setSmallIcon(R.drawable.ic_baseline_directions_walk)
                .setContentIntent(pendingIntent)
                .build();
        BeaconManager.getInstanceForApplication(this).setRegionStatePersistenceEnabled(false);
        mBeaconManager = BeaconManager.getInstanceForApplication(this);
        mBeaconManager.setEnableScheduledScanJobs(false);
        mBeaconManager.setDistanceModelUpdateUrl("invalid");
        mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
        mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
        // Binds this activity to the BeaconService        
        startForeground(1, notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mBeaconManager.bind(this);
        return START_STICKY;
    }

 @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
@Override
public void onBeaconServiceConnect() {
        // Encapsulates a beacon identifier of arbitrary byte length
        identifiers = new ArrayList<>();
        // Set null to indicate that we want to match beacons with any value
        identifiers.add(null);
        // Represents a criteria of fields used to match beacon
        region = new Region("AllBeaconsRegion", identifiers);
        try {
            // Tells the BeaconService to start looking for beacons that match the passed Region object
            //Log.i("","Inside Service Connect");
            mBeaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        // Specifies a class that should be called each time the BeaconService gets ranging data, once per second by default
        mBeaconManager.addRangeNotifier(this);
    }

@Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        try {
            if (beacons.size() > 0) {
                // Beacons Infomation
            } else {
               // empty beacon information
            }

In Android Menifest.xml
<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
</application ....>
<activity android:name="com.example.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.example.MyService"
            android:exported="false"
            android:directBootAware="true"></service>
<service android:name="org.altbeacon.beacon.service.BeaconService"
            android:exported="false"
            android:enabled="true"
            android:directBootAware="true"></service>
        <service android:name="org.altbeacon.beacon.service.ScanJob"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:directBootAware="true">
            <meta-data android:name="immediateScanJobId" android:value="208352939" />
        </service>
        <service
            android:name="org.altbeacon.beacon.BeaconIntentProcessor"
            android:enabled="true"
            android:exported="false"
            android:directBootAware="true"/>
    </application>

Mobile Setup to run the app indefinitly

Battery Optimization - Disabled Provided all these permissions in Location, File, Storage, Telephone Bluetooth - ON, Location On, Mobile Data On, Wifi On

Mobile device model and OS version

Samsung X Cover Pro Device with OS Android 10, 11

Android Beacon Library version

2.19

IMPORTANT: This forum is reserved for feature requests or reproducible bugs with the library itself. If you need help with using the library with your project, please open a new question on StackOverflow.com.

davidgyoung commented 2 years ago

@rakathal please read the instructions here and see if that helps.