Register Things

By registering a thing, it will be acknowledged by Kii Cloud.

Upon the thing registration, you can set various attributes as the the thing information field values (See Thing Management for the explanation of each field).

The following sample code blocks are examples of registering a thing with its vendorThingID "rBnvSPOXBDF9r29GJeGS", its type "sensor", its vendor name "Kii" and the password "123ABC".

Android

  • // Set thing information.
    String thingType = "sensor";
    ThingFields thingFields = new ThingFields();
    thingFields.setVendor("Kii");
    
    try {
      // Register the thing.
      KiiThing thing = KiiThing.register("rBnvSPOXBDF9r29GJeGS", "123ABC", thingType, thingFields);
    
      // Resister the current user as the thing owner.
      thing.registerOwner(KiiUser.getCurrentUser());
    } catch (AppException e) {
      // Handle the error.
    } catch (IOException e) {
      // Handle the error.
    }
  • // Set thing information.
    String thingType = "sensor";
    ThingFields thingFields = new ThingFields();
    thingFields.setVendor("Kii");
    
    // Register the thing.
    KiiThing.register("rBnvSPOXBDF9r29GJeGS", "123ABC", thingType, thingFields, new KiiCallback<KiiThing>() {
      @Override
      public void onComplete(KiiThing result, Exception e) {
        if (e != null) {
          // Handle the error.
          return;
        }
    
        // Resister the current user as the thing owner.
        result.registerOwner(KiiUser.getCurrentUser(), new KiiCallback<KiiThingOwner>() {
          @Override
          public void onComplete(KiiThingOwner result, Exception e) {
            if (e != null) {
              // Handle the error.
              return;
            }
          }
        });
      }
    });

iOS

Swift 3:

  • // Set thing information.
    let thingType = "sensor"
    let thingFields  = KiiThingFields()
    thingFields.vendor = "Kii"
    
    let thing : KiiThing
    do{
      // Register the thing.
      thing = try KiiThing.registerSynchronous(
        "rBnvSPOXBDF9r29GJeGS",
        password: "123ABC",
        type: thingType,
        fields: thingFields)
    
      // Resister the current user as the thing owner.
      try thing.registerOwnerSynchronous(KiiUser.current()!)
    }catch(let error as NSError){
      // Handle the error.
      return
    }
  • // Set thing information.
    let thingType = "sensor"
    let thingFields  = KiiThingFields()
    thingFields.vendor = "Kii"
    
    // Register the thing.
    KiiThing.register(
      "rBnvSPOXBDF9r29GJeGS",
      password: "123ABC",
      type: thingType,
      fields: thingFields) { (thing , error) -> Void in
        if error != nil {
          // Handle the error.
          return
        }
    
        // Resister the current user as the thing owner.
        thing?.register(KiiUser.current()!, block: { (thing , error) -> Void in
          if error != nil {
            // Handle the error.
            return
          }
        })
    }

Objective-C:

  • // Set thing information.
    NSString* thingType = @"sensor";
    KiiThingFields* thingFields = [[KiiThingFields alloc] init];
    thingFields.vendor = @"Kii";
    NSError* error = nil;
    
    // Register the thing.
    KiiThing* thing = [KiiThing registerThingSynchronous:@"rBnvSPOXBDF9r29GJeGS"
                                                password:@"123ABC"
                                                    type:thingType
                                                  fields:thingFields
                                                   error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
    
    // Resister the current user as the thing owner.
    [thing registerOwnerSynchronous:[KiiUser currentUser]
                              error:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Set thing information.
    NSString* thingType = @"sensor";
    KiiThingFields* thingFields = [[KiiThingFields alloc] init];
    thingFields.vendor = @"Kii";
    
    // Register the thing.
    [KiiThing registerThing:@"rBnvSPOXBDF9r29GJeGS"
                   password:@"123ABC"
                       type:thingType
                     fields:thingFields
                      block:^(KiiThing *thing, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    
      // Resister the current user as the thing owner.
      [thing registerOwner:[KiiUser currentUser]
                     block:^(KiiThing *thing, NSError *error) {
        if (error != nil) {
          // Handle the error.
          return;
        }
      }];
    }];

JavaScript

// Set thing information.
var thingFields = {
  _vendorThingID: "rBnvSPOXBDF9r29GJeGS",
  _password: "123ABC",
  _thingType: "sensor",
  _vendor: "Kii"
};

// Register the thing.
KiiThing.register(thingFields, {
  success: function(thing) {
    // Get the current user.
    var user = KiiUser.getCurrentUser();

    // Register the current user as the thing owner.
    thing.registerOwner(user, {
      success: function(thing, user) {
        // Do something.
      },
      failure: function(error) {
        // Handle the error.
      }
    });
  },
  failure: function(error) {
    // Handle the error.
  }
});

The basic steps are as follows:

  1. Set the thing attributes in the thing information fields as needed (See here to see how you can set the attributes).
  2. Register the thing with the vendorThingID and the password.
  3. Register the current user as the thing owner. See Registering Owners for more details on the thing owner registration.

Hint for Creating a Thing List

If you need to get a list of things, you need to implement the feature in your application. For example, your application can use a dedicated application scope bucket to store information of all things. See Hint for creating a user list (Android) to learn how you can implement it.