Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

[Mobile App] Xamarin iBeacon

Avevamo già adottato Xamarin per creare applicazioni per smartphone, apprezzando molto Xamarin.Forms che permette ti sviluppare un applicazione cross-platform quasi totalmente.
Abbiamo verificato che effettivamente il riutilizzo del codice è veramente alto, siamo riusciti a raggiungere una percentuale del 90% realizzando due applicazioni (iOS e Android) con un effort soltanto.

Inoltre abbiamo realizzato un applicazione per iOS e Android che integra gli iBeacon (BLE Bluetooth Low Enery) per abilitare le notifiche geo-localizzate, tutta questa parte è stata gestita utilizzando due librerie diverse per iOS e Android.

iOS

Abbiamo gestito con una funzione tutta la parte di interfacciamento iBeacon


public void startBeacon(){

beaconUUID = new NSUuid (uuid);
beaconRegion = new CLBeaconRegion (beaconUUID,beaconMajor,beaconId);

beaconRegion.NotifyEntryStateOnDisplay = true;
beaconRegion.NotifyOnEntry = true;
beaconRegion.NotifyOnExit = true;

locationmanager = new CLLocationManager ();
locationmanager.RegionEntered += (object sender, CLRegionEventArgs e) => {
Console.WriteLine (“Region Entered”);
};
locationmanager.DidRangeBeacons += (object sender, CLRegionBeaconsRangedEventArgs e) => {

if( e.Beacons.Length > 0 ) {
var beacon = e.Beacons[0];
Console.WriteLine(beacon.Minor.ToString()+” “+ beacon.Rssi);
if(beacon.Proximity==CLProximity.Immediate||beacon.Proximity==CLProximity.Near||beacon.Rssi>(nint)(-76)){

//beacon 1
if(beacon.Minor == (NSNumber)1 && FlagB1 == true)
{
var notification = new UILocalNotification () { AlertBody = “Beacon numero 1” };
notification.SoundName = UILocalNotification.DefaultSoundName;
UIApplication.SharedApplication.CancelAllLocalNotifications();
UIApplication.SharedApplication.PresentLocalNotificationNow(notification);

window.RootViewController = BeaconMainPage.GetMainPage(1).CreateViewController();
window.MakeKeyAndVisible ();
}

}
}

};

if(UIDevice.CurrentDevice.CheckSystemVersion(8,0))
locationmanager.RequestAlwaysAuthorization ();
locationmanager.StartMonitoring (beaconRegion);
locationmanager.StartRangingBeacons (beaconRegion);

}

Info.plist

Per utilizzare le notifiche in background è essenziale impostare il seguente codice:

NSLocationAlwaysUsageDescription
Accetta per avere le informazioni sull'evento

Android

Per la parte Android abbiamo utilizzato le librerie RadiusNetwork disponibili dai componenti di Xamarin, abbiamo verificato che funzionano bene e ci sono piaciute a differenza delle librerie Estimote sulle quali abbiamo avuto deie problemi.


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

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

_monitoringRegion = new Region(beaconId, UUID, (Java.Lang.Integer)101, null);
_rangingRegion = new Region(beaconId, UUID, (Java.Lang.Integer)101, null);

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

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle(“Bluetooth down”);
builder.SetMessage(“Perfavore Abilitare il Bluetooth.”);
builder.SetCancelable(false);
builder.SetPositiveButton(“OK”, delegate {});

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter;

if (!bluetoothAdapter.IsEnabled) {
builder.Show ();
}

_iBeaconManager.Bind(this);

_iBeaconManager.SetMonitorNotifier(_monitorNotifier);
_iBeaconManager.SetRangeNotifier(_rangeNotifier);

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

_rangeNotifier.DidRangeBeaconsInRegionComplete += RangingBeaconsInRegion;

var metrics = Resources.DisplayMetrics;
var widthInDp = (int)(metrics.WidthPixels / Resources.DisplayMetrics.Density);
var heightInDp = (int)(metrics.HeightPixels / Resources.DisplayMetrics.Density);

Xamarin.Forms.Forms.Init(this, bundle);

Console.WriteLine (widthInDp+” “+heightInDp+” “+Resources.DisplayMetrics.Density+” “+Resources.DisplayMetrics.DensityDpi);

SetPage (App.GetMainPage (Resources.DisplayMetrics.Density));

}
void RangingBeaconsInRegion(object sender, RangeEventArgs e)
{
if (e.Beacons.Count > 0)
{

IBeacon beacon = e.Beacons.FirstOrDefault();

if ((ProximityType)beacon.Proximity == ProximityType.Immediate || (ProximityType)beacon.Proximity == ProximityType.Near || beacon.Rssi > -76) {
Console.WriteLine (“vicino ” + beacon.Rssi);
//beacon 1
if (beacon.Minor == 1 ) {
FlagB1 = false;
FlagB2 = true;
FlagB3 = true;
FlagB4 = true;
FlagB5 = true;
FlagB6 = true;
FlagB7 = true;
FlagB8 = true;
FlagB9 = true;
FlagB10 = true;
RunOnUiThread (() => {
SetPage (BeaconMainPage.GetMainPage (1, Resources.DisplayMetrics.Density));
});
ShowNotification (1);
}

}

_previousProximity = beacon.Proximity;
}
}
#region IBeaconConsumer impl
public void OnIBeaconServiceConnect()
{
Console.WriteLine (“Connesso Al beacon”);

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

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

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.SetDefaults((int)(NotificationDefaults.Sound | NotificationDefaults.Vibrate))
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle(this.GetText(Resource.String.app_label))
.SetContentIntent(pendingIntent)
.SetAutoCancel(true);

//beacon1
if (i == 1) {
notificationId = Resource.String.beacon1_notification;
builder.SetContentText(this.GetText(Resource.String.beacon1_notification));
}

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

Grazie a questo codice siamo riusciti ad avere un applicazione funzionale e funzionante per il marketing di prossimità, applicazione presentata ad un evento a Firenze.
Potete trovare l’app NetForYou qui per Android e iOS

The post [Mobile App] Xamarin iBeacon appeared first on KeepUP.



This post first appeared on Web Agency - Web Design - Agenzia SEO - Web Market, please read the originial post: here

Share the post

[Mobile App] Xamarin iBeacon

×

Subscribe to Web Agency - Web Design - Agenzia Seo - Web Market

Get updates delivered right to your inbox!

Thank you for your subscription

×