我正在开发一个使用altbecaon库扫描信标的应用程序。每次更改方案时遇到不同的问题。我不知道是什么原因。一切正常。但是问题是当我关闭应用程序以免从最近的应用程序滑动然后再次打开时,我的应用程序未扫描。然后,我只是关闭了信标以使出口区域方法运行。之后,它照常进行扫描。即使我从最近的应用程序中关闭了我的应用程序并再次打开后,我也希望该应用程序能够进行扫描。请帮助我。
扫描片段的代码为:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.d("scannerfragment","oncraeteview");
view= inflater.inflate(R.layout.fragment_scanning, container, false);
beaconManager = BeaconManager.getInstanceForApplication(a);
ArrayList<OfferPromotion> offersPromotions=new ArrayList<>();
scanningPageAdapter = new ScanningPageAdapter(a,offersPromotions,ScanningPage.this);
recycler_scanner.setAdapter(scanningPageAdapter);
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.setForegroundScanPeriod(3001);
beaconManager.setForegroundBetweenScanPeriod(2001);
beaconManager.setBackgroundBetweenScanPeriod(2001);
beaconManager.setBackgroundScanPeriod(3001);
identifier = Identifier.parse("E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"); //beacon 1
region = new Region("myMonitoringUniqueId", identifier, null, null);
Log.w(TAG,"application"+Constantsforbinding.application_stateregion);
beaconManager.bind(this);
return view;
}
@Override
public Context getApplicationContext() {
return getActivity().getApplicationContext();
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
getActivity().unbindService(serviceConnection);
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return getActivity().bindService(intent, serviceConnection, i);
}
@Override
public void onBeaconServiceConnect() {
try{
beaconManager.updateScanPeriods();
}
catch (RemoteException e){
e.getLocalizedMessage();
Toast.makeText(a, ""+e, Toast.LENGTH_SHORT).show();
}
addmonitor();
addrangenotifieer();
beaconManager.addMonitorNotifier(monitorNotifier);
beaconManager.addRangeNotifier(rangeNotifier);
}
public void addmonitor(){
monitorNotifier=new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw an beacon for the first time!");
startbeaconranging(region);
nobeacon=0;
}
@Override
public void didExitRegion(final Region region) {
Log.i(TAG, "No beacons..waiting for 25 seconds to confirm");
beacondetected=false;
stopranginbeacon(region);
if(a!=null){
Toast.makeText(a, "No beacons..wait for 25 seconds to confirm", Toast.LENGTH_LONG).show();
a.runOnUiThread(new Runnable() {
@Override
public void run() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(beacondetected){
/*Toast.makeText(a, "Beacon detected", Toast.LENGTH_SHORT).show();*/
Log.i(TAG, ""+beacondetected);
}
else {
try {
lay_no_offer.setVisibility(View.VISIBLE);
((NavigationActivity)a).setAlreadyScannerlaunched(false);
Log.i(TAG, "no beacons for last 25 seconds"+beacondetected);
Toast.makeText(a, "no beacons for last 25 seconds", Toast.LENGTH_SHORT).show();
tv_status.setText("No beacons detected..scanning");
lay_having_offer.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
},25000);
}
});
}
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
if(state==1){
tv_status.setText("Scanning...wait for 15 seconds");
}else if(state==0){
tv_status.setText("No beacons..scanning");
}
}
};
}
public void addrangenotifieer(){
rangeNotifier = new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, final Region region) {
if (beacons.size() > 0) {
beacondetected=true;
Collections.sort((List<Beacon>) beacons, new Comparator<Beacon>()
{
@Override
public int compare(Beacon o1, Beacon o2) {
return Double.compare(o2.getDistance(),( o1.getDistance()));
}
});
mac1=((List<Beacon>) beacons).get(0).getBluetoothAddress();
nobeacon=0;
if (a != null) {
call_offers_api(mac1);
}
Log.i(TAG, "The first beacon I see is about "+nobeacon+((List<Beacon>) beacons).get(0).getBluetoothAddress()+"");
}
else {
}
}
};
}
@Override
public void onDestroyView() {
super.onDestroyView();
stopranginbeacon(region);
Stopmonitoringbecaon(region);
beaconManager.removeAllMonitorNotifiers();
beaconManager.removeAllRangeNotifiers();
beaconManager.unbind(this);
Log.d("scannerfragment","ondestroyview");
}
答案 0 :(得分:0)
了解,仅当进入与该区域匹配的信标时,didEnterRegion才会触发一次。即使停止并重新启动应用程序,库也会记住您所在区域的输入/输出状态。因此,EnterEnterRegion不会仅因为您重新启动应用程序而再次触发-信标实际上必须超出范围并重新出现才能再次触发。
由于上述原因,在didEnterRegion中开始范围调整可能会出现问题。相反,应从onBeaconServiceConnect或didDetermineStateForRegion开始。这两个回调总是在应用重启时触发。