2015-04-22 31 views

回答

3

這是怎麼了,我通常設置我的信標。首先你需要一個服務類:

public class BeaconServicing extends Service { 

private static Context context; 
private static final Region ALL_BEACONS = new Region(BeaconConfig.GLOBAL_REGION_ID, BeaconConfig.UUID, BeaconUtils.ALL_MAJOR, BeaconUtils.ALL_MINOR); 
private static BeaconManager beaconManager; 

public static BeaconManager getBeaconManager() { 
    if (beaconManager == null) { 
     beaconManager = new BeaconManager(context); 
    } 
    return sBeaconManager; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    context = this; 
    startBeaconing(); 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

private void startBeaconing() { 
    if (BeaconUtils.isCapable()) { 
     beaconManager = getBeaconManager(); 
     beaconManager.setErrorListener(new BeaconError()); 
     beaconManager.setMonitoringListener(new BeaconMonitoring()); 
     beaconManager.setRangingListener(new BeaconRanging()); 
     beaconManager.setBackgroundScanPeriod(BeaconConfig.BACKGROUND_SCAN_PERIOD, BeaconConfig.BACKGROUND_WAIT_PERIOD); 
     beaconManager.setForegroundScanPeriod(BeaconConfig.FOREGROUND_SCAN_PERIOD, BeaconConfig.FOREGROUND_WAIT_PERIOD); 
     beaconManager.connect(new BeaconManager.ServiceReadyCallback() { 
      @Override 
      public void onServiceReady() { 
       beaconManager.startMonitoring(ALL_BEACONS); 
      } 
     }); 
    } 
} 
} 

這就是你打電話來啓動信標服務。從這裏你會想要你的監控類:

public class BeaconMonitoring implements BeaconManager.MonitoringListener { 

private static final String TAG = BeaconMonitoring.class.getSimpleName(); 

@Override 
public void onEnteredRegion(Region region, List<Beacon> beacons) { 
    Log.d(TAG, "Entering region: " + region.getIdentifier()); 
    BeaconServicing.getBeaconManager().startRanging(region); 
    // send notification that you have entered range of beacon 
} 

@Override 
public void onExitedRegion(Region region) { 
    Log.d(TAG, "Exiting region: " + region.getIdentifier()); 
    BeaconServicing.getBeaconManager().stopRanging(region); 
} 
} 

這是當你進入和退出一個區域將監視。從這些方法你可以做任何你需要做的事情。大多數其他類的簡單實現相應的監聽器和覆蓋的方法,就像這樣:

public class BeaconRanging implements BeaconManager.RangingListener { 

@Override 
public void onBeaconsDiscovered(Region region, List<Beacon> beacons) { 
} 
} 

然後我的燈塔配置類包含掃描時間和IDS,我需要:

public class BeaconConfig { 

public static final long FOREGROUND_SCAN_PERIOD = 1000; 
public static final long FOREGROUND_WAIT_PERIOD = 0; 

public static final long BACKGROUND_SCAN_PERIOD = 5000; 
public static final long BACKGROUND_WAIT_PERIOD = 5000; 

public static final String GLOBAL_REGION_ID = App.getContext().getPackageName(); 
public static final String UUID = "your uuid"; 
} 

然後開始它,你只是這樣做:

startService(new Intent(this, BeaconServicing.class)); 

你準備好了。這應該足以讓你朝正確的方向發展。

+0

謝謝@ Zamereon – Kheerthi

+0

@ zamereon我越來越錯誤可以幫助我http://stackoverflow.com/q/30231811/3292795 – prabhakaran

相關問題