Build your first robot application
Prepare the development environment
Developing robot applications based on Cruzr is the same as developing ordinary Android applications. They both use Android Studio IDE for development. Please go to Android's official website to download and install Android Studio.
Create a project
Open the installed Android Studio, and refer to Android's official guide to create an Android application project.
Add library dependencies
Add a declaration of the shared library.
Open the file app/src/main/AndroidManifest.xmland add a declaration of using the Cruzr shared library inside the <application>tag.
<?xml version="1.0" encoding="utf-8"?> <manifest> <application> <uses-library android:name="com.ubtrobot.rosa" android:required="true"/> </application> </manifest>
Introduce the SDK package
Get the Cruzr SDK (cruzr.jar), and creat the cruzr-libs directory in theappdirectory ,put the jar package in the cruzr-libsdirectory.
Open fileapp/build.gradle and add the Cruzr library dependencies in the dependencies.
allprojects { repositories { flatDir { dirs 'cruzr-libs' } } } dependencies { compileOnly files('cruzr-libs/cruzr.jar') }
Find the Sync Project with Gradle Files button on the Android Studio tool bar and click to perform a sync operation. After synchronizing, you can use the API in the Cruzr library.
Note: The latest version of the Cruzr SDK is built into the system. Developers only use the downloaded SDK for compilation to ensure stable compatibility.
Configure permissions
To develop a robot application based on Cruzr, you need to declare the necessary permissions and Android application components in AndroidManifest.
Open the file app/src/main/AndroidManifest.xml and add the permission to call the Cruzr API in the <manifest> tag.
<?xml version="1.0" encoding="utf-8"?> <manifest> <uses-permission android:name="com.ubtrobot.permission.ROBOT" /> </manifest>
SDK initialization
Create a subclass of Application and set the class name to the [android: name[ attribute of the [
<?xml version="1.0" encoding="utf-8"?> <manifest> ... <application android:name=".YourApp"> ... </application> </manifest>
The initialization method to call Cruzr inside onCreate, which is the subclass of Application.
public class YourApp extends Application { @Override public void onCreate() { super.onCreate(); Robot.initialize(this); } }
Make the robot say "Hello"
Cruzr provides the basic functions of multiple modules in the form of system services. Let's try to call the speech synthesis API of the voice service to make the robot say "Hello".
Open the pre-created file app/src/main/java/.../MainActivity.java in the project and add the following code to the onCreate method:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SpeechManager speechManager = Robot.globalContext() .getSystemService(SpeechManager.SERVICE); speechManager.synthesize("Hello"); } }
To install and run the application, refer to the "Run on a real device" chapter of the ["Run your app"[ document in the official Android guide, and perform the corresponding operations to get your first robot application running on the robot. If everything runs smoothly, you will hear the robot say "Hello".
Next step
In the example above, when the robot application is started, the robot carries out the action of saying "Hello". In fact, it is the user's instructions that make the robot carry out the action. Skill is the component that Cruzr uses to receive user instructions and perform the corresponding actions. You should first be proficient in implementing a skill. Please refer to the create your first skill chapter.