How Keyed Interfaces work in DJI SDK
masterIntroduced in SDK 4.0, Keyed Interfaces provide a modern way to access and monitor the state of a connected DJI product. Instead of using traditional component-based interfaces (e.g., getProduct().getCamera()), you use DJIKey objects as "addresses" to specific pieces of state and interact with them via the KeyManager.
Core Workflow
- Identify the Key: Use a specific
DJIKeysubclass (likeCameraKeyorFlightControllerKey) to create a key representing the state you want to access. - Access State: Use
KeyManager.getValue()to retrieve the current value orKeyManager.setValue()to change it. - Listen for Changes: Use
KeyManager.addListener()to receive updates only when the specific state actually changes.
Key Advantages over Existing Interfaces
- Multiple Listeners: Unlike traditional state callbacks which often allow only one listener at a time, you can attach multiple
KeyListenerinstances to the sameDJIKey. - Reduced Noise: Traditional callbacks often fire at high frequencies regardless of whether the specific value changed. Keyed interface listeners are only triggered when the value actually changes.
- Performance: The SDK uses an in-memory cache for keyed interfaces, reducing latency by avoiding unnecessary roundtrip calls to the hardware when a valid cached value exists.
- UI Integration: The mechanism is optimized for UI layers; the DJI UI Library is built entirely on this interface.
// Example: Accessing ISO via Keyed Interface
DJIKey isoKey = CameraKey.create(CameraKey.ISO);
DJISDKManager.getInstance().getKeyManager().getValue(isoKey, new GetCallback() {
@Override public void onSuccess(@NonNull Object value) {
if (value instanceof SettingsDefinitions.ISO) {
SettingsDefinitions.ISO iso = (SettingsDefinitions.ISO) value;
// Use ISO value
}
}
@Override public void onFailure(@NonNull DJIError error) {
// Handle error
}
});