我正在开发一个使用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");
}
了解didEnterRegion仅在匹配该区域的信标进入视图时触发一次。即使您停止并重新启动应用,图书馆也会记住您所在地区的进/出状态。所以didEnterRegion不会因为你重启你的应用而第二次开火 - 信标实际上必须超出范围一段时间并重新出现以便它再次开火。
由于上述原因,在didEnterRegion中开始测距可能会有问题。相反,在onBeaconServiceConnect或didDetermineStateForRegion中开始测距。这两个回调总是在应用程序重启时触发。