Xamarin.Android中的多个Estimote / Beacons

时间:2017-05-24 05:42:41

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

我对这个信标概念很陌生。为了演示目的,我使用此示例应用程序 Android iBeacon App配置了我的一个beacon / estimote。

这里应用程序一次能够找到单个信标。我的意思是我必须传递单个信标的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: 我尝试了类似这样的操作,当用户进入信标的不同区域时触发通知..但每次同一通知触发两个信标时..i表示每次在用户进入区域时调用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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

通过制作多个区域,可以轻松扩充显示的代码以检测多个信标UUID。

步骤1.除了您拥有的区域外,开始测量其他两个区域

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

可以对代码进行大量其他更改以监控这些区域,在活动关闭时停止对其进行测距,以查找在每个区域内检测到的多个信标。但所显示的更改将完成您所需的基础知识。