## SDK integration instructions
### The first step is to import the AAR file
Add the AAR file to the LIBS file

```groovy
implementation(name: 'csjsdk-beta', ext: 'aar')
```
On the app build.gradle Add the code under the structure {Android}

```groovy
repositories {
    flatDir {
        dirs 'libs'
    }
}
```
###   The second step is to introduce the dependency library

```groovy
implementation 'io.netty:netty-all:4.1.23.Final'
```

### Step 3 initialization
Initialize SDK in application
**Please ensure that the device side has the network for code authorization**

1. Register account on [Csjbot Developer Platform]（ http://openpro.csjbot.com )
2. Fill in the information and apply for API key and user key
3. Find API management in the menu on the right. API key corresponds to key and user key corresponds to secret
4. Fill in when initializing. Pay attention to the application permission

```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- Read external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  <!-- Write external storage -->
<uses-permission android:name="android.permission.INTERNET" /> <!-- Network Communications -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
```

```java
CsjRobot.authentication(this,"yourkey", "yoursecret", new OnAuthenticationListener() {
    @Override
    public void success() {
        Log. D ("tag", "authorization succeeded!");
    }

    @Override
    public void error() {
        Log. D ("tag", "authorization failed!");
    }
});
```

Only once network authorization is required, and subsequent use does not require network authorization, and local verification will be performed
After success, you can initialize the SDK, select the required function (can be skipped, open by default), set the connection address and port (optional, the default is 192.168.99.101), set the robot type, the default is Amy welcome robot, and set the corresponding machine type if necessary

```java
//enable voice module (default open) before calling init.
CsjRobot.enableAsr (true);
//Whether to enable face recognition module
CsjRobot.enableFace (true);
//Is navigation module enabled
CsjRobot.enableSlam (true);
//Set the communication address, the default is 192.168.99.10160002
CsjRobot.setIpAndrPort("127.0.0.1", 60002);

/*
* set up the robot type (before calling init).
* If Alice is set, it is the protocol of the eighth generation robot
*/
CsjRobot.setRobotType(CsjRobot.RobotType.ALICE_NEW);
/*
*Initialize SDK
*/
CsjRobot.getInstance ().init(this);

```



## Instructions for robot functions

SDK initialization

After initialization, the robot connection state events can be registered, and different states are displayed in the callback


```java
CsjRobot.getInstance().registerConnectListener(new OnConnectListener() {
            @Override
            public void success() {
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
            }

            @Override
            public void faild() {

            }

            @Override
            public void timeout() {

            }

            @Override
            public void disconnect() {

            }
});
```



### 一、ASR、NLP and Wakeup

#### ASR（ONLY FOR Chinese）

Functions can be referred to [Demo的AsrNlpActivity.java](http://gitlab.csjbot.com/open/RobotSDKForYingbin-Android-Sample/blob/master/Android-Sample/app/src/main/java/com/demo/csjbot/csjsdkdemo/future/AsrNlpActivity.java)

1. Turn on and off voice service (enabled ASR needs to be set to true by default)
```java
CsjRobot.getInstance().getSpeech().startSpeechService();
```

2、Register robot speech recognition and semantic understanding events

```java
private OnSpeechListener onSpeechListener = new OnSpeechListener() {
    @Override
    public void speechInfo(final String s, final int i) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // Simple parsing example
                if (Speech.SPEECH_RECOGNITION_RESULT == i) { // Information identified
                  ...
                } else if (Speech.SPEECH_RECOGNITION_AND_ANSWER_RESULT == i) {// Identified information and answers
                  ...
                }
            }
        });
    }
};
```

3、Turn on multiple speech recognition

```java
CsjRobot.getInstance().getSpeech().startIsr();
```

Note: this method will not be called by default. After calling, the SDK will monopolize the microphone
If you want to stop recognition

4、Stop speech recognition

```
CsjRobot.getInstance().getSpeech().stopIsr();
```

5、close

```java
CsjRobot.getInstance().getSpeech().closeSpeechService();
```

    #### Wake Events

Register the robot wake-up event and say the wake-up word "Lingxi Lingxi" to the robot

```java
private OnWakeupListener onWakeupListener = new OnWakeupListener() {
    @Override
    public void response(final int i) {
		...        
    }
};
```



###  2、 Navigation and walking

Navigation process, function can refer to [Demo的SlamDemoActivity.java](http://gitlab.csjbot.com/open/RobotSDKForYingbin-Android-Sample/blob/master/Android-Sample/app/src/main/java/com/demo/csjbot/csjsdkdemo/future/SlamDemoActivity.java)

#### Power on for the first time

​	1、Find a suitable place to place the charging point and power it on

​	2、The robot charges and starts up at the charging point

​	3、After waiting to enter the system, use the robot stuido software to build the map

​	4、Call the save map interface. The default name of the map is map.map

```java
mCsjBot.getAction().saveMap();
```

​	5、You can then call to view the map recovery status

```java
public void queryMapState(View view) {
	mCsjBot.getAction().getMapState(onMapStateListener);
}

private OnMapStateListener onMapStateListener = new OnMapStateListener() {
    @Override
    public void mapState(String s) {
        ...
    }
};
```



​	6、Using robot stuido software, mobile robot to the appropriate position, call to obtain the coordinate 1

```java
public void getAndSavePose1(View view) {
    ...
	mCsjBot.getAction().getPosition(listener);
    ...
}
```



​	7、Using robot stuido software, mobile robot to the appropriate position, call to obtain the coordinate 2

```java
public void getAndSavePose2(View view) {
    ...
	mCsjBot.getAction().getPosition(listener);
    ...
}
```



​	8、If you want to go to point 1, you can call it. See calling method

```java
public void goPose1(View view) {...}
```

```java
private void goPose(final RobotPose pose) {...}
```



​	9、If you want to cancel the navigation, you can call the

```java
public void cancelNavi(View view) {
    mCsjBot.getAction().cancelNavi(new OnNaviListener() {
        @Override
        public void moveResult(String s) {}

        @Override
        public void messageSendResult(String s) {}

        @Override
        public void cancelResult(String s) {
            //Gets the status in this callback
        }

        @Override
        public void goHome() {}
    });
}
```

####  Restart navigation process


premise：

- **The charging point is in the original position**

- **The robot is starting the charging pile**

- **The save map interface has been called**

  

  At this time, after the robot SDK is connected, the restore map interface can be called to restore the map

  ```java
  public void restoreMap(View view) {
      mCsjBot.getAction().loadMap();
  }
  ```

  

  After restoring the map, you can directly call the saved points for navigation

   ```java
	public void goPose1(View view) {...}
   ```

   ```java
	private void goPose(final RobotPose pose) {...}
   ```

#### Charging back to pile

- Note: it is necessary to start the machine at the charging point

  The robot can be recharged by calling the interface for charging back to the pile

  ```java
  public void goHome(View view) {
      mCsjBot.getAction().goHome(new OnNaviListener() {
  		...
          @Override
          public void goHome() {
  			// If successful, there is a callback
          }
      });
  }
  ```

  

​	

#### Other motion interfaces

​	1、Click and call the interface to make the robot move forward, backward, left and right. Note: these sensors will not work and will not navigate

```java
public void forward(View view) {
    mCsjBot.getAction().moveForward();
}

public void backward(View view) {
    mCsjBot.getAction().moveBack();
}

public void turnLeft(View view) {
    mCsjBot.getAction().moveLeft();
}

public void turnRight(View view) {
    mCsjBot.getAction().moveLeft();
}
```

​	Tips：If you want to make app and other remote control robot movement, you can call these methods

​	2、Turn, turn a specific angle, with the current orientation of the robot as a reference

```java
public void turnRight60Degrees(View view) {
    mCsjBot.getAction().moveAngle(-60, new OnGoRotationListener() {
        @Override
        public void response(int i) {

        }
    });
}

public void turnLeft60Degrees(View view) {
    mCsjBot.getAction().moveAngle(60, new OnGoRotationListener() {
        @Override
        public void response(int i) {

        }
    });
}
```

​	Tips：You can use this to turn around the sound source location



​	3、Turn to a specific angle, and take the orientation of the robot as a reference

```java
public void turnTo60(View view) {
    mCsjBot.getAction().goAngle(60);
}
```



### TTS(speech synthesis)

```java
        // robot TTS
        ISpeechSpeak speak = CsjRobot.getInstance().getTts();

        // Start Speaking
        speak.startSpeaking("How do you do！", new OnSpeakListener() {
            @Override
            public void onSpeakBegin() {
                // Begin speaking
            }

            @Override
            public void onCompleted(SpeechError speechError) {
                // Speech completion
            }
        });

        // Stop talking
        speak.stopSpeaking();

        // Are you talking
        speak.isSpeaking();

        //You can also use your own TTS (inherited from ispeechspeak interface)
        CsjRobot.getInstance().setTts(null);
```



### Face recognition information
```java
        // Robot face recognition
        Face face = CsjRobot.getInstance().getFace();

        // Turn on the camera (turn on video streaming by default)
        face.openVideo();

        // Turn off the camera
        face.closeVideo();

        // Start face recognition service (on by default)
        face.startFaceService();

        // Turn off face recognition service
        face.closeFaceService();


        // Take photos by camera
        face.snapshot(new OnSnapshotoListener() {
            @Override
            public void response(String s) {
                /*
                * {
                    "error_code": 0,
                    "face_position": 0,
                    "msg_id":”FACE_SNAPSHOT_RESULT_RSP"
                    } */
                // erro_code : 0 means that there is a face, and the other means that there is no face
            }
        });

        // Face registration (save the currently photographed face)
        face.saveFace("张三", new OnFaceSaveListener() {
            @Override
            public void response(String s) {
                /*
                * {
                    "msg_id":"FACE_SAVE_RSP",
                    “person_id”:”personx20170107161021mRJOVw”,
                       “error_code":0
                   }*/

                // error_cdoe : 0 success, 40002 face has been registered, 40003 face name format error
            }
        });

        // Face information deletion
        face.faceDel("faceId");

        // Mass deletion of face information
        face.faceDelList("faceIdsJson");

        // Get all face databases
        face.getFaceDatabase(new OnGetAllFaceListener() {
            @Override
            public void personList(String s) {
                // s
            }
        });
```

### Robot status
```java

```

### Robot expression
```java

```

### Robot version
```java
// Robot version information
Version version = CsjRobot.getInstance().getVersion();

// Get robot version information
version.getVersion(new OnGetVersionListener() {
    @Override
    public void response(String s) {
        // Version information returned
    }
});
```



