IOS Phonegap Estimote Beacons Ranging

时间:2014-08-21 12:37:45

标签: ios cordova phonegap-plugins

我正在尝试为Estimote iBeacons创建一个Phonegap插件。 #pragma mark - Beacon Manager delegate methods.区域内未触发任何功能。

getRange函数的调用有效,当我添加标记时,start函数在调试中停止。我还在Beacon manager delegate methods中添加了中断标记,但没有触发它们。

我不知道我做错了什么,我一直在阅读有关7.1的IOS问题,并且已经修复了7.1.2,所以我已经更新了我的iPad mini。

以下是我的代码。有人有任何线索吗?

#import <Cordova/CDV.h>
#import "Beacon.h"
#import "ESTBeaconManager.h"

@interface Beacon () <ESTBeaconManagerDelegate,ESTBeaconDelegate>

@property (nonatomic, strong) ESTBeaconManager* beaconManager;
@property (nonatomic, strong) NSMutableArray *callbacks;
@property (nonatomic, strong) NSMutableArray *watches;
@end


@implementation Beacon

- (Beacon*)pluginInitialize
{
    // region watchers
    self.regionWatchers = [[NSMutableDictionary alloc] init];

    // Create callbacks list
    self.callbacks = [[NSMutableArray alloc]init];
    self.watches = [[NSMutableArray alloc]init];

    // craete manager instance
    self.beaconManager = [[ESTBeaconManager alloc] init];
    self.beaconManager.delegate = self;
    self.beaconManager.avoidUnknownStateBeacons = YES;

    // create sample region object (you can additionaly pass major / minor values)
    NSUUID* uuid = [[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];
    self.currentRegion = [[ESTBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"test"];
    self.currentRegion.notifyEntryStateOnDisplay=YES;


    return self;
}

#pragma mark - Start monitoring methods

- (void)getRange:(CDVInvokedUrlCommand*)command {
    [self addCallback:command.callbackId];
}
- (void)addWatch:(CDVInvokedUrlCommand*)command {

}
- (void)clearWatch:(CDVInvokedUrlCommand*)command {

}

- (void)start {
    [self stop];

    // start ranging
    [self.beaconManager startMonitoringForRegion:self.currentRegion];
    [self.beaconManager startRangingBeaconsInRegion:self.currentRegion];
}

- (void)stop {
    // stop existing discovery/ranging
    [self.beaconManager stopEstimoteBeaconDiscovery];
    [self.beaconManager stopRangingBeaconsInRegion:self.currentRegion];
}

- (void)addCallback:(NSString*)callbackId {
    [self.callbacks addObject:callbackId];
    if( [self size] == 1){
        [self start];
    }
}
- (NSUInteger)size {
    return [self.callbacks count] + [self.watches count];
}
- (void)win {
    // Create output
    NSMutableArray* output = [NSMutableArray array];
    if([self.beacons count] > 0)
    {
        //convert list of beacons to a an array of simple property-value objects
        for (id beacon in self.beacons) {
            [output addObject:[self beaconToDictionary:beacon]];
        }
    }

    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:output];

    // Handle callbacks
    for (NSString *callbackid in self.callbacks) {
        [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackid];
    }

    [self.callbacks removeAllObjects];

    // Handle watches
    [pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
    for (NSString *callbackid in self.watches) {
        [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackid];
    }

    if( [self.watches count] == 0){
        [self stop];
    }
}
#pragma mark - Helpers

- (NSMutableDictionary*)beaconToDictionary:(ESTBeacon*)beacon
{
    NSMutableDictionary* props = [NSMutableDictionary dictionaryWithCapacity:16];
    NSNumber* major = beacon.major;
    NSNumber* minor = beacon.minor;
    NSNumber* rssi = [NSNumber numberWithInt:beacon.rssi];

    if(major == nil) {
        major = beacon.major;
    }
    if(minor == nil) {
        minor = beacon.minor;
    }
    if(rssi == nil) {
        rssi = [NSNumber numberWithInt:beacon.rssi];
    }

    [props setValue:beacon.batteryLevel forKey:@"batteryLevel"];
    [props setValue:beacon.firmwareVersion forKey:@"firmwareVersion"];
    [props setValue:beacon.hardwareVersion forKey:@"hardwareVersion"];
    [props setValue:major forKey:@"major"];
    [props setValue:minor forKey:@"minor"];
    [props setValue:beacon.advInterval forKey:@"advInterval"];
    [props setValue:beacon.description forKey:@"description"];
    [props setValue:rssi forKey:@"rssi"];
    [props setValue:beacon.debugDescription forKey:@"debugDescription"];
    [props setValue:beacon.macAddress forKey:@"macAddress"];
    [props setValue:beacon.measuredPower forKey:@"measuredPower"];
    [props setValue:[NSNumber numberWithBool:beacon.isConnected] forKey:@"isConnected"];

    if(beacon.power != nil) {
        [props setValue:[NSNumber numberWithChar:[beacon.power charValue]] forKey:@"power"];
    }

    if(beacon != nil) {
        [props setValue:beacon.distance forKey:@"accuracy"];
        [props setValue:[NSNumber numberWithInt:beacon.proximity] forKey:@"proximity"];

        if(beacon.proximityUUID != nil) {
            [props setValue:beacon.proximityUUID.UUIDString forKey:@"proximityUUID"];
        }
    }


    return props;
}

#pragma mark - Beacon Manager delegate methods.

- (void)beaconManager:(ESTBeaconManager *)manager
   didDiscoverBeacons:(NSArray *)beacons
             inRegion:(ESTBeaconRegion *)region
{
    self.beacons = beacons;

    [self win];
}

-(void)beaconManager:(ESTBeaconManager *)manager
     didRangeBeacons:(NSArray *)beacons
            inRegion:(ESTBeaconRegion *)region
{
    self.beacons = beacons;
    [self win];
}

- (void)beaconManager:(ESTBeaconManager *)manager didDetermineState:(CLRegionState)state forRegion:(ESTBeaconRegion *)region {
    NSString *result = nil;

    switch(state) {
        case CLRegionStateUnknown:
            result = @"unknown";
            break;
        case CLRegionStateInside:
            result = @"enter";
            break;
        case CLRegionStateOutside:
            result = @"exit";
            break;
        default:
            result = @"unknown";
    }

    NSString* callbackId = [self.regionWatchers objectForKey:region.identifier];

    if(callbackId != nil) {
        NSMutableDictionary* props = [NSMutableDictionary dictionaryWithCapacity:4];

        [props setValue:region.identifier forKey:@"id"];
        [props setValue:region.major forKey:@"major"];
        [props setValue:region.minor forKey:@"minor"];
        [props setValue:result forKey:@"action"];

        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:props];
        [result setKeepCallback:[NSNumber numberWithBool:YES]];

        [self.commandDelegate sendPluginResult:result callbackId:callbackId];
    }
}

@end

1 个答案:

答案 0 :(得分:0)

你是如何打电话给灯塔经理的?那是在同一个文件中吗?

我找到了一个已创建的Estimote Phonegap插件。您应该能够在项目中使用它或创建自己的项目。 https://github.com/divineprog/phonegap-estimotebeacons

我使用该插件使用上面的插件在Angular中创建自己的应用程序。

此外,Estimote发布了一个新的SDK版本,因此方法已经改变