Thing の登録
Thing を Kii Cloud で使用するには、まず Thing の登録が必要です。登録を行うことで Kii Cloud はこの Thing を認識します。
Thing を登録する際には、属性情報を Thing 情報のフィールド値として設定することができます(Thing に設定可能なフィールドの詳細は Thing 管理 を参照してください)。
ここでは、Thing のベンダーが割り当てた ID "rBnvSPOXBDF9r29GJeGS"、Thing のタイプ "sensor"、Thing のベンダー名 "Kii"、パスワード "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:
-
// 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.
}
});
基本手順は以下のとおりです。
- 必要に応じて属性情報を Thing 情報のフィールド値として設定します。設定方法の詳細は こちら をご参照ください。
- Thing のベンダーが割り当てた ID とパスワードを指定して Thing を作成、登録します。
- ログイン中のユーザーを Thing のオーナーとして登録します。オーナー登録の詳細は オーナーの設定 をご覧ください。
Thing 一覧機能の実装ヒント
Thing 一覧の機能が必要な場合、アプリ側での実装が必要となります。一覧が必要な場合は、アプリケーションスコープに Thing の一覧情報を格納するなどの方法をとれます。実装方法は、ユーザー一覧の実装方法(Android) を参考にしてください。