首页 文章

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

提问于
浏览
0

我正在使用altbeacon参考应用程序来处理信标 . 当我尝试打印arg0时我得到null我在监视信标时得到null .

@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系统杀死 . 我以前尝试这样做并将结果存储在一个集合中,看看该区域是否有新的信标,然后将其添加到那里,但导致服务被杀死 .

Edit: 我尝试了以下方法

@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:Iam试图在应用程序被杀时获取测距日志

1 回答

  • 0

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

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

    • 除监视API外,还使用测距API . (你不会't have to turn on ranging after didEnterRegion is called, you can just turn it on at the same time as monitoring and leave it on.) When ranging is turned on, you'回到 didRangeBeaconsInRegion(...) 回调,其中包含检测到的实际信标的列表 . 然后你可以从这个回调中读取第一个标识符 .

相关问题