<分区>
你好,我刚开始使用 estimote beacons 并尝试构建一个示例应用程序来查找信标范围并在信标接近范围时向移动设备发送通知。
<分区>
你好,我刚开始使用 estimote beacons 并尝试构建一个示例应用程序来查找信标范围并在信标接近范围时向移动设备发送通知。
最佳答案
这就是我通常设置信标的方式。首先,您需要一个服务类:
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) {
}
}
然后我的信标配置类包含我需要的扫描周期和 ID:
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));
然后你就可以开始了。这至少应该足以让您朝着正确的方向前进。
关于android - 谁能帮助我开始使用 estimote beacons 示例 android 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29805672/