Search code examples
androidibeaconibeacon-androidandroid-ibeacon

How do I implement background service for kontakt.io sdk?


If I enter a region of a beacon with my Android device I like to show a notification. Thats well documented at Android SDK Quickstart

This is just working as long as the app is active. How do I get notifications when the app is closed?


Solution

  • I build a service by myself:

    BeaconRangingService.java

        public class BeaconRangingService extends Service {
    
            private static final String TAG = BeaconRangingService.class.getSimpleName();
            private BeaconManager beaconManager;
    
            @Override
            public void onCreate() {
                super.onCreate();
                beaconManager = BeaconManager.newInstance(getApplicationContext());
                beaconManager.setMonitorPeriod(MonitorPeriod.MINIMAL);
                beaconManager.setForceScanConfiguration(ForceScanConfiguration.DEFAULT);
                beaconManager.registerMonitoringListener(new BeaconManager.MonitoringListener() {
                    @Override
                    public void onMonitorStart() {
                        Log.v(TAG, "start monitoring beacons");
                    }
    
                    @Override
                    public void onMonitorStop() {
                        Log.wtf(TAG, "stop monitoring beacons");
                    }
    
                    @Override
                    public void onBeaconsUpdated(Region region, List<Beacon> list) {
    
                    }
    
                    @Override
                    public void onBeaconAppeared(Region region, Beacon beacon) {
                       Toast.makeText(getApplicationContext(), "Beacon appeared\n BEACON ID: " + beacon.getBeaconUniqueId(), Toast.LENGTH_SHORT).show();
                    }
    
                    @Override
                    public void onRegionEntered(Region region) {
    
                    }
    
                    @Override
                    public void onRegionAbandoned(Region region) {
    
                    }
                });
    
            }
    
            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
    
                Log.v(TAG, "service started");
    
                if (!beaconManager.isBluetoothEnabled()) {
                    Log.w(TAG, "bluetooth disabled, stop service");
                    stopSelf();
                } else {
                    connect();
                }
    
                return super.onStartCommand(intent, flags, startId);
            }
    
            @Override
            public IBinder onBind(Intent intent) {
                return null;
            }
    
            @Override
            public void onDestroy() {
                Log.v(TAG, "service destroyed");
                beaconManager.stopMonitoring();
                beaconManager.disconnect();
                beaconManager = null;
                super.onDestroy();
            }
    
            private void connectBeaconManager() {
                try {
                    beaconManager.connect(new OnServiceBoundListener() {
                        @Override
                        public void onServiceBound() {
                            try {
                                HashSet<Region> regions = new HashSet<>();
                                regions.add(Region.EVERYWHERE);
                                beaconManager.startMonitoring(regions);
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (RemoteException e) {
                    throw new IllegalStateException(e);
                }
            }
    
        }     
    

    AndroidManifest.xml

     <service
                android:name="com.your.package.BeaconRangingService"
                android:exported="false"/>
    

    start service

     Intent intent = new Intent(this, BeaconRangingService.class);
     startService(intent);