Does Android Beacon Library really support background scanning?

↘锁芯ラ 提交于 2019-12-03 00:42:45

I worked with android iBeaon library in that for background scanning I created a service and in service I defined both monitoring and ranging. I start the service when application is distroy and its work for me. Create new service like this.

public class Beaconservice extends Service implements IBeaconConsumer {
private ArrayList<IBeacon> arrayL = new ArrayList<IBeacon>();
private BeaconServiceUtility beaconUtill = null;
private IBeaconManager iBeaconManager = IBeaconManager.getInstanceForApplication(this);
private Handler hand;
private Runnable runn;
private int count = 30;



@Override
public void onIBeaconServiceConnect() {
    iBeaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
            arrayL.clear();
            arrayL.addAll((ArrayList<IBeacon>) iBeacons);
            if(count>0){
                count=0;    
            }
        }
    });

    iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            Log.e("BeaconDetactorService", "didEnterRegion");
            // logStatus("I just saw an iBeacon for the first time!");
        }

        @Override
        public void didExitRegion(Region region) {
            Log.e("BeaconDetactorService", "didExitRegion");
            // logStatus("I no longer see an iBeacon");
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.e("BeaconDetactorService", "didDetermineStateForRegion");
            // logStatus("I have just switched from seeing/not seeing iBeacons: " + state);
        }

    });

    try {
        iBeaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }

    try {
        iBeaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }

}


@Override
public IBinder onBind(Intent arg0) {
    return null;
}
@Override
public void onCreate() {
    beaconUtill = new BeaconServiceUtility(this);
    Log.e("UUID","start service");

    hand = new Handler();
    runn = new Runnable() {

        @Override
        public void run() {
            count ++;
            hand.postDelayed(runn, 1000);
        }
    };

    hand.post(runn);
    super.onCreate();

}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
    beaconUtill.onStart(iBeaconManager, this);
    beaconUtill = new BeaconServiceUtility(this);
    super.onStart(intent, startId);
}
@Override
public void onDestroy() {
    beaconUtill.onStop(iBeaconManager, this);
    super.onDestroy();
}
}

In AndroidManifest.xml

 <service android:name="com.beacone.keyfinder.app.Beaconservice" >
 </service>

Yes, the Android Beacon Library will continue detecting beacons after the app is killed, but it can take time for scanning to resume. See here for details: http://altbeacon.github.io/android-beacon-library/resume-after-terminate.html

The library uses an AlarmManager to accomplish this, and the scanning can take up to five minutes to resume.

The reference app you link to actually has an example of how to set this up using the RegionBootstrap class. A more descriptive example is in the code examples under the Starting an App in the Background section here: http://altbeacon.github.io/android-beacon-library/samples.html

  1. No, nor does any other app. Killing a task via Task Switcher means killing the process of that app, and since your background scan relies on a service, that service is killed too since services don't start their own process.

  2. You can probably have an AlarmManager that periodically checks if your service is running and starts it automatically if it's not, but I'd imagine that would hit the battery big time if you set the interval too low.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!