2014-01-17 4 views
1

저는 RadiusNetworks API를 사용하여 iBeacons를 사용하고 있습니다.iBeacon 백그라운드 스캐닝 RadiusNetworks 라이브러리의 PRO 기능?

이 라이브러리를 사용했지만 제대로 작동하지만 배경 스캔이 내 응용 프로그램에서 발생하지 않는다고 내가 잘못하고 있는지 물어볼 필요가 있습니까? 내가 뭘 놓치고 있니? 여기

어떻게 당신이 당신의 활동을 보내는 지금까지, 난 단지 그러나 배경에 활동을 설정, 비콘 측에 관련 코드를 유지 완전히 스캔을 정지가 구현 ...

package ro.gebs.zonizbeacon; 



public class MainActivity extends FragmentActivity 
     implements NavigationDrawerFragment.NavigationDrawerCallbacks, SearchView.OnQueryTextListener, IBeaconConsumer, RangeNotifier, IBeaconDataNotifier { 
    /** 
    * Fragment managing the behaviors, interactions and presentation of the navigation drawer. 
    */ 
    private NavigationDrawerFragment mNavigationDrawerFragment; 
    private MainOffersFragment mOffersFragment; 

    protected static final String TAG = "BeaconActivity"; 
    private IBeaconManager iBeaconManager = IBeaconManager.getInstanceForApplication(this); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //getActionBar().setIcon(R.drawable.home); 
     setContentView(R.layout.activity_main); 



     BeaconUtils.verifyBluetooth(MainActivity.this); 
     iBeaconManager.bind(this); 
    } 




    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     iBeaconManager.unBind(this); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (iBeaconManager.isBound(this)) iBeaconManager.setBackgroundMode(this, true); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     if (iBeaconManager.isBound(this)) iBeaconManager.setBackgroundMode(this, false); 

    } 


    @Override 
    public void onIBeaconServiceConnect() { 
     Region region = new Region("MainActivityRanging", null, null, null); 
     try { 
      iBeaconManager.startRangingBeaconsInRegion(region); 
      iBeaconManager.setRangeNotifier(this); 
     } catch (RemoteException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void iBeaconDataUpdate(IBeacon iBeacon, IBeaconData iBeaconData, DataProviderException e) { 
     if (e != null) { 
      Log.d(TAG, "data fetch error:" + e); 
     } 
     if (iBeaconData != null) { 
      String displayString = iBeacon.getProximityUuid() + " " + iBeacon.getMajor() + " " + iBeacon.getMinor() + "\n" + "Welcome message:" + iBeaconData.get("welcomeMessage"); 
      Log.d(TAG, displayString); 
     } 
    } 

    @Override 
    public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) { 
     for (IBeacon iBeacon : iBeacons) { 
      iBeacon.requestData(this); 
      String displayString = iBeacon.getProximityUuid() + " " + iBeacon.getMajor() + " " + iBeacon.getMinor() + "\n"; 
      Log.d(TAG, displayString); 
     } 
    } 
} 

답변

2

입니다 배경? 뒤로 버튼, 홈 버튼 또는 무엇을 치는거야?

나는 무슨 일이 일어나고 있는지 안드로이드가 실제로 당신의 앱을 종료하고 있다고 생각합니다. 귀하의 onDestroy 방법은 다음과 같습니다

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    iBeaconManager.unBind(this); 
} 

안드로이드이 메소드를 호출하면

iBeaconManagerAndroidIBeaconService 효과적으로 중지 스캔에 바인딩을 해제합니다. 이 코드를 제거하더라도 Android는 앱 종료를 결정하면 자동으로 서비스를 종료합니다.

이 작업을 백그라운드에서 계속 실행하려면 IBeaconManager을 해당 작업보다 수명주기가 긴 무언가에 연결해야합니다. 이 작업을 수행하는 가장 쉬운 방법은 (또한 매니페스트에 선언해야 함)과 같은 사용자 정의 안드로이드 어플리케이션 클래스입니다 : (전용 라이브러리의 프로 버전)

public class MyTestIBeaconApplication extends Application { 
    private BackgroundPowerSaver backgroundPowerSaver; 
    private IBeaconManager iBeaconManager; 

    public void onCreate() { 
     super.onCreate(); 
     // Simply constructing this class and holding a reference to it 
     // enables auto battery saving of about 60% 
     backgroundPowerSaver = new BackgroundPowerSaver(this); 
     iBeaconManager = IBeaconManager.getInstanceForApplication(this); 
    } 
} 

BackgroundPowerSaver 일부 선택 사항이지만 앱이 배터리를 절약하기 위해 백그라운드에있을 때 자동으로 스캔 빈도를 낮 춥니 다. 이 기능을 사용하면 다양한 및 onResume 방법에서 setBackgroundMode으로 전화하지 않아도됩니다.

+1

답장을 보내 주셔서 감사합니다. 정말 유용한 팁과 멋진 라이브러리입니다. :) –