0

我只是想让我的多平台应用程序在跨平台 xamarin 解决方案中找到 ibeacons 的一个非常简单的 POC。我已经完成了 android 方面的工作,但只是遇到了 iOS 方面的问题。

我在 AppDelegate 类中有以下破解代码(这只是为了首先解决它,我意识到这不是它应该驻留的地方):

[Register("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    UIWindow window;

    static readonly string uuid = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
    static readonly string monkeyId = "Monkey";

    CBPeripheralManager peripheralMgr;
    BTPeripheralDelegate peripheralDelegate;
    CLLocationManager locationMgr;

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        window = new UIWindow(UIScreen.MainScreen.Bounds);

        myAppManagerApp.Init(typeof(myAppManagerApp).Assembly);
        Forms.Init();
       // FormsMaps.Init();            

        UINavigationBar.Appearance.BackgroundColor = UIColor.FromRGBA(0, 0, 0, 0);
        UINavigationBar.Appearance.TintColor = UIColor.Blue;          
        UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes()
        {
            TextColor = UIColor.White
        });

        window.RootViewController = BuildView();
        window.MakeKeyAndVisible();

        var monkeyUUID = new NSUuid(uuid);
        var beaconRegion = new CLBeaconRegion(monkeyUUID, monkeyId);
        //power - the received signal strength indicator (RSSI) value (measured in decibels) of the beacon from one meter away
        var power = new NSNumber(-59);
        NSMutableDictionary peripheralData = beaconRegion.GetPeripheralData(power);
        peripheralDelegate = new BTPeripheralDelegate();
        peripheralMgr = new CBPeripheralManager(peripheralDelegate, DispatchQueue.DefaultGlobalQueue);
        peripheralMgr.StartAdvertising(peripheralData);

        locationMgr = new CLLocationManager();
        locationMgr.RegionEntered += (object sender, CLRegionEventArgs e) =>
        {
            if (e.Region.Identifier == monkeyId)
            {
                var notification = new UILocalNotification() { AlertBody = "There's a monkey hiding nearby!" };
                UIApplication.SharedApplication.PresentLocationNotificationNow(notification);
            }
        };

        locationMgr.DidStartMonitoringForRegion += locationMgr_DidStartMonitoringForRegion;
        locationMgr.MonitoringFailed += locationMgr_MonitoringFailed;

        locationMgr.StartMonitoring(beaconRegion);
        locationMgr.StartRangingBeacons(beaconRegion);

        locationMgr.DidRangeBeacons +=locationMgr_DidRangeBeacons;

        return true;
    }

    private void locationMgr_DidRangeBeacons(object sender, CLRegionBeaconsRangedEventArgs e)
    {
            throw new NotImplementedException();
    }

    private void locationMgr_MonitoringFailed(object sender, CLRegionErrorEventArgs e)
    {
        throw new NotImplementedException();
    }

    private void locationMgr_DidStartMonitoringForRegion(object sender, CLRegionEventArgs e)
    {
        int i = 0;
        //throw new NotImplementedException();
    }

    static UIViewController BuildView()
    {
        var root = new pgeRoot();
        var controller = root.CreateViewController();
        return controller;
    }

我已经从 Find the monkey 示例中删除了大部分代码。无论哪种方式,DidRangeBeacons 或 RegionEntered 事件都不会触发。我正在使用 estimote iBeacons,所以我不知道这是否有区别?

关于我在这里缺少什么的任何想法?我需要将权限或设置放入 plist 吗?

谢谢

4

1 回答 1

2

在 iOS 8 中,您需要明确请求使用定位服务的权限 - 这是CLLocationManager's RequestAlwaysAuthorization(用于监控)和RequestWhenInUseAuthorization(用于测距)方法。您还需要在 iOS 应用程序的 Info.plist 文件中有一个适当的 (NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription) 条目,尽管我不完全确定如何在 Xamarin 中执行此操作。

于 2014-10-21T07:48:12.670 回答