Note: This tutorial only works for applications used in China. The same steps can be used for activating application and binding aircraft in an existing application.
You can download the tutorial's final sample project from this Github Page.
DJI aircraft firmware requires mobile applications that control DJI aircraft to be activated with the user's DJI account, if that application is being used in China. This will ensure operators use the correct set of geospatial information and flight functions for their aircrafts, as determined by their geographical location and user profile. A summary of the activation system is:
Users in China are required to activate their application by logging into their DJI account at least once every three months within the application.
Activation will be persistent in the application until the user logs out.
Internet connection will be required to log into a DJI account.
Outside of China, the SDK will automatically activate the application without requiring the user to log in.
Additionally, users in China are required to bind their aircraft to their user account in DJI Go / DJI Go 4. This is required only once. If an application is not activated, the aircraft not bound (if required), or a legacy version of the SDK (< 4.1) is being used, all camera live streams will be disabled, and flight will be limited to a zone of 100m diameter and 30m height to ensure the aircraft stays within line of sight.
Now, let's create a new project in Android Studio, open Android Studio and select File -> New -> New Project to create a new project, named 'ActivationDemo'. Enter the company domain and package name (Here we use "com.dji.activationDemo") you want and press Next. Set the minimum SDK version as API 18: Android 4.3 (Jelly Bean)
for "Phone and Tablet" and press Next. Then select "Empty Activity" and press Next. Lastly, leave the Activity Name as "MainActivity", and the Layout Name as "activity_main", press "Finish" to create the project.
This demo is build based on the ImportSDKDemo Github Sample, you can check the Integrate SDK into Application tutorial to learn how to import the Android SDK Maven Dependency and register the application using DJI Mobile SDK.
Please check the Creating an Camera Application tutorial and the sample project of this tutorial for the detailed implementations.
The MainActivity.java file is created by Android Studio by default. Let's replace the code of it with the following:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { |
In the code shown above, we implement the following features:
1. Create the layout UI elements variables, including two TextureView bindingStateTV
and appActivationStateTV
, two Buttons loginBtn
, logoutBtn
.
2. Then invoke the initUI()
method to initialize UI variables. And implement the setOnClickListener()
method of Button for all the Buttons.
3. Override the onClick()
method to implement the three Buttons' click actions.
Open the activity_main.xml layout file and replace the code with the following:
|
In the xml file, we implement the followings:
We implement the RelativeLayout
element, then declare two TextView
elements: (id:tv_binding_state_info) and (id:tv_activation_state_info) to show the activation and binding state infos. For the other three TextView
elements, we declare them as labels to show titles.
Then, we create the "Login" button(id: btn_login) and "Logout" button(id: btn_logout) buttons.
For more detail configurations of the layout, please check the activity_main.xml file of the tutorial's Github sample project.
Once you finish the above steps, let's add some resources files to the res folder on the left navigator of Android Studio.
Copy the following image and xml files from the tutorial Github Sample project's drawable folder to your project, they are used for the button's UI:
Next, open the "colors.xml" file and add the following code at the bottom to declare the black color:
<color name="colorBlack">#ff000000</color> |
Moreover, open the "strings.xml" file and replace the content with the followings:
<resources> |
Here, we define string elements for showing text in the ConnectionActivity layout.
Furthermore, open the "styles.xml" file and replace the content with the followings:
<resources> |
In the code above, we change the AppTheme style and define a common button style.
Now, if you check the activity_main.xml file, you can see the preview screenshot of the MainActivity as shown below:
In order to fetch the updated application activation state and aircraft binding state, we can use the AppActivationManager
to add listeners to fetch this infos.
Now, let's open the "MainActivity.java" file and create a AppActivationManager variable appActivationManager
, a AppActivationStateListener listener activationStateListener
and a AircraftBindingStateListener listener bindingStateListener
:
|
Next, create an initData()
method as shown below and invoke it at the bottom of onCreate()
method:
|
In the code above, we implement the following features:
We initialize the appActivationManager
variable by invoking the getAppActivationManager
method of DJISDKManager
.
Then we check if the appActivationManager
variable exist and invoke the addAppActivationStateListener()
method and pass the activationStateListener
listener as param to add listener for the app activation state update. Similarly, we invoke the addAircraftBindingStateListener()
method and pass the bindingStateListener
listener as param to add listener for the aircraft binding state update.
Lastly, we invoke the getAppActivationState()
method of appActivationManager
to get the current app activation state and update the text value of the appActivationStateTV
. Similarly, we invoke the getAircraftBindingState()
method of appActivationManager
to get the current aircraft binding state and update the text value of bindingStateTV
.
Once you finished the steps above, let's add another two methods: setUpListener
and tearDownListener
to setup the listeners and invoke them in the onResume()
and onDestroy()
methods as shown below:
private void setUpListener() { |
Here, we implement the following features:
In the setUpListener()
method, we initialize the activationStateListener
and bindingStateListener
listeners and override their onUpdate()
methods to fetch the updated AppActivationState
and AircraftBindingState
enum values. Then use them to update the text values of the appActivationStateTV
and bindingStateTV
text views.
Next, since we have added the listeners for the app activation state and aircraft binding state update in the appActivationManager
, we also need to remove them. So in the tearDownListener()
method, we invoke the removeAppActivationStateListener
and removeAircraftBindingStateListener
methods of appActivationManager
to remove the listeners. Moreover, update the text values of appActivationStateTV
and bindingStateTV
textViews to "Unknown".
Lastly, override the onResume()
method and invoke the setUpListener()
to setup the listeners. Then override the onDestroy()
method and invoke the tearDownListener()
method to remove the listeners in appActivationManager
.
In order to activate the application, we need to login a DJI user account. Now let's create the following methods and invoke them in the onClick()
method:
private void loginAccount(){ |
In the code above, we implement the following things:
In the loginAccount()
method, we invoke the logIntoDJIUserAccount()
method of UserAccountManager
to show the login dialog. And override the onSuccess()
and onFailure()
methods to show the result messages by invoking the showToast()
method.
Next, in the logoutAccount()
method, we invoke the logoutOfDJIUserAccount()
method of UserAccountManager
to logout the DJI user account. Also, override the onResult()
method to show the result messages.
For more implementation details, please check this tutorial's Github Sample Project.
Now, let's build and run the project, connect the demo application to your aircraft (Please check the Run Application for more details) and check the features we have implemented so far.
Here are the steps to activate the application:
appActivationStateTV
's text value changes to "Activated":After you finish activating the application, you can now connect your aircraft to DJI Go 4/DJI Go app and bind your aircraft to your DJI account. This is required only once. If you press the Enter Device button, you may see an alertView pop up as shown below:
Now, press the Link button to finish the mobile phone link and verification process. After that, connect the aircraft back to the application, you may see the bindingStateTV
's text value has changed to "Bound" as shown below:
Congratulations! Now, your mobile application and aircraft can be used in China without issues. In another word, your application can now see the aircraft's camera live streams and the flight will not be limited to a cylinder of 100m diameter and 30m height.
In this tutorial, you’ve learned how to use DJI Mobile SDK to activate the SDK mobile application and use DJI Go app to bind the aircraft to your DJI Acccount. The same steps can be used for activating application and binding aircraft in your application. Hope you enjoy this tutorial, and stay tuned for our next one!