Secondary development based on DJI UAV SDK

Recently, the company needs to develop a mobile APP based on THE SDK of DJI UAV to coordinate with the background to achieve the management of UAV. Of course, DJI itself also provides us with a management platform —– DJI Sakong. Through dJI’s official APP and background management system DJI Sakong, the management of UAV can be realized. But DJI quotidian cost is too high, the advanced version needs 1 year 19999 cost. Therefore, we need to develop our own mobile phone APP to transmit information about uav to the background.

Here is a summary of some places that need to pay attention to in the development process, so as not to step on the posterity pit.

Related links

Here are the links to the materials used:

Download center

Through the download center, we can download uav operation manual, quick start manual and different APP versions, providing convenient software tools for our development and so on.

The official Demo

There are many demos, and different ones are used to describe different function points. Remember to use the code in the demo when developing, not the official documentation, because you may not be able to run the program.

mall

In the mall, you can learn about the features of related products and develop them better

The document

Documentation is something to look at, especially if you’re new to drones. The documents are all in English, so it’s actually easy to understand. I really don’t understand that you can translate the document into Chinese through the Translation function of Google Browser. It is recommended to view the original text together with Chinese.

API

The Api can be used as a supplement in the development process. If you don’t know the method, you can search here directly. The Api search function is very powerful, you just need to enter the different method name or class name. The explanation in the API is relatively detailed.

To enter the body

First of all, to develop DJI DRONE APP, the following conditions need to be met:

  • First of all, you need to have a drone (this drone needs to support SDK to check the supported drone model).
  • Developer account

If the above conditions are met, you can start development. Just like using the third-party SDK, you need to create an APP in the developer account. Here you need to pay attention to the APP package name must be the same as the real APP package name, otherwise it will not run successfully!

I’m not going to repeat the basics of how to import dependencies, how to create an APP, but I’m going to use all the documentation, but the documentation doesn’t have all the dependencies, so what you need to do is download the demo, and then copy all the dependencies from the demo into your project, okay

To be more specific, this is not complete in the documentation, you need to add dependency:

    packagingOptions {
        doNotStrip "*/*/libdjivideo.so"
        doNotStrip "*/*/libSDKRelativeJNI.so"
        doNotStrip "*/*/libFlyForbid.so"
        doNotStrip "*/*/libduml_vision_bokeh.so"
        doNotStrip "*/*/libyuv2.so"
        doNotStrip "*/*/libGroudStation.so"
        doNotStrip "*/*/libFRCorkscrew.so"
        doNotStrip "*/*/libUpgradeVerify.so"
        doNotStrip "*/*/libFR.so"
        doNotStrip "*/*/libDJIFlySafeCore.so"
        doNotStrip "*/*/libdjifs_jni.so"
        doNotStrip "*/*/libsfjni.so"
        doNotStrip "*/*/libDJICommonJNI.so"
        doNotStrip "*/*/libDJICSDKCommon.so"
        doNotStrip "*/*/libDJIUpgradeCore.so"
        doNotStrip "*/*/libDJIUpgradeJNI.so"
        exclude 'META-INF/rxjava.properties'} instead of the ones written in the documentationCopy the code

Now that dependencies have been introduced, you are ready to develop.

Here is a brief introduction to the use of SDK steps, the emphasis is to accept the need for special attention.

First you need to register with the SDK, and then call the login in the successful callback

 if (isRegistrationInProgress.compareAndSet(false.true)) {
            AsyncTask.execute(() -> DJISDKManager.getInstance()
                    .registerApp(MainActivity.this.getApplicationContext(),
                            new DJISDKManager.SDKManagerCallback() {
                                @Override
                                public void onRegister(DJIError djiError) {
                                    // If there are no problems with the package name and API_KEY then the registration is successful
                                    if (djiError == DJISDKError.REGISTRATION_SUCCESS) {
                                        DJISDKManager.getInstance().startConnectionToProduct();
                                       	// Log inloginAccount(); }}@Override
                                public void onProductDisconnect(a) {
                                    Log.e(TAG, "onProductDisconnect");
                                    notifyStatusChange();

                                }

                                @Override
                                public void onProductConnect(BaseProduct baseProduct) {
                                    Log.e(TAG, String.format("onProductConnect newProduct:%s",
                                            baseProduct));
                                    notifyStatusChange();
                                }

                                @Override
                                public void onComponentChange(BaseProduct.ComponentKey componentKey, BaseComponent oldComponent, BaseComponent newComponent) {}@Override
                                public void onInitProcess(DJISDKInitEvent djisdkInitEvent, int i) {}@Override
                                public void onDatabaseDownloadProgress(long l, long l1) {}})); }Copy the code

The above step is the one you have to go through to get the APP started, otherwise you can’t fly the drone.

Here are some things to note:

  • None of the above callbacks are in the main thread, so if you want to manipulate interface content, you need to do it in the main thread
  • Network support is required to register the SDK and login account for the first time, and is not required after that.
  • Login must be invoked after the SDK is successfully registered; otherwise, the login page cannot be loaded

Ok, after the above steps are completed, we are waiting for the drone to connect. When the drone connects, the callback will be triggered. When the drone is connected, we can start the real development.

The main thing to do is to make use of a few classes in the SDK, and their methods are well understood.

DJISDKManager

This class is key as it is the gateway to using the SDK and DJI drones.

This class is used to register the SDK and get drone objects.

After obtaining the object of UAV through SJISDKManager, the Aircraft can be used to obtain the object corresponding to each component of uav, such as: Flight control FlightController (this is the core component of UAV, which controls the flight of UAV and its position and status information), Battery Battery, Camera Camera, Gimbal Gimbal, RemoteController and so on. As shown in the COMPONENT CLASSES

We can also introduce UX SDK addresses to help us develop quickly.

The UX SDK mainly provides threaded controls that can be used in a UI that is not static but has data that does not require any processing.

For example, the dji.ux.widget.fpvWidget component, which you just put into the layout, displays the drone camera’s picture.

Pay attention to the content

Live broadcast:

if(! DJISDKManager.getInstance().getLiveStreamManager().isStreaming()) {new Thread() {
                        @Override
                        public void run(a) {
                            fpv.registerLiveVideo(VideoFeeder.getInstance()
                                            .getSecondaryVideoFeed(),
                                    true);
                            DJISDKManager.getInstance().getLiveStreamManager().setLiveUrl(
                                    "rtmp://x.x.x.x/x");
                            DJISDKManager.getInstance().getLiveStreamManager()
                                    .setVideoEncodingEnabled(true);
                            int result =
                                    DJISDKManager.getInstance().getLiveStreamManager()
                                            .startStream();
                            L.e("startLive:" + result + DJISDKManager.getInstance()
                                    .getLiveStreamManager().isStreaming() +
                                    "\n isVideoStreamSpeedConfigurable:" + DJISDKManager
                                    .getInstance().getLiveStreamManager()
                                    .isVideoStreamSpeedConfigurable() +
                                    "\n isLiveAudioEnabled:" + DJISDKManager.getInstance()
                                    .getLiveStreamManager().isLiveAudioEnabled());
                        }
                    }.start();
                }
Copy the code

Simply setLiveUrl() and start livestream will not succeed, you need to register the livestream in the previous step, FPV in the code is dji.ux.widget.fpvWidget control.

A way to get the location of the drone

/ / the initiative for Aircraft aircraft1 = (Aircraft) DJISDKManager. GetInstance (). The getProduct (); FlightControllerState state = aircraft1.getFlightController().getState(); L.e("==altitude:" + state.getAircraftLocation().getAltitude() + "latitude:" + state.getAircraftLocation().getLatitude() + "longitude:"); // Of course you can register the callback voidsetStateCallback(@Nullable FlightControllerState.Callback callback);
Copy the code

Layout name pit

Do not display the dialog_login name in the layout, because this name is already in the SDK of DJI. When you add a dialog_LOGIN name, calling the login API will report a null pointer error.

If there is an unexplained layout null pointer exception, it is likely that our own layout and the layout in THE DJI SDK have the same name.

Most callbacks are not on the main thread

Note that most of the callbacks in THE DJI SDK are not on the main thread