gpt4 book ai didi

android - 如何识别哪个信标正在退出,当信标超出范围时看到的信标通知

转载 作者:行者123 更新时间:2023-11-29 01:34:48 26 4
gpt4 key购买 nike

我正在尝试使用我的设备检测信标设备,从而监控它们并收听范围通知。

我正在使用 this图书馆和图书馆引用应用程序。我设法使用设置信标布局方法收听自定义信标。

第一步。我在应用程序类中设置布局第 2 步。我让 baseactivity 实现 beaconconsume,它执行测距。

当启动范围监控服务/方法时,我们使用“myRangeUniqueId”,但我认为 didexit 和 didenter 方法使用“backgroundId”。为什么会这样?

所以情况是这样的我将信标设备移到几米远,我仍然看到信标通知,但我没有看到信标。即使信标很远,这些消息也会交替变化。

我需要做些什么来防止这种情况发生吗?请帮忙。

代码 fragment 如下:

  1. 应用类实现了BootstrapNotifier,代码如下

    BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("LAYOUT_HERE"));

    Region region = new Region("backgroundRegion",
    null, null, null);
    regionBootstrap = new RegionBootstrap(this, region);

    // simply constructing this class and holding a reference to it in your custom Application
    // class will automatically cause the BeaconLibrary to save battery whenever the application
    // is not visible. This reduces bluetooth power usage by about 60%
    backgroundPowerSaver = new BackgroundPowerSaver(this);



    @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.
    Log.d(TAG, "did enter region.");
    if (!haveDetectedBeaconsSinceBoot) {
    Log.d(TAG, "auto launching MainActivity");
    haveDetectedBeaconsSinceBoot = true;
    } else {
    // i am not sending out any notification here since there could be multiple beacons and i need to identify any one of them with a specific uuid
    }


    }


    @Override
    public void didExitRegion(Region region) {
    sendNotification("exited",2);
    }

我在 didDetermineStateForRegion 方法中什么都不做,它只是被覆盖了

  1. 我有一个 BaseActivity 实现了 BeaconConsumer

        private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    beaconManager.bind(this);
    }

    @Override
    protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
    }
    @Override
    protected void onPause() {
    super.onPause();
    if(beaconManager.isBound(this))beaconManager.setBackgroundMode(true);
    }
    @Override
    protected void onResume() {
    super.onResume();
    if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(false);
    }

    @Override
    public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
    if (beacons.size() > 0) {
    /*EditText editText = (EditText)RangingActivity.this
    .findViewById(R.id.rangingText);
    Beacon firstBeacon = beacons.iterator().next();
    logToDisplay("The first beacon "+firstBeacon.toString()+" is about "+firstBeacon.getDistance()+" meters away."); */
    CommonUtilities.sendNotification(BaseActivity.this,"entered",1);
    }
    }
    }
    }
    }
    });

    try {
    beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId",null, null, null));
    } catch (RemoteException e) { }
    }

P.S:当我将设备移动到另一个位置时,比如大约 5 米远,我会收到随机通知,告知信标在范围内,然后我立即收到通知,告知信标超出范围。

谢谢

最佳答案

几点:

  1. 唯一标识符用作标识区域 的键,因此您可以启动和停止测距和监控。您构建并在系统中注册的每个区域都应该有一个不同的字符串标识符,但该值可以是您不是的任何值,只要它是唯一的即可。

  2. didRangeBeaconsInRegion 回调每秒进行一次,而不仅仅是当移动设备进入信标范围时。

  3. 如果当信标在 20 米左右范围内时您收到重复的 didExitRegion 回调,则您的信标可能没有足够频繁地传输。了解信标品牌、型号和传输频率以及您的移动设备型号可能有助于解决此问题。

关于android - 如何识别哪个信标正在退出,当信标超出范围时看到的信标通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29002037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com