H.P.
H.P.

Reputation: 113

Get properties of Kontakt iBeacon

I'm developing an iBeacon app and I want to get all properties of Kontakt iBeacons.

There are 2 or 3 topic about that in stackoverflow but nobody couldn't answer this.

My app is a global iBeacon project. I can see all properties of Estimote iBeacons also can change major,minor values. I did this with using connect method in Estimote SDK however I couldn't connect Kontakt iBeacons. I just accessed major,minor and rssi values with Kontakt SDK.

I have an API key to connect these iBeacons but I couldn't connect. I just want to learn way of accessing these values. Can anybody tell the way of this ?

Thank you, Halil.

Upvotes: 1

Views: 979

Answers (2)

David Rees
David Rees

Reputation: 7332

Johny Kumar provides very helpful but ugly and outdated response. Somehow his code is the only example I can find of using the KontaktSDK API, so to shed some more light on the KontaktSDK API...

This is the swift version of Johny Kumar's answer with the new API

import KontaktSDK

func beaconManager(manager: KTKBeaconManager, didRangeBeacons beacons: [CLBeacon], inRegion region: KTKBeaconRegion) {
    print("Ranged beacons count: \(beacons.count)")

    for beacon in beacons {
        print(beacon)

        let uuid = beacon.proximityUUID.UUIDString
        let major = beacon.major
        let minor = beacon.minor
        let parameters: [String: AnyObject] = ["proximity":uuid, "major" : major, "minor": minor]

        KTKCloudClient.sharedInstance().getObjects(KTKDevice.self, parameters: parameters){
            response, error in
            guard let objects = response?.objects else {return}

            for object in objects{
                if let device = object as? KTKDevice {
                    print("device: \(device)")
                }
            }
        }
    }
}

If you would like to get the associated actions for a beacon (or device)

func fetchActions(device: KTKDevice) {
    let params = ["uniqueId":device.uniqueID]

    KTKCloudClient.sharedInstance().getObjects(KTKAction.self, parameters: params) { response ,error in
        guard let objects = response?.objects else {return}
        for object in objects{
            if let action = object as? KTKAction {
                print(action.url)
            }
        }
    }
}

It's pretty terrible that Kontakt doesnt provide any example of how to do this, so if you're reading this from Kontakt please put this in your example document.

Upvotes: 1

johny kumar
johny kumar

Reputation: 1270

client=[KTKClient new];
client.apiKey=@"apikey";
locationManager=[KTKLocationManager new];
locationManager.delegate=self;
NSError *error;
NSArray *array=[client getRegionsError:&error];

[locationManager setRegions:array];
//[locationManager stopMonitoringBeacons];
[locationManager startMonitoringBeacons];
beaconManager = [KTKBeaconManager new];
beaconManager.delegate = self;
[beaconManager startFindingDevices];

- (void)locationManager:(KTKLocationManager *)locationManager didRangeBeacons:(NSArray *)beacons{

    for (CLBeacon *beacon in beacons) {
    KTKBeacon *beaconData = [self _getDataForBeacon:beacon];
    if (beaconData) beaconsData[beacon] = beaconData;
    }
}

-(KTKBeacon *)_getDataForBeacon:(CLBeacon *)beacon
{
NSString *strURL = [NSString stringWithFormat:@"https://api.kontakt.io/beacon?proximity=%@&major=%@&minor=%@",
       [beacon.proximityUUID UUIDString],beacon.major,beacon.minor];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL]];
[request setValue:@"apikey" forHTTPHeaderField:@"Api-Key"];
[request setValue:@"application/vnd.com.kontakt+json; version=2" forHTTPHeaderField:@"Accept"];
NSError *error;
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSError * error1=nil;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error1];

 KTKBeacon *ktkBeacon = [[KTKBeacon alloc] initWithDictionary:dic];

NSLog(@"%@",dic);
return ktkBeacon;
}

Upvotes: 2

Related Questions