Android Beacon 库真的支持后台扫描吗?

2024-01-06

我在用Android 信标库 https://github.com/AltBeacon/android-beacon-library用于 BLE 扫描example https://github.com/AltBeacon/android-beacon-library-reference。它在前台的监控和测距方面工作得很好。但是,对于后台,它仅适用于在应用程序中按“Home”并且屏幕关闭的情况。当我从任务切换器杀死该应用程序时,它不起作用。在这个例子中,我找不到像服务这样的东西来让事情在后台工作。

问题:

  1. 如果应用程序在任务切换器中被杀死,Android Beacon 库是否支持后台扫描?
  2. 如果是这样,该怎么做?有什么例子吗?

我使用 android iBeaon 库进行后台扫描,我创建了一个服务,并在服务中定义了监控和测距。当应用程序被销毁并且它为我工作时,我启动该服务。创建这样的新服务。

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();
}
}

在AndroidManifest.xml中

 <service android:name="com.beacone.keyfinder.app.Beaconservice" >
 </service>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Beacon 库真的支持后台扫描吗? 的相关文章

随机推荐