获取Kontakt iBeacon的房产

时间:2015-04-17 08:26:15

标签: ios objective-c ibeacon

我正在开发iBeacon应用,我希望得到Kontakt iBeacons的所有属性。

在stackoverflow中有2个或3个主题,但没有人无法回答这个问题。

我的应用是一个全球iBeacon项目。我可以看到Estimote iBeacons的所有属性也可以更改majorminor值。我使用Estimote SDK中的连接方法完成了此操作但是我无法连接Kontakt iBeacons。我刚刚使用major访问了minorrssiKontakt SDK值。

我有API key来连接这些iBeacons,但我无法连接。我只是想学习访问这些值的方法。任何人都可以告诉你的方式吗?

谢谢你,Halil。

2 个答案:

答案 0 :(得分:2)

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;
}

答案 1 :(得分:0)

Johny Kumar提供非常有帮助但又丑陋且过时的回复。不知怎的,他的代码是我能找到的唯一使用KontaktSDK API的例子,所以要更多地了解KontaktSDK API ......

这是Johny Kumar对新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)")
                }
            }
        }
    }
}

如果您想获得信标(或设备)的相关操作

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)
            }
        }
    }
}

Kontakt没有提供任何如何执行此操作的示例,这非常可怕,因此,如果您从Kontakt中阅读此内容,请将其放在示例文档中。