如何在监控信标时获取 UUID?



我正在使用altbeacon参考应用程序来处理信标。当我尝试打印 arg0 时,我得到空,我在监控信标时得到空。

@Override
public void didEnterRegion(Region arg0) {
// In this example, this class sends a notification to the user 
whenever a Beacon
// matching a Region (defined above) are first seen.
System.out.println(arg0);
System.out.println(arg0.getUniqueId());
System.out.println(arg0.getId1());
Log.d(TAG, "did enter region.");
}

如何在此处获取 UUID?是否有必要在这里调用范围,因为我正在尝试制作前台服务,如果我每次遇到didEnter事件时都调用范围服务,它不会很重并被android系统杀死。 我之前尝试这样做并将结果存储在集合中,看看是否有新的信标出现在该地区,然后将其添加到那里,但导致服务被杀死。

编辑:我尝试了以下方法

@Override
public void didEnterRegion(Region arg0) {
if (!haveDetectedBeaconsSinceBoot) {
Log.d(TAG, "auto launching MainActivity");
Intent RangingIntent = new Intent(this, Monitoring.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getApplicationContext().startForegroundService(RangingIntent);
} else  {
getApplicationContext().startService(RangingIntent);
}
haveDetectedBeaconsSinceBoot = true;
} else {
if (monitoringActivity != null) {
Intent RangingIntent = new Intent(this, Monitoring.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {               
getApplicationContext().startForegroundService(RangingIntent);
} else  {
getApplicationContext().startService(RangingIntent);
}
}
}

测距代码

public class Monitoring extends Service implements BeaconConsumer {
protected static final String TAG = "RangingService";
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
super.onCreate();
System.out.println("********STARTING RANGING*********");
beaconManager.bind(this);
}
@Override
public void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier()  {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.d(TAG, "didRangeBeaconsInRegion called with beacon count:  "+beacons.size());
Log.d(TAG, String.valueOf(beacons.iterator().next()));
Beacon firstBeacon = beacons.iterator().next();
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) {   }
}
}

PS:我正在尝试在应用程序被杀死时获取测距日志

如果使用将每个标识符设置为 null 的通配符区域,则根本无法使用监视 API 读取标识符。 两个选项:

  1. 定义多个区域,为每个区域指定 UUID,然后监视所有这些区域。 当您收到回调时,作为参数传递的区域对象将在调用region.getId1()时包含 UUID。

  2. 除了监控 API 之外,还使用范围 API。(调用didEnterRegion后,您不必打开范围,只需在监视的同时将其打开并保持打开状态即可。 打开测距后, 您将收到一个回调didRangeBeaconsInRegion(...),其中包含检测到的实际信标列表. 然后,您可以从此回调中读取第一个标识符。

最新更新