kontaktio / kontakt-ios-sdk

This SDK makes it easy for you to configure, monitor, and securely update Kontakt.io Beacons through our Kontakt.io API.
http://developer.kontakt.io
Other
48 stars 9 forks source link

Reading Configuration returns CBErrorDomain 5 #23

Closed LateralSolutions closed 8 years ago

LateralSolutions commented 8 years ago

Hi, I can't seem to read the beacon configuration using the following code, It always return an NSError 5.

Here is the code, I've tried this with firmware 3.1 and 4.1 with the same results. The admin App is working.

In AppDelegate.swift Kontakt.setAPIKey(Constants.BeaconAPI.kontakt)

Scanning for devices works and in the delegate I'm trying to read the config however it already returns an error

func devicesManager(manager: KTKDevicesManager, didDiscoverDevices devices: [KTKNearbyDevice]?) {

    for device in devices! {
        if (device.name == selectedBeacon?.beaconName) {
            self.kontaktNearBy = device
            kontaktManager!.stopDevicesDiscovery()
            self.tableView.reloadData()

            let connection = KTKDeviceConnection( nearbyDevice: self.kontaktNearBy!)

            connection.connectionTimeout = NSTimeInterval(10)

            var config : KTKDeviceConfiguration  // = KTKDeviceConfiguration()
            var error : NSError

            connection.readConfigurationWithCompletion { config, error in
                if (error == nil) {
                    // OK
                    //self.tableView.reloadData()
                }
            }

        }
    }
}
nick-holmes commented 8 years ago

I'm getting the same issue. Did you manage to resolve it by any chance?

stoprocent commented 8 years ago

Hi Guys

You are right 👍 I made a mistake in a sample code and this is the reason for your issue. KTKDeviceConnection should be class var or it will be deallocated before reading configuration is complete.

Code should be as follows:

var connection: KTKDeviceConnection?

func devicesManager(manager: KTKDevicesManager, didDiscoverDevices devices: [KTKNearbyDevice]?) {

    for device in devices! {
        if (device.name == selectedBeacon?.beaconName) {
            self.kontaktNearBy = device
            kontaktManager!.stopDevicesDiscovery()
            self.tableView.reloadData()

            connection = KTKDeviceConnection( nearbyDevice: self.kontaktNearBy!)

            connection?.connectionTimeout = NSTimeInterval(10)

            var config : KTKDeviceConfiguration  // = KTKDeviceConfiguration()
            var error : NSError

            connection?.readConfigurationWithCompletion { config, error in
                if (error == nil) {
                    // OK
                    //self.tableView.reloadData()
                }
            }

        }
    }
}

Please note that next update of the SDK 1.2.1 will allow you to use KTKDeviceConnection without defining class var so exactly like you are trying to use it now.

LateralSolutions commented 8 years ago

Thanks for the update, I moved the KTKDeviceConnection scope to class level as you suggested and the error is now nil on the readConfigurationWithCompletion {} block

However the config is also returned nil, I tried moving that also to the class scope with no luck.

adrianz commented 8 years ago

@LateralSolutions I guess you have beacons with firmware 4.0 or newer. If that's the case it explains everything – Kontakt.io SDK currently can't properly read configuration of such beacons. It will get a current config from a beacon, but it doesn't have a way to decrypt it, that's why an error is nil, same as the configuration.

As far as I know this should be fixed one of the future versions.

LateralSolutions commented 8 years ago

OK, I will wait for a future fix. Testing on a 3.1 firmware results in the original error, FW 4.0, Error Nil

stoprocent commented 8 years ago

Please not this is fixed in 1.2.1 release. Closing issue for now :)