Please select Into the mobile phone version | Continue to access the computer ver.
Difference between onProductChange and onProductConnect
72 0 10-14 06:08
Uploading and Loding Picture ...(0/1)
o(^-^)o
NCB
lvl.1

Brazil
Offline

Context

I'm building a Flutter Plugin above the DJK SDK. For that, I have to implement the communication with the aircraft on the native side, and I'm doing it with Java. I'm also doing it only for Android.

One of the methods of the API is boolean connectToAircraft(), which must return if the connection with the aircraft succeeded.

Expected/current behavior

After I call connectToAircraft() - which invokes the DJISDKManager.getInstance().startConnectionToProduct() method, I expected to be able to use anything related to aircraft immediately, but this doesn't happen. I have to wait a few seconds before I can retrieve data from the aircraft.

Some code

public class UavApi implements IUavApi, DJISDKManager.SDKManagerCallback {
        ...
       
        private final CountDownLatch onConnectToUavFinishedSignal = new CountDownLatch(1);
       
        ...
       
        public boolean connectToUav() throws InterruptedException {
                Logger.v("connectToUav()");

                DJISDKManager.getInstance().startConnectionToProduct();

                synchronized (onConnectToUavFinishedSignal) {
                        onConnectToUavFinishedSignal.await();
                }

                return DJISDKManager.getInstance().getProduct() instanceof Aircraft;
        }
       
        ...
       
        @Override
        public void onProductConnect(@Nullable final BaseProduct baseProduct) {
                Logger.v(MessageFormat.format("onProductConnect(product: {0})", baseProduct));

                if (baseProduct != null) {
                  handleProductConnected(baseProduct);
                }
        }

        @Override
        public void onProductChanged(@Nullable final BaseProduct baseProduct) {
                Logger.v(MessageFormat.format("onProductChanged(product: {0})", baseProduct));

                if (baseProduct != null) {
                  handleProductConnected(baseProduct);
                }
        }

        ...

        private void handleProductConnected(@NonNull final BaseProduct baseProduct) {
                Logger.d(MessageFormat.format("Is null? {0}", baseProduct == null ? "Yes" : "No"));
                Logger.d(MessageFormat.format("Type: {0}", baseProduct.getClass().getSimpleName()));
               
                onConnectToUavFinishedSignal.countDown();
        }

        ...
}

Problem

The code above is what I tried to do, but it's not working and guess it's because I'm misunderstanding the use of the onProductChange() and onProductConnect() methods.

The DJISDKManager.getInstance().getProduct() is always returning null.

OBS: It's always returning null immediately after the onConnectToUavFinishedSignal.await() call finishes. After a few seconds, I get a valid instance of the aircraft.

Something I've also noticed is that sometimes the onProductChange() is called with some value that the log outputs as Unknwoun and None. What are those and how can I test for them? Like if (baseProduct == ???) doSomething()

Environment

- Android 9
- MSDK 4.13.1
- Phantom 4 Pro
10-14 06:08
Use props
Advanced
You need to log in before you can reply Login | Register now

Credit Rules