2

응용 프로그램을 만들었습니다.이 응용 프로그램은 비콘 범위를 지정하고 4 개 이상의 비컨이 원거리에 배치되었을 때 위치를 계산합니다.새로운 안드로이드 인 텐트를 열면 범위 찾기가 중지됩니다.

문제는 포지션을 보여주고 싶을 때 새로운 의도를 시작해야하기 때문에 메인 액티비티가 여전히 포 그라운드에 있지 않다는 것입니다. 약 5 초 후에 비컨의 거리가 멈추고 위치도 계산됩니다. 왜냐하면 비컨의 거리가 더 이상 변하지 않기 때문입니다.

계속해서 비콘을 측정 할 가능성이 있습니까? 내 주요 활동에서 비동기 작업을 시작하려고 시도했지만 작동하지 않습니다. 어쩌면 실수가있을 수 있습니다. 모르겠습니다. 여기

내 비동기 작업의 코드와 OnBeaconServiceConnect()이다 :

public class Monitor_Screen extends Activity implements BeaconConsumer, 
    SensorEventListener { 

private class asyncThread extends AsyncTask<Activity, Void, BeaconManager> { 


    @Override 
    protected BeaconManager doInBackground(Activity... arg0) { 
     // TODO Auto-generated method stub 

     Thread.currentThread().setName("BeaconManagerThread"); 

     Application myApplication = new Application(); 
     // BeaconManager 
     myBeaconManager = BeaconManager.getInstanceForApplication(myApplication); 
     myBeaconManager 
       .getBeaconParsers() 
       .add(new BeaconParser() 
         .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); 
     myBeaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1)); 
     myBeaconManager.bind(Monitor_Screen.this); 



     // Region 
     myRegion = new Region("all beacons", null, null, null); 
     startService(Monitor_Screen.this.getIntent()); 
     return myBeaconManager; 
    } 

} 

@Override 
public void onBeaconServiceConnect() { 
    // // range beacons on connect 

    try { 
     myBeaconManager.startRangingBeaconsInRegion(myRegion); 
    } catch (RemoteException e) { 
     Toast.makeText(getApplicationContext(), e.getMessage(), 
       Toast.LENGTH_LONG).show(); 
    } 
    myBeaconManager.setRangeNotifier(new RangeNotifier() { 

     public void didRangeBeaconsInRegion(Collection<Beacon> beacons, 
       Region myRegion) { 

      if (beacons.size() > 0) { 
       Iterator<Beacon> myIterator = beacons.iterator(); 
       while (myIterator.hasNext()) { 
        Beacon tempBeacon = myIterator.next(); 
        MyBeacon myBeacon = new MyBeacon(tempBeacon.getId1(), 
          tempBeacon.getId2(), tempBeacon.getId3(), 
          tempBeacon.getBluetoothAddress(), tempBeacon 
            .getRssi(), tempBeacon.getDistance(), 
          tempBeacon.getTxPower(), System 
            .currentTimeMillis(), 
          new Position(0, 0)); 
        boolean isIn = false; 
        for (MyBeacon blub : myVector) { 
         if (blub.getBTAdress().equals(
           myBeacon.getBTAdress())) { 
          isIn = true; 
          blub.distance = myBeacon.getDistance(); 
         } 
        } 
        if (!isIn) 
         myVector.add(myBeacon); 
       } 
      } 

      logBeaconData(); 
      for (int i = 0; i < myVector.size(); i++) { 
       if (System.currentTimeMillis() 
         - myVector.get(i).getTimeStamp() > 10000) { 
        myVector.remove(i); 
       } 
      } 
     } 
    }); 

    try { 
     myBeaconManager.startMonitoringBeaconsInRegion(myRegion); 
    } catch (RemoteException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    myBeaconManager.setMonitorNotifier(new MonitorNotifier() { 

     @Override 
     public void didExitRegion(Region arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void didEnterRegion(Region arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void didDetermineStateForRegion(int arg0, Region arg1) { 
      // TODO Auto-generated method stub 

     } 
    }); 

} 

답변

0

백그라운드에서 계속 징을 비콘을하는 가장 쉬운 방법은 사용자 정의 android.app.Application 클래스에 이르기까지 비콘을 설정하고,이 해당 Activity에 콜백을 전달하여 Activity이 포 그라운드에있는 경우에만 디스플레이를 업데이트합니다.

당신은 여기 Android Beacon Library에 대한 참조 응용 프로그램에서이 작업을 수행하는 예를 볼 수 아래 https://github.com/AltBeacon/android-beacon-library-reference

이이 설정 사용자 지정 android.app.Application 클래스에서 발췌 한 것입니다. 범위 지정은 한 번 설정되며 응용 프로그램의 전체 라이프 사이클 동안 계속됩니다. 레인 징 콜백은 존재하는 경우 mRangingActivity으로 전달되므로 필요한 모든 UI 처리를 수행 할 수 있습니다.

public class BeaconReferenceApplication extends Application implements BootstrapNotifier, RangeNotifier { 
... 

    public void onCreate() { 
     mAllBeaconsRegion = new Region("all beacons", null, null, null); 
     mBeaconManager = BeaconManager.getInstanceForApplication(this); 
     mBackgroundPowerSaver = new BackgroundPowerSaver(this);  
     mRegionBootstrap = new RegionBootstrap(this, mAllBeaconsRegion); 
    } 

    @Override 
    public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) { 
     if (mRangingActivity != null) { 
      mRangingActivity.didRangeBeaconsInRegion(arg0, arg1); 
     } 

    } 

    @Override 
    public void didEnterRegion(Region arg0) { 
     try { 
      Log.d(TAG, "entered region. starting ranging"); 
      mBeaconManager.startRangingBeaconsInRegion(mAllBeaconsRegion); 
      mBeaconManager.setRangeNotifier(this); 
     } catch (RemoteException e) { 
      Log.e(TAG, "Cannot start ranging"); 
     } 
    } 

... 
} 
+0

불행히도이 솔루션은 나에게 적합하지 않습니다. 새 프로젝트를 시작하고 ReferenceApplication을 테스트했지만 작동하지 않습니다. 응용 프로그램이 충돌 함 : ( –

+0

참조 응용 프로그램의 수정되지 않은 버전을 실행 해보십시오. 충돌이 표시되면 스택 추적에 대한 링크를 붙여 넣으십시오. – davidgyoung