【问题标题】:Multiple Estimote/Beacons in Xamarin.AndroidXamarin.Android 中的多个 Estimote/信标
【发布时间】:2017-05-24 05:42:40
【问题描述】:

我对这个信标概念非常陌生。出于演示目的,我使用此示例应用程序 Android iBeacon App 配置了我的信标/估计之一。

在这里,应用程序可以一次找到单个信标。我的意思是我必须通过单个信标的 UUID 来查找它是否在信标范围内。有没有可能找到多个信标使用相同的应用程序。? 我的意思是如果用户进入特定的信标范围,用户应该得到有关特定信标的通知。那么是否有可能添加多个信标..?根据信标的 UUID,我必须区分信标的范围。任何人都可以指导任何好的教程或如何修改这个。任何帮助将不胜感激..

这是我的 MainActivity.cs

using System.Linq;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Views;
using Android.Widget;
using Android.OS;
using RadiusNetworks.IBeaconAndroid;
using Color = Android.Graphics.Color;
using Android.Bluetooth;

namespace FindTheMonkey.Droid
{
    [Activity(Label = "Estimote", MainLauncher = true, LaunchMode = LaunchMode.SingleTask)]
    public class MainActivity : Activity, IBeaconConsumer
    {
        private const string UUID = "78540181-ea7f-fc83-2d61-4031622455b6";
        private const string UUID1 = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
        private const string UUID2 = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
        private const string monkeyId = "blueberry";

        bool _paused;
        private BluetoothManager _manager;
        View _view;
        IBeaconManager _iBeaconManager;
        MonitorNotifier _monitorNotifier;
        RangeNotifier _rangeNotifier;
        Region _monitoringRegion;
        Region _rangingRegion;
        TextView _text;

        int _previousProximity;

        public MainActivity()
        {
            _iBeaconManager = IBeaconManager.GetInstanceForApplication(this);

            _monitorNotifier = new MonitorNotifier();
            _rangeNotifier = new RangeNotifier();

            _monitoringRegion = new Region(monkeyId, UUID, null, null);
            _rangingRegion = new Region(monkeyId, UUID, null, null);
        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            _view = FindViewById<RelativeLayout>(Resource.Id.findTheMonkeyView);
            _text = FindViewById<TextView>(Resource.Id.monkeyStatusLabel);

            _iBeaconManager.Bind(this);

            _monitorNotifier.EnterRegionComplete += EnteredRegion;
            _monitorNotifier.ExitRegionComplete += ExitedRegion;

            _rangeNotifier.DidRangeBeaconsInRegionComplete += RangingBeaconsInRegion;
        }

        protected override void OnResume()
        {

            _manager = (BluetoothManager)Application.Context.GetSystemService(BluetoothService);
            _manager.Adapter.Enable();

            base.OnResume();
            _paused = true;
        }

        protected override void OnPause()
        {
            base.OnPause();
            _paused = true;
        }

        void EnteredRegion(object sender, MonitorEventArgs e)
        {
            if(_paused)
            {
                ShowNotification();
            }
        }

        void ExitedRegion(object sender, MonitorEventArgs e)
        {
            if (_paused)
            {   
                ShowNotification1();
                UpdateDisplay("There are no beacons around you.!", Color.Black);
            }
        }

        void RangingBeaconsInRegion(object sender, RangeEventArgs e)
        {
            if (e.Beacons.Count > 0)
            {
                var beacon = e.Beacons.FirstOrDefault();
                var message = string.Empty;

                switch((ProximityType)beacon.Proximity)
                {
                    case ProximityType.Immediate:
                        UpdateDisplay("You found the Estimote!", Color.Green);
                        break;
                    case ProximityType.Near:
                        UpdateDisplay("You're getting warmer", Color.Yellow);
                        break;
                    case ProximityType.Far:
                        UpdateDisplay("You're freezing cold", Color.Blue);
                        break;
                    case ProximityType.Unknown:
                        UpdateDisplay("I'm not sure how close you are to the Estimote", Color.Red);
                        break;
                }

                _previousProximity = beacon.Proximity;
            }
        }

        #region IBeaconConsumer impl
        public void OnIBeaconServiceConnect()
        {
            _iBeaconManager.SetMonitorNotifier(_monitorNotifier);
            _iBeaconManager.SetRangeNotifier(_rangeNotifier);

            _iBeaconManager.StartMonitoringBeaconsInRegion(_monitoringRegion);
            _iBeaconManager.StartRangingBeaconsInRegion(_rangingRegion);
        }
        #endregion

        private void UpdateDisplay(string message, Color color)
        {
            RunOnUiThread(() =>
            {
                _text.Text = message;
                _view.SetBackgroundColor(color);
            });
        }

        private void ShowNotification()
        {
            var resultIntent = new Intent(this, typeof(MainActivity));
            resultIntent.AddFlags(ActivityFlags.ReorderToFront);
            var pendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.UpdateCurrent);
            var notificationId = Resource.String.monkey_notification;

            var builder = new Notification.Builder(this)
                .SetSmallIcon(Resource.Drawable.Xamarin_Icon)
                .SetContentTitle(this.GetText(Resource.String.app_label))
                .SetContentText(this.GetText(Resource.String.monkey_notification))
                .SetContentIntent(pendingIntent)
                .SetAutoCancel(true);

            var notification = builder.Build();

            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.Notify(notificationId, notification);
        }

        private void ShowNotification1()
        {
            var resultIntent = new Intent(this, typeof(MainActivity));
            resultIntent.AddFlags(ActivityFlags.ReorderToFront);
            var pendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.UpdateCurrent);
            var notificationId = Resource.String.monkey_notification1;

            var builder = new Notification.Builder(this)
                .SetSmallIcon(Resource.Drawable.Xamarin_Icon)
                .SetContentTitle(this.GetText(Resource.String.app_label))
                .SetContentText(this.GetText(Resource.String.monkey_notification1))
                .SetContentIntent(pendingIntent)
                .SetAutoCancel(true);

            var notification = builder.Build();

            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.Notify(notificationId, notification);
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();

            _monitorNotifier.EnterRegionComplete -= EnteredRegion;
            _monitorNotifier.ExitRegionComplete -= ExitedRegion;

            _rangeNotifier.DidRangeBeaconsInRegionComplete -= RangingBeaconsInRegion;

            _iBeaconManager.StopMonitoringBeaconsInRegion(_monitoringRegion);
            _iBeaconManager.StopRangingBeaconsInRegion(_rangingRegion);
            _iBeaconManager.UnBind(this);
        }
    }
}



EDIT1: 当用户进入信标的不同区域时,我已经尝试过类似的方法来触发通知..但是每次都会为两个信标触发相同的通知..我的意思是每次如果用户进入该区域将调用 ShowNotification(),如果用户退出该区域将调用 ShowNotification1()。

当用户进入其区域时,如何为不同的信标调用不同的通知..?

namespace FindTheMonkey.Droid
{
    [Activity(Label = "Estimote", MainLauncher = true, LaunchMode = LaunchMode.SingleTask)]
    public class MainActivity : Activity, IBeaconConsumer
    {
        private const string UUID = "78540181-ea7f-fc83-2d61-4031622455b6";
        private const string monkeyId = "blueberry";
        private const int major = 26110;
        private const int minor = 16681;

        private const string UUID1 = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
        private const string monkeyId1 = "mint";
        private const int major1 = 62068;
        private const int minor1 = 28983;


        bool _paused;
        private BluetoothManager _manager;
        View _view;
        IBeaconManager _iBeaconManager;
        MonitorNotifier _monitorNotifier;
        RangeNotifier _rangeNotifier;
        Region _monitoringRegion;
        Region _rangingRegion;

        IBeaconManager _iBeaconManager1;
        MonitorNotifier _monitorNotifier1;
        RangeNotifier _rangeNotifier1;
        Region _monitoringRegion1;
        Region _rangingRegion1;
        TextView _text;

        int _previousProximity;

        public MainActivity()
        {
            _iBeaconManager = IBeaconManager.GetInstanceForApplication(this);

            _monitorNotifier = new MonitorNotifier();
            _rangeNotifier = new RangeNotifier();

            _monitoringRegion = new Region(monkeyId, UUID, Integer.ValueOf(major), Integer.ValueOf(minor));
            _rangingRegion = new Region(monkeyId, UUID, Integer.ValueOf(major), Integer.ValueOf(minor));

            _iBeaconManager1 = IBeaconManager.GetInstanceForApplication(this);
            _monitorNotifier1 = new MonitorNotifier();
            _rangeNotifier1 = new RangeNotifier();

            _monitoringRegion1 = new Region(monkeyId1, UUID1, Integer.ValueOf(major1), Integer.ValueOf(minor1));
            _rangingRegion1 = new Region(monkeyId1, UUID1, Integer.ValueOf(major1), Integer.ValueOf(minor1));
        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            _view = FindViewById<RelativeLayout>(Resource.Id.findTheMonkeyView);
            _text = FindViewById<TextView>(Resource.Id.monkeyStatusLabel);

            _iBeaconManager.Bind(this);

            _monitorNotifier.EnterRegionComplete += EnteredRegion;
            _monitorNotifier.ExitRegionComplete += ExitedRegion;

            _rangeNotifier.DidRangeBeaconsInRegionComplete += RangingBeaconsInRegion;

            _iBeaconManager1.Bind(this);

            _monitorNotifier1.EnterRegionComplete += EnteredRegion1;
            _monitorNotifier1.ExitRegionComplete += ExitedRegion1;

            _rangeNotifier1.DidRangeBeaconsInRegionComplete += RangingBeaconsInRegion;
        }

        protected override void OnResume()
        {

            _manager = (BluetoothManager)Application.Context.GetSystemService(BluetoothService);
            _manager.Adapter.Enable();

            base.OnResume();
            _paused = true;
        }

        protected override void OnPause()
        {
            base.OnPause();
            _paused = true;
        }

        void EnteredRegion(object sender, MonitorEventArgs e)
        {
            if(_paused)
            {
                ShowNotification();
            }
        }

        void ExitedRegion(object sender, MonitorEventArgs e)
        {
            if (_paused)
            {   
                ShowNotification1();
                UpdateDisplay("There are no beacons around you.!", Color.Black);
            }
        }

        void EnteredRegion1(object sender, MonitorEventArgs e)
        {
            if (_paused)
            {
                ShowNotification2();
            }
        }

        void ExitedRegion1(object sender, MonitorEventArgs e)
        {
            if (_paused)
            {
                ShowNotification3();
                UpdateDisplay("There are no beacons around you.!", Color.Black);
            }
        }

        void RangingBeaconsInRegion(object sender, RangeEventArgs e)
        {
            if (e.Beacons.Count > 0)
            {
                var beacon = e.Beacons.FirstOrDefault();
                var message = string.Empty;

                switch((ProximityType)beacon.Proximity)
                {
                    case ProximityType.Immediate:
                        UpdateDisplay("You found the Estimote!", Color.Green);
                        break;
                    case ProximityType.Near:
                        UpdateDisplay("You're getting warmer", Color.Yellow);
                        break;
                    case ProximityType.Far:
                        UpdateDisplay("You're freezing cold", Color.Blue);
                        break;
                    case ProximityType.Unknown:
                        UpdateDisplay("I'm not sure how close you are to the Estimote", Color.Red);
                        break;
                }

                _previousProximity = beacon.Proximity;
            }
        }

        #region IBeaconConsumer impl
        public void OnIBeaconServiceConnect()
        {

            _iBeaconManager1.SetMonitorNotifier(_monitorNotifier1);
            _iBeaconManager1.SetRangeNotifier(_rangeNotifier1);
            _iBeaconManager1.StartMonitoringBeaconsInRegion(_monitoringRegion1);
            _iBeaconManager1.StartRangingBeaconsInRegion(_rangingRegion1);

            _iBeaconManager.SetMonitorNotifier(_monitorNotifier);
            _iBeaconManager.SetRangeNotifier(_rangeNotifier);
            _iBeaconManager.StartMonitoringBeaconsInRegion(_monitoringRegion);
            _iBeaconManager.StartRangingBeaconsInRegion(_rangingRegion);
        }
        #endregion

        private void UpdateDisplay(string message, Color color)
        {
            RunOnUiThread(() =>
            {
                _text.Text = message;
                _view.SetBackgroundColor(color);
            });
        }

        private void ShowNotification()
        {
            var resultIntent = new Intent(this, typeof(MainActivity));
            resultIntent.AddFlags(ActivityFlags.ReorderToFront);
            var pendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.UpdateCurrent);
            var notificationId = Resource.String.monkey_notification;

            var builder = new Notification.Builder(this)
                .SetSmallIcon(Resource.Drawable.Xamarin_Icon)
                .SetContentTitle(this.GetText(Resource.String.app_label))
                .SetContentText(this.GetText(Resource.String.monkey_notification))
                .SetContentIntent(pendingIntent)
                .SetAutoCancel(true);

            var notification = builder.Build();

            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.Notify(notificationId, notification);
        }

        private void ShowNotification2()
        {
            var resultIntent = new Intent(this, typeof(MainActivity));
            resultIntent.AddFlags(ActivityFlags.ReorderToFront);
            var pendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.UpdateCurrent);
            var notificationId = Resource.String.monkey_notification2;

            var builder = new Notification.Builder(this)
                .SetSmallIcon(Resource.Drawable.Xamarin_Icon)
                .SetContentTitle(this.GetText(Resource.String.app_label))
                .SetContentText(this.GetText(Resource.String.monkey_notification2))
                .SetContentIntent(pendingIntent)
                .SetAutoCancel(true);

            var notification = builder.Build();

            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.Notify(notificationId, notification);
        }

        private void ShowNotification1()
        {
            var resultIntent = new Intent(this, typeof(MainActivity));
            resultIntent.AddFlags(ActivityFlags.ReorderToFront);
            var pendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.UpdateCurrent);
            var notificationId = Resource.String.monkey_notification1;

            var builder = new Notification.Builder(this)
                .SetSmallIcon(Resource.Drawable.Xamarin_Icon)
                .SetContentTitle(this.GetText(Resource.String.app_label))
                .SetContentText(this.GetText(Resource.String.monkey_notification1))
                .SetContentIntent(pendingIntent)
                .SetAutoCancel(true);

            var notification = builder.Build();

            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.Notify(notificationId, notification);
        }

        private void ShowNotification3()
        {
            var resultIntent = new Intent(this, typeof(MainActivity));
            resultIntent.AddFlags(ActivityFlags.ReorderToFront);
            var pendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.UpdateCurrent);
            var notificationId = Resource.String.monkey_notification3;

            var builder = new Notification.Builder(this)
                .SetSmallIcon(Resource.Drawable.Xamarin_Icon)
                .SetContentTitle(this.GetText(Resource.String.app_label))
                .SetContentText(this.GetText(Resource.String.monkey_notification3))
                .SetContentIntent(pendingIntent)
                .SetAutoCancel(true);

            var notification = builder.Build();

            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.Notify(notificationId, notification);
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();

            _monitorNotifier.EnterRegionComplete -= EnteredRegion;
            _monitorNotifier.ExitRegionComplete -= ExitedRegion;

            _rangeNotifier.DidRangeBeaconsInRegionComplete -= RangingBeaconsInRegion;

            _iBeaconManager.StopMonitoringBeaconsInRegion(_monitoringRegion);
            _iBeaconManager.StopRangingBeaconsInRegion(_rangingRegion);

            _iBeaconManager.UnBind(this);

            _monitorNotifier1.EnterRegionComplete -= EnteredRegion1;
            _monitorNotifier.ExitRegionComplete -= ExitedRegion1;

            _rangeNotifier1.DidRangeBeaconsInRegionComplete -= RangingBeaconsInRegion;

            _iBeaconManager1.StopMonitoringBeaconsInRegion(_monitoringRegion1);
            _iBeaconManager1.StopRangingBeaconsInRegion(_rangingRegion1);

            _iBeaconManager1.UnBind(this);

            _manager.Adapter.Disable();
        }
    }
}

【问题讨论】:

    标签: c# android xamarin.android ibeacon-android estimote


    【解决方案1】:

    通过创建多个区域,可以轻松地扩充代码以检测多个信标 UUID。

    第 1 步。开始测距除您拥有的区域之外的其他两个区域

    _iBeaconManager.StartRangingBeaconsInRegion(_rangingRegion);
    _iBeaconManager.StartRangingBeaconsInRegion(new Region("uuid2", UUID2, null, null));
    _iBeaconManager.StartRangingBeaconsInRegion(new Region("uuid3", UUID3, null, null));
    

    可以对代码进行许多其他更改,以同时监视这些区域,在活动关闭时停止对它们进行测距,以查找在每个单独区域中检测到的多个信标。但是显示的更改将完成您需要的基本功能。

    【讨论】:

    • 是的..这也有效..但是当用户到达信标的范围时,您是否知道为不同的信标触发不同的通知。?
    • 我的意思是,如果用户进入第一个信标范围,则应该触发不同的通知..与他到达第二个信标范围时相同..如何区分信标并触发通知..?
    • RangeEventArgs 有一个 Region 属性,您可以检查您查看触发的多个区域中的哪一个。
    • 是的..但我不知道由于某种原因我将 Region 设为 null。
    • 不确定为什么 region 为空,但您也可以检查 Beacon 对象的属性以查看第一个标识符(您的 UUID)并基于此执行条件逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多