2017-05-24 8 views
0

私はこのビーコンのコンセプトにはとても新しいです。デモの目的のために、このサンプルアプリケーション Android iBeacon Appを使用して私のビーコン/推定値の1つを設定しました。Xamarin.Androidの複数の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 ..ですいただければ幸いですユーザがビーコンの異なる領域に入るときに、同じビーコンの両方に対して同じ通知がトリガされるたびにnユーザーがリージョンに入ると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

複数の領域を作成することで、複数のビーコンUUIDを検出するように示されているコードを簡単に拡張できます。 2つの他の地域の範囲

ステップ1.あなたは

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

を持っているものに加えて、あなたまで停止すること、また、これらの領域を監視するために、コードを作ることができる他の多くの変更があります。それらのアクティビティが閉じられたときに、個々の領域内で検出された複数のビーコンを探す。しかし、表示される変更は、必要なものの基本を達成します。

+0

はい。これも同様です。しかし、ユーザーがビーコンの範囲に達すると、別のビーコンのさまざまな通知をトリガーする考えはありますか? –

+0

つまり、ユーザーが第2のビーコン範囲に到達したときと同じように、異なる通知がトリガーすべき第1のビーコン範囲に入ると、ビーコンを区別して通知をトリガーすることができます。 –

+0

RangeEventArgsにはRegionプロパティがあり、トリガーされた複数のリージョンのどれを調べるかを調べることができます。 – davidgyoung

関連する問題