Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Beacon and Dependency

$
0
0

Hello!

I have been using the AltBeacon library to find some beacons I just bought. I have largely been re-using the code of @chrisriesgo (many thanks to him by the way).
What I am trying to pull here is a little bit different from what he does however. I do not want to call the service on the main page, but when I navigate to a certain ContentPage. But when I launch my app and the service, the app crashes directly and I get an Unhandled Exception.
Here is my piece of Dependency code :

`[assembly: Xamarin.Forms.Dependency(typeof(BeaconClassDroid))]

namespace Mapstest.Droid
{

public class BeaconClassDroid : Java.Lang.Object, BeaconInterface
{

    public event EventHandler<ListChangedEventArgs> ListChanged;
    BeaconManager _iBeaconManager;
    RangeNotifier _rangeNotifier;
    Region _rangingRegion;
    Context context;
    List<Beacon> beacons;

    public BeaconClassDroid ()
    {
        beacons = new List<Beacon> ();

        context = Forms.Context;
        _rangeNotifier = new RangeNotifier ();


    }

    public BeaconManager BeaconManagerImpl
    {
        get
        {
            if (_iBeaconManager == null)
            {
                _iBeaconManager = InitializeBeaconManager();
            }
            return _iBeaconManager;
        }
    }

    public void InitializeService()
    {
        _iBeaconManager = InitializeBeaconManager();
    }

    private BeaconManager InitializeBeaconManager()
    {
        BeaconManager bm = BeaconManager.GetInstanceForApplication(Forms.Context);
        bm.BeaconParsers.Add(new BeaconParser().SetBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));

        _rangingRegion = new Region("HelloBeacon", null, null, null);
        _rangeNotifier.DidRangeBeaconsInRegionComplete += RangingBeaconsInRegion;
        bm.Bind((IBeaconConsumer)Forms.Context);
        return bm;

    }


    public void StartRanging()
    {
        BeaconManagerImpl.SetForegroundBetweenScanPeriod(5000); // 5000 milliseconds

        BeaconManagerImpl.SetRangeNotifier(_rangeNotifier);
        _iBeaconManager.StartRangingBeaconsInRegion(_rangingRegion);
    }


    public void RangingBeaconsInRegion(object sender, RangeEventArgs e)
    {
        if (e.Beacons.Count > 0)
        {
            foreach (var b in e.Beacons)
            {
                beacons.Add(b);

            }
        }
    }`

And my Content Page code :

`public class AboutPage : ContentPage
{
public List Data { get; set; }

    public event EventHandler ListChanged;

    public AboutPage ()
    {
        Title = "About";
        Icon = "about.png";
        DependencyService.Get<BeaconInterface>().StartRanging();

        var button = new Button
        {
            Text = "Testing",
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        var stop = new Button
        {
            Text = "Stop",
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand
        };


    bool timerStatus = false;

        button.Clicked += delegate
        {
            timerStatus = true;
            Device.StartTimer (new TimeSpan (0,0,6), () =>{
                if (timerStatus)
                {
                    DependencyService.Get<BeaconInterface>().InitializeService();
                    /*if (Data.Count != 0)
                    {
                        moi.Text = Data[0].Id;
                    }
                    else
                    {
                        moi.Text = "You failed!";
                    }*/
                }
                return timerStatus;
            });
        };

        stop.Clicked += delegate
        {
            timerStatus = false;
        };`

I am only trying to locate the beacons in the room, not monitor them.
Also, my use permissions are correct (I think) :

<uses-permission android:name="android.permission.BLUETOOTH"/>
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
  <uses-feature android:name="android.hardware.bluetooth" android:required="true"/>

I would really appreciate any kind of help, I am really drowning :/
Thank you!


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>