Create your first skill

After you build your first app, you can start building skills. You can use the various system services of Cruzr in skills to customize the functions of the robot.

The following example will implement a simple time-telling skill. The function of the skill is to broadcast the current time when triggered.

Configure services

In addition to adding library dependencies and configuration permissions (see build your first app) to develop skills, you also need to configure skill services separately for the system startup skill.

Open the file app/src/main/AndroidManifest.xml and add Bootstrap service in the <application> tag.

<application>
    <service
        android:name="com.ubtrobot.app.Bootstrap"
        android:exported="true" />
</application>

Create skills

Create a TimerSkillclass in the directory of the module package of the previously created robot application, and extends RobotSkill.

public class TimerSkill extends RobotSkill {

}

At the same time, create a robot_manifest.xmlfile in the app/src/main/res/xml/folder and add the following content.

<manifest>
    <skill class="com.ubtrobot.demo.TimerSkill" name="timer">
    </skill>
</manifest>

A skill named timeris configured in the above code, and its path is com.ubtrobot.demo.TimerSkill.

Add command receiving method

After creating a skill, you need to add command receiving method in the skill class to receive and process the commands. Add the timeIndicate method here, use the @OnDirective annotation, and configure an action attribute for it

public class TimerSkill extends RobotSkill {

    @OnDirective(action = "timer.indicator")
    public void timeIndicate(Directive directive) {

    }
}

Add the <directive /> command tag in the <skill /> tag of robot_manifest.xml, where the action needs to be the same as the action in the code above.

<manifest>
    <skill class="com.ubtrobot.demo.TimerSkill" name="timer">
        <directive action="timer.indicator" />
    </skill>
</manifest>

Implement the time-telling function

Get the current time using the timeIndicate method and use the voice service to broadcast the time.

public class TimerSkill extends RobotSkill {

    @OnDirective(action = "timer.indicator")
    public void timeIndicate(Directive directive) {
        //Get the current time 
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HHmm");
        String timeStr = "Now is:" + simpleDateFormat.format(new Date());

        //use the voice service to broadcast the time
        SpeechManager speechManager = getSystemService(SpeechManager.SERVICE);
        speechManager.synthesize(timeStr);
    }
}

Simulate command sending

The skill has been completed, and the robot is now able to tell the time. So how do you use this function? Next we will simulate an command and send it to skill to start it.

Add the following code in MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //send command
        Robot.globalContext().dispatchDirective("timer.indicator");
    }
}

Install and run the app and you will hear the robot broadcast the time for you.

The above process demonstrated how to implement a simple skill. Skill still has many other functions. For details, please refer to skill.

More services

Above, we used the voice service to broadcast the time. We can also add other effects for time-telling, such as:

Add lighting effects using lighting services

LightManager lightManager = getSystemService(LightManager.SERVICE);
lightManager.displayEffect(lightId, effectUri);

Use the emotion service to display emojis

EmotionManager emotionManager = getSystemService(EmotionManager.SERVICE);
emotionManager.express(emotionUri);

Use the servo service to add actions

ServoManager servoManager = getSystemService(ServoManager.SERVICE);
servoManager.rotate(rotationOption);

In order to implement a robot skill, a lot of basic functions of the robot need to be called. Cruzr has many system services. You can use these services to implement various special function. For details, please see the System Services chapter.