B4A Library DJI Drones

Status
Not open for further replies.
DJI Drones are very popular and advanced drones. DJI provides an Android app that is used during flights to control the drone.
The Android device connects to the remote controller with a USB cable (USB debug mode should be enabled).

DJI are also providing a feature-rich SDK that allows us, developers, to create alternative applications to control the drone. It is quite amazing to be able to easily program a drone to do all kinds of sophisticated tasks.

The following video is an example created with B4A and the DJI library. On the left side you can see the drone camera feed and various parameters. On the right side there is a standard Google Map. Touching on a point in the map starts a hotspot mission. The drone will rotate automatically around the spot.


How to use

1. Register with DJI: https://developer.dji.com/ and create an app in the User Center page. You will see an App Key that you need to add to the manifest editor.
The bundle identifier must match your app package name.
2. Add this code to the manifest editor:
B4X:
AddReplacement($DJI_KEY$, XXXXXXXXX)
CreateResourceFromFile(Macro, DJI.DJI)
Replace XXXXXXXXXX with your App key.
3. Add to the main activity code:
B4X:
#BridgeLogger: true
#MultiDex: true
#AdditionalJar: dji-sdk-provided-4.16, ReferenceOnly
#AdditionalJar: kotlin-stdlib-1.5.10

4. You might need to install a few components with B4A Sdk Manager: https://www.b4x.com/android/forum/t...droid-attr-lstar-not-found.141799/post-898956
5 Initialize the SDK when the application starts. The RegisteredResult event will be raised. If successful then call sdk.StartConnectionToProduct to start a connection.
6. The ProductConnected event will be raised. If AircraftData is not Null then you are good to go and can initialize the DJIAircraft object and the other features (see how it is done in the example).
7 Most of the operations are asynchronous. They are built to work with the Wait For keyword.
8. Expect disconnections and other errors.
9. New: See the Activity_Resume code in the example. It is needed to handle reconnections.

See the attached example.

Tips & Notes

- Make sure that you are able to connect to the drone with the DJI app. Not all Android devices have proper support for USB connected devices.
- You will see a dialog that asks you whether you always want to start the DJI app when the USB is connected. Click on "This time only". Otherwise you will need to uninstall the DJI app as it will get an exclusive permission to use the USB device.
- Use B4A-Bridge to connect the IDE to the Android device.
- Supported drones: https://developer.dji.com/mobile-sd.../product_introduction.html#supported-products
- Use the simulator to test your app. It works well.
https://developer.dji.com/mobile-sd...on-development-workflow/workflow-testing.html

- Don't fly the drone indoors.
- Check the GPS status. All the nice automatic features will fail without the GPS. Should be 4 or 5.
- Make sure that the home location is set before flight.
- Always be prepared to manually control the drone.


Download the native libraries and copy them to the additional libraries folder:
www.b4x.com/android/files/dji_additional.zip
Note that only 64 bit binary is included inside dji-sdk-4.15.aar. You can download the full aar with both 32 bit and 64 bit binaries here:

During development it will be faster to use the smaller aar file.

Download the attached library and copy it as well.

Updates

- 4.80: Based on SDK 4.16. Requires B4A v11.5+. Please add to Main module:
B4X:
#MultiDex: true
#AdditionalJar: dji-sdk-provided-4.16, ReferenceOnly
#AdditionalJar: kotlin-stdlib-1.5.10
- 4.70: Based on SDK 4.15. Check the updated example and don't miss the Activity_Resume code.
- 4.60: It is based on DJI SDK v4.14-trial1.
It requires B4A v11.5+
The minimum Android version is 5.0.

Start with the attached example. Note that the hotspot mission calls ExitApplication. You must configure a reasonable coordinate before you test it (and remove the ExitApplication).
The example needs to be updated with the attributes mentioned in v4.80 update above.
 

Attachments

  • DJI_Example.zip
    12.8 KB · Views: 887
  • DJI.zip
    39.5 KB · Views: 639
Last edited:

Mr Blue Sky

Active Member
Licensed User
Longtime User
Hi Erel yes i use b4a 8.0 last drone firmware and last DJI library 4.41 too. I check too with library helper and don't have inside DJIMissionManager

Untitled-1.jpg
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
1. Register with DJI: https://developer.dji.com/ and create an app in the User Center page. You will see an App Key that you need to add to the manifest editor.
May i suggest to add the info that the Bundle Identifier in the console must match the Packagename!
 

ELCHARO

Member
Licensed User
Longtime User
Hello Erel!

I run the app. In Samsung S7 Edge. The problem is the screen are inverse and can't rotate. All apps do is no android problem. Rotation is enable.
But the djidemo (both) are inverted and is imposible to rotate (fixed)
What can i do?

Thanks
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi,
I tried to compile your examples, but I have these errors. What can they be?
thank you
regards
 

Attachments

  • Immagine.png
    Immagine.png
    262.1 KB · Views: 359

Roberto P.

Well-Known Member
Licensed User
Longtime User
1. You should start a new thread for any Question you have!
2. You should post errors as TEXT instead of an Image

Ok DonManfred,
next time I create new thread.

here is error in text format:

Logger connected to: 1TSB3DF72B
--------- beginning of system
--------- beginning of main
**************************** BaseApplication **************************
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main$ResumableSub_Activity_Createresume (java line: 383)
java.lang.VerifyError: Verifier rejected class dji.sdk.sdkmanager.DJISDKManager due to bad method void dji.sdk.sdkmanager.DJISDKManager.<init>() (declaration of 'dji.sdk.sdkmanager.DJISDKManager' appears in /data/app/b4a.dji.test1-1/base.apk:classes2.dex)
at anywheresoftware.b4a.objects.DJISDKManagerWrapper.Initialize(DJISDKManagerWrapper.java:44)
at b4a.dji.test1.main$ResumableSub_Activity_Create.resume(main.java:383)
at b4a.dji.test1.main._activity_create(main.java:344)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at b4a.dji.test1.main.afterFirstLayout(main.java:102)
at b4a.dji.test1.main.access$000(main.java:17)
at b4a.dji.test1.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5282)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
(Exception) java.lang.Exception: java.lang.VerifyError: Verifier rejected class dji.sdk.sdkmanager.DJISDKManager due to bad method void dji.sdk.sdkmanager.DJISDKManager.<init>() (declaration of 'dji.sdk.sdkmanager.DJISDKManager' appears in /data/app/b4a.dji.test1-1/base.apk:classes2.dex)
** Activity (main) Resume **
main_activity_resume (java line: 457)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean dji.sdk.sdkmanager.DJISDKManager.hasSDKRegistered()' on a null object reference
at anywheresoftware.b4a.objects.DJISDKManagerWrapper.getRegistered(DJISDKManagerWrapper.java:87)
at b4a.dji.test1.main._activity_resume(main.java:457)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.dji.test1.main.afterFirstLayout(main.java:108)
at b4a.dji.test1.main.access$000(main.java:17)
at b4a.dji.test1.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5282)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
(NullPointerException) java.lang.NullPointerException: Attempt to invoke virtual method 'boolean dji.sdk.sdkmanager.DJISDKManager.hasSDKRegistered()' on a null object reference
** Activity (main) Pause, UserClosed = true **
No wakelock.
**************************** BaseApplication **************************
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main$ResumableSub_Activity_Createresume (java line: 412)
java.lang.VerifyError: Verifier rejected class dji.sdk.sdkmanager.DJISDKManager due to bad method void dji.sdk.sdkmanager.DJISDKManager.<init>() (declaration of 'dji.sdk.sdkmanager.DJISDKManager' appears in /data/app/b4a.dji.test1-2/base.apk:classes2.dex)
at anywheresoftware.b4a.objects.DJISDKManagerWrapper.Initialize(DJISDKManagerWrapper.java:44)
at b4a.dji.test1.main$ResumableSub_Activity_Create.resume(main.java:412)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1717)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5282)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
(Exception) java.lang.Exception: java.lang.VerifyError: Verifier rejected class dji.sdk.sdkmanager.DJISDKManager due to bad method void dji.sdk.sdkmanager.DJISDKManager.<init>() (declaration of 'dji.sdk.sdkmanager.DJISDKManager' appears in /data/app/b4a.dji.test1-2/base.apk:classes2.dex)
main$ResumableSub_btnStartMission_Clickresume (java line: 708)
java.lang.VerifyError: Verifier rejected class dji.common.mission.waypoint.WaypointMission$Builder due to bad method void dji.common.mission.waypoint.WaypointMission$Builder.<init>(dji.midware.data.model.P3.DataFlycDownloadWayPointMissionMsg) (declaration of 'dji.common.mission.waypoint.WaypointMission$Builder' appears in /data/app/b4a.dji.test1-2/base.apk)
at anywheresoftware.b4a.objects.WaypointMissionOperatorWrapper$WaypointMissionBuilderWrapper.Initialize(WaypointMissionOperatorWrapper.java:134)
at b4a.dji.test1.main$ResumableSub_btnStartMission_Click.resume(main.java:708)
at b4a.dji.test1.main._btnstartmission_click(main.java:682)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:4809)
at android.view.View$PerformClick.run(View.java:20006)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5282)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
(Exception) java.lang.Exception: java.lang.VerifyError: Verifier rejected class dji.common.mission.waypoint.WaypointMission$Builder due to bad method void dji.common.mission.waypoint.WaypointMission$Builder.<init>(dji.midware.data.model.P3.DataFlycDownloadWayPointMissionMsg) (declaration of 'dji.common.mission.waypoint.WaypointMission$Builder' appears in /data/app/b4a.dji.test1-2/base.apk)
** Activity (main) Pause, UserClosed = true **

thank you
 

DonManfred

Expert
Licensed User
Longtime User

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi,
after following all the steps successfully, I can not make the app connect with the drone.

** Activity (main) Pause, UserClosed = false **
**************************** BaseApplication **************************
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Registered successfully!
activation state: UNKNOWN
binding state: UNKNOWN
activation listener added
No connected product

The DJI app connects.

Can you tell me what I have to do to make it connect?

thank you
 

Michael S King

New Member
Licensed User
SUPER COOL! Question? Can you control the camera with this? Specifically I want to change the shutter speed and then capture an image programmatically. In essence, I want to create an true automatic exposure bracketing routine with more control than the built in disaster lacking any imagination.

These kind of things will really inspire a programmer. Sure, I could make an app to fly the Drone but photography and cinematography is why we fly. creating a beautiful HDR (High dynamic range) photograph requires exposure bracketing. from what i read of DJI's version, they change the ISO to create the AEB. I want to not only change the shutter speed but change the number of exposures to infinity if I choose. So essentially:

  • Take and image at the current Shutter speed
  • Drop 1 f stop and take another image
  • drop 2 f stops and take and image
  • Drop 3 f stops and take an image
  • drop 4 f stops and take an image
  • return to original shutter speed
  • add 1 f stop take and image
  • add 2 f stops and take and image
  • add 3 f stops and take and image
  • add 4 f stops and take and image
  • alet me that it is finished.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Version 4.50 is released. It is based on DJI SDK v4.11.1. It works with 64 bit devices and it is compatible with AndroidX SDK.
It requires B4A v9.5+.

There are some changes in the events. The ProductConnectivityChanged event was removed and there is a new BindingStateChanged event that is used instead.
See the two examples.
 
Status
Not open for further replies.
Top