Multiple Estimote/Beacons in Xamarin.Android

北慕城南 提交于 2019-12-25 16:54:22

问题


I am very new to this beacons concept. For demo purpose i have configured one of my beacon/estimote using this sample application Android iBeacon App .

Here the application can able to find single beacon at a time.I mean i have to pass the UUID of a single beacon to find whether it is inside the beacon range or not .Is there any possibility to find the multiple beacons using the same application.? I mean if the user enters the particular beacons range, the user should get the notification regarding the particular beacon. So is there any possibility to add the multiple beacon..?Based on UUID's of the beacons i have to differentiate the range of the beacon.Can anyone please guide to any good tutorial or how to make modification to this. Any help would be much appreciated..

Here is my 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: I have tried something like this to trigger the notification when a user enters the different region of a beacon..but every time the same notification is triggering for both of the beacons..i mean everytime the ShowNotification() will be called if the user enters the region, ShowNotification1() will be called if the user exits the region.

How to call different notification for a different beacons when the user enters its region..?

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();
        }
    }
}

回答1:


It's easy to augment the code shown to detect multiple beacon UUIDs by making multiple regions.

Step 1. Start ranging the two other regions in addition to the one you have

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

There are lots of other changes you could make to the code to also monitor these regions, to stop ranging them when the activity is closed, to look for multiple beacons detected within each individual region. But the changes shown will accomplish the basics of what you need.



来源:https://stackoverflow.com/questions/44149877/multiple-estimote-beacons-in-xamarin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!