【问题标题】:How to detect beacons using Android Beacon Library in foreground and background?如何在前台和后台使用 Android 信标库检测信标?
【发布时间】:2019-10-28 23:29:47
【问题描述】:

我正在尝试在我的应用程序运行时检测信标并将它们放入列表中,并在未运行时检测并显示通知。我似乎无法同时在 android 上同时工作。

我有我的主要活动......

[Activity(LaunchMode = LaunchMode.SingleInstance, Label = "MyApp", Icon = "@mipmap/icon", MainLauncher = true, Theme = "@style/MainTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IMonitorNotifier, IRangeNotifier, IBeaconConsumer, IBootstrapNotifier

protected override void OnCreate(Bundle savedInstanceState)
{
    set beacon parsers
    beaconmanager.bind(this)
    ...

public void OnBeaconServiceConnect()
{
    BeaconManager.AndroidBeaconManager.RemoveAllMonitorNotifiers();
    BeaconManager.AndroidBeaconManager.RemoveAllRangeNotifiers();

    BeaconManager.AndroidBeaconManager.AddMonitorNotifier(this);
    BeaconManager.AndroidBeaconManager.AddRangeNotifier(this);

    StartMonitoringBeacons...
    StartRangingBeacons...
}

那我有一个单独的班级...

[Activity(LaunchMode=LaunchMode.SingleInstance, Label="MyApp", MainLauncher = true)]
public class LaunchApplication : Application, IBootstrapNotifier, IRangeNotifier, IBeaconConsumer
{
    private RegionBootstrap _bootstrap = null;

    public override void OnCreate()
    {
        base.OnCreate();

        var proximities = BeaconManager.Instance.GetServerProximitiesAsync().GetAwaiter().GetResult();
        var regions = proximities.Select(x => new Region(x.UUID, null, null, null)).ToList();

        BeaconManager.GetParsers().ForEach(x => BeaconManager.AndroidBeaconManager.BeaconParsers.Add(x));

        _bootstrap = new RegionBootstrap(this, regions);
    }
    public void DidEnterRegion(Region region)
    {
        add parsers
        beaconmanager.bind(this);
    }
    public void OnBeaconServiceConnect()
    {
        BeaconManager.AndroidBeaconManager.AddMonitorNotifier(this);
        BeaconManager.AndroidBeaconManager.AddRangeNotifier(this);

        start monitoring...
        start ranging...

我没有在清单中添加任何内容,因为我相信它会自动使用 Activity 属性。

我的问题是我应该有一个 MainActivity 和一个派生自 Application 的单独类(创建区域引导程序)吗?

我是否绑定到不同的实例 (bind(this))?

当我调试 LaunchApplication OnCreate 时,它​​永远不会被调用,那么它是如何创建区域引导程序的?

我应该如何将其设置为仅在前台和后台运行时进行检测?

谢谢。

【问题讨论】:

  • 您为此使用什么 Xamarin 库?它是否有关于如何设置 RegionBootstrap 的示例?我是 Android 信标库的首席开发人员,并且知道它如何与本机应用程序一起使用,但我不确定 Xamarin 库包装器如何改变这一点。您显然 必须 以某种方式调用 onCreate。我们需要 Xamarin 专家来帮助解决这个问题。
  • 感谢大卫的帮助。问题是我认为应用程序无法在前台扫描信标并在不运行时扫描它们。一旦你调用 RegionBootstrap,它就会摆脱现有的监视器和游侠。您是否曾经编写过一个无需 Xamarin 的应用程序?

标签: android xamarin beacon


【解决方案1】:

对于这个特定问题,您不需要在活动中调用 bind() - 它已经由 RegionBootstrap 在内部完成(只要您可以确认在自定义应用程序类中调用了 onCreate)。

要使这两个地方都能工作,您只需删除对 bind() 的调用并取出删除活动中的测距和监视区域的代码(如果删除所有监视器通知器,您实际上禁用了 RegionBootstrap 回调,因为您正在删除其通知程序)。只需将代码留在您的活动中,为该活动添加新的监视器和范围通知器,然后根据活动的需要开始范围和监视。

请记住,BeaconManager 是一个单例。您的应用程序的所有组件(应用程序和活动类实例)都有一组范围和受监控的区域。因此,您只需要绑定一次(构造并且您可以添加任意数量的通知程序。如果您在一个类中删除通知程序,它也会从其他类中删除它们。

【讨论】:

  • David...我验证了如果我使用 BackgroundPowerSaver(this);然后它工作。没有它,我在运行和使用 RegionBootstrap 时不会在前台收到任何事件。你知道这是什么原因吗?我现在拥有主要活动中的所有内容。
【解决方案2】:

这不是您问题的答案,但可能是一个起点:

https://github.com/acaliaro/AltBeaconLibrarySample

这是@davidgyoung 库的“java 绑定”

https://github.com/acaliaro/BeaconLibraryBindings

你也可以在这里找到一些东西:https://github.com/chrisriesgo/Android-AltBeacon-Library

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多