Note: This Tutorial and Sample Project is developed based on Android SDK v3.5.1, an update version for Android SDK v4.0 will be published soon.
In this tutorial, we will use the Android Studio's HelloWorld template project to show you how to import DJI Android SDK and register the application. Throughout this tutorial we will be using Android Studio 2.1.1, which you can download from here: http://developer.android.com/sdk/index.html.
You can download the tutorial's final sample project from this Github Page.
Open Android Studio and select Start a new Android Studio project to create a new project.
Then enter the Application name as "ImportSDKDemo". Next enter the Company Domain and Package Name you want and select the Project location path to store the project.
Here we use "com.dji.importSDKDemo" for both Company Domain and Package name.
Important: The Package Name you enter here should be the same as the "Package Name" of the App Information you apply in DJI Developer Website.
For more details of applying for an DJI App, please check Get Started for details.
Press "Next" button to continue and set the mimimum SDK version as API 19: Android 4.4 (KitKat)
for "Phone and Tablet" and press Next.
Then select "Basic Activity" and press Next.
Lastly, leave the Activity Name as "MainActivity", the Layout Name as "activity_main", the Title as "MainActivity", and the Menu Resource Name as "menu_main". Press "Finish" to create the project.
You can download DJI Android SDK from DJI Developer Website.
Unzip the Android SDK package, go to File -> New -> Import Module on the top bar of Android Studio. Then enter the "API Library" folder location of the downloaded Android SDK package in the Source directory field. A "dJISDKLib" name will show in the "Module name" field. Press Next and finish button to finish the settings.
Double click on the "build.gradle(Module: app)" in the project navigator to open it:
Then replace the content with the following:
|
Here we add compile project(':dJISDKLIB')
at the end of "dependencies" part. This is where we configure the Android Studio Project dependencies.
Then, select the Tools -> Android -> Sync Project with Gradle Files on the top bar of Android Studio and wait for Gradle project sync finish.
Lastly, right click on the "app" module in the project navigator and click "Open Module Settings" to open the Project Struture window.
Select "app" Modules and navigate to the "Dependencies" tab, you should find the "dJISDKLIB" appear in the list. Your SDK environmental setup should be ready now!
Now, let's double click to open the MainActivity.java file in the com.dji.importSDKDemo
package from the project navigator area.
Then add import dji.sdk.sdkmanager.DJISDKManager;
at the bottom of the import classes section as shown below:
|
Wait for a few seconds and check if the import dji.sdk.sdkmanager.DJISDKManager;
line turn red, if it remains gray color, it means that you import DJI Android SDK to your Android Studio project successfully. Here is the status you should see if everything goes well:
Congratulations, now you should be able to use DJI Android SDK in your Android Studio Project!
After you finish the above steps, let's register our application with the App Key you apply from DJI Developer Website. If you are not familiar with the App Key, please check the Get Started.
Let's double click the AndroidManifest.xml file in the project navigator to open it:
Then add the following elements code on top of the "application" element:
|
Here, we request permissions that the application must be granted in order for it to register DJI SDK correctly. Also we declare the camera and usb hardwares which is used by the application.
Moreover, let's add the following elements as childs of
|
In the code above, you should substitude your App Key of the application for "Please enter your App Key here." in the value attribute under the android:name="com.dji.sdk.API_KEY"
attribute.
Finally, once you finish all the configurations for AndroidManifest.xml file, you should see the following content:
|
After you finish the steps above, open the MainActivity.java file and replace the code with the following content:
|
Here, we implement several features:
onCreate()
method, we request several permissions at runtime to ensure the SDK works well when the compile and target SDK version is higher than 22(Like Android Marshmallow 6.0 device and API 23).onCreate()
method.onGetRegisteredResult()
method to check the Application registration status and show text message here. Using the onProductChanged()
method, we can check the product connection status and invoke the notifyStatusChange()
method to notify status changes.onComponentChange()
method to check if a component object changes. Using the onProductConnectivityChanged()
method to check the connectivity status changes for the base product.Now let's build and run the project and install it to your Android device. If everything goes well, you should see the "success" textView like the following screenshot when you register the app successfully.
Important: Please check if the "armeabi-v7a", "arm64-v8a" and "x86" lib folders has been added to your jnLibs folder in dJISDKLib successfully before testing resgistering the app.
If you want to learn how to use DJI Android SDK to create a simple app, please check this tutorial: Creating a Camera Application. It’s our introductory tutorial, which guides you through connecting to your aircraft's camera to displaying a live video feed in your app, through which you can take photos and videos.