Get properties of Kontakt iBeacon

2019-02-28 12:26发布

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.

2条回答
Explosion°爆炸
2楼-- · 2019-02-28 12:50
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;
}
查看更多
Melony?
3楼-- · 2019-02-28 13:00

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.

查看更多
登录 后发表回答