“DJI onProductChange”与”onProductConnect”之间的区别是什么?

huangapple go评论88阅读模式
英文:

Difference between DJI onProductChange and onProductConnect

问题

问题

上面的代码是我尝试的方式,但它不起作用,我猜这是因为我误解了onProductChange()onProductConnect()方法的用法。

DJISDKManager.getInstance().getProduct()总是立即返回null。

注意:在onConnectToUavFinishedSignal.await()调用完成后立即返回null。几秒钟后,我才会得到飞机的有效实例。

我还注意到有时会调用onProductChange(),并且某些值被日志输出为Unknown和None。这些是什么,我如何测试它们?像if (baseProduct == ???) doSomething()

环境

  • Android 9
  • MSDK 4.13.1
  • Phantom 4 Pro

请让我知道如果你需要更多的信息。

英文:

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

答案1

得分: 1

Difference

根据SDK文档onProductChanged主要用于检测连接状态的变化,从仅远程控制器连接到飞机与在您的设备上运行的SDK之间的完全连接。

请记住,当飞机断开连接时,将使用飞机实例调用此方法,但此实例将具有属性isConnectedfalse。如果将飞机对象打印到控制台,您会注意到如果isConnectedtrue,它将打印飞机名称,否则将打印"None"。

至于onProductConnect,它将始终在DJISDKManager.getInstance().registerApp()成功后或在您使用DJISDKManager.getInstance().startConnectionToProduct()成功手动连接到飞机后调用。在我的测试中,即使应用程序注册成功,该方法也将返回false,因此您可能需要检查SDKManagerCallback::onRegister是否导致DJISDKError.REGISTRATION_SUCCESS

Solution

您需要侦听组件更改事件。不幸的是,仅因为飞机已连接,并不意味着各个组件,如飞行控制器、相机等都已连接。您需要实现onComponentChange并添加侦听器以侦测组件何时连接。这些组件的连接顺序不总是相同的,可能在产品连接之前或之后开始连接。

@Override
public void onComponentChange(
  BaseProduct.ComponentKey componentKey,
  BaseComponent oldBaseComponent,
  BaseComponent newBaseComponent
) {
  newBaseComponent.setComponentListener(isConnected -> {
    // 检查组件是否已连接并访问数据
    if (isConnected) {
      if(componentKey == ComponentKey.FLIGHT_CONTROLLER) {
        // DJISDKManager.getInstance().getProduct()不应再为空
        DJISDKManager.getInstance().getProduct().getModel();
      }
    }
  })
}
英文:

Difference

According to the SDK Docs onProductChanged is primarily used to detect when the connection status changes from only remote controller connected to a full connection between the aircraft and the SDK running on your device.

Keep in mind that when the aircraft is disconnected, this method will be called with an instance of an aircraft, but this instance will come with property isConnected as false. If you print the aircraft object to the console you will notice that if isConnected is true, it will print the aircraft name, otherwise, it will print "None".

As long for the onProductConnect, it will be called always after DJISDKManager.getInstance().registerApp() succeeded or after you manually connect to the aircraft with success using DJISDKManager.getInstance().startConnectionToProduct(). In my tests, even though the app registration succeeds, the method will return false, so you might need to check if the SDKManagerCallback::onRegister results in DJISDKError.REGISTRATION_SUCCESS.

Solution

You need to listen to component change events. Unfortunately just because the product is connected it does not mean that the individual components, such as the flight controller, camera etc are connected. You will need to implement onComponentChange and add a listener to detect when a component is connected. These don't always connect in the same order and may start to connect before or after the product is connected.

@Override
public void onComponentChange(
  BaseProduct.ComponentKey componentKey,
  BaseComponent oldBaseComponent,
  BaseComponent newBaseComponent
) {
  newBaseComponent.setComponentListener(isConnected -> {
    // check if component connected and access data
    if (isConnected) {
      if(componentKey == ComponentKey.FLIGHT_CONTROLLER) {
        // DJISDKManager.getInstance().getProduct() should no longer be null
        DJISDKManager.getInstance().getProduct().getModel();
      }
    }
  })
}

huangapple
  • 本文由 发表于 2020年10月14日 21:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64354240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定