Xamarin.Android中的多个Estimote/Beacons [英] Multiple Estimote/Beacons in Xamarin.Android

查看:76
本文介绍了Xamarin.Android中的多个Estimote/Beacons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 .

这里应用程序可以一次找到单个信标.我的意思是我必须传递单个信标的UUID才能确定它是否在信标范围内.多个信标使用同一应用程序?? 我的意思是,如果用户输入特定的信标范围,则用户应获得有关特定信标的通知.因此,有可能添加多个信标吗?.我必须根据信标的UUID区分信标的范围.任何人都可以指导任何好的教程或如何对信标进行修改这.任何帮助将不胜感激..

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..

这是我的 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);
        }
    }
}


我已经尝试过类似的操作,以在用户进入信标的不同区域时触发通知.但是,每次两个信标都触发相同的通知时, ..i表示,如果用户进入该区域,则每次都会调用ShowNotification();如果用户离开该区域,则每次都会调用ShowNotification1().

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

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

推荐答案

很容易通过显示多个区域来扩展显示的代码以检测多个信标UUID.

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

第1步.除了对您拥有的区域之外,还开始对其他两个区域进行测距

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.

这篇关于Xamarin.Android中的多个Estimote/Beacons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆