当作为独立类实现时,DJI Sdk将无法连接

时间:2017-03-22 23:58:12

标签: android dji-sdk

我似乎无法让我的独立课程连接到我的幻像4,无论我做什么,因为它是作为一个独立的类实现的,可以随意旋转。永远不会达到回调,数据似乎永远不会从设备返回。

公共类DjiManager {

private static DjiManager instance = null;

private Context mContext;
private DJIBaseProduct mProduct;
private SendData mData;
private DJIFlightController mFlightController = null;
private Handler mConnectHandler = null;
private Runnable mConnectRunnable = null;

public DjiManager(Context context) {
    mContext = context;
    //start eventbus
    EventBus.getDefault().register(this);
    //set reference to data storage
    mData = iOtManager.deviceData;
    //Initialize DJI SDK Manager
    initSdk();
}

public static DjiManager getIntance(Context context) {
    if (instance==null) {
        instance = new DjiManager(context);
    }
    return instance;
}

public void shutDown() {
    Log.d("DJI", "DGI Shutting down");
    //stop eventbus
    EventBus.getDefault().unregister(this);
    //stop the sdk
    stopSdk();
    //clear data buffer reference
    mData = null;
    //clear instance
    instance = null;
}

@Subscribe
public void usbAttached(UsbAttachedEvent event) {
    Log.d("DJI", "DGI Usb attached event");
    connectDrone();
}

@Subscribe
public void commandSent(SdkCommandSent event) {

}

private void stopSdk() {
    Log.d("DJI", "DGI stopSdk");
    //shutdown geo interface
    DJIFlyZoneManager.getInstance().setGEOSystemEnabled(false, new DJICommonCallbacks.DJICompletionCallback() {
        @Override
        public void onResult(DJIError djiError) {
            if (null != djiError) {
                Log.d("DJI", "Stopping Geo interface");
                Toast.showError((Activity) mContext, djiError.getDescription());
            }
        }
    });
    //stop connection
    DJISDKManager.getInstance().stopConnectionToProduct();
}

private void initSdk() {
    //Initialize DJI SDK Manager
    DJISDKManager.getInstance().initSDKManager(mContext, mDJISDKManagerCallback);
}

private void reInitSdk() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            initSdk();
        }
    },5000);
}

private void connectDrone() {
    if (mConnectHandler!=null && mConnectRunnable!=null) {
        mConnectHandler.removeCallbacks(mConnectRunnable);
    }
    mConnectHandler = new Handler();
    mConnectRunnable = new Runnable() {
        @Override
        public void run() {
            DJISDKManager.getInstance().startConnectionToProduct();
        }
    };
    mConnectHandler.postDelayed(mConnectRunnable,1000);
}

private void setupCallbacks() {
    //start geo sytem interface
    Log.d("DJI", "Setting up callbacks");
    DJIFlyZoneManager.getInstance().setGEOSystemEnabled(true, new DJICommonCallbacks.DJICompletionCallback() {
        @Override
        public void onResult(DJIError djiError) {
            if (null != djiError) {
                Log.d("DJI", "Error in callbacks: " + djiError.getDescription());
                Toast.showError((Activity) mContext, djiError.getDescription());
            } else {
                initFlightController();
            }
        }
    });
}


private void initFlightController() {
    if (isFlightControllerSupported()) {
        Log.d("DJI", "Flight controller supported");
        mFlightController = ((DJIAircraft) DJISDKManager.getInstance().getDJIProduct()).getFlightController();
        mFlightController.setUpdateSystemStateCallback(new DJIFlightControllerDelegate.FlightControllerUpdateSystemStateCallback() {
            @Override
            public void onResult(final DJIFlightControllerCurrentState state) {
                mData.sdk_speed_h = String.valueOf(state.getVelocityX());
                mData.sdk_speed_v = String.valueOf(state.getVelocityY());
                mData.sdk_lat = String.valueOf(state.getAircraftLocation().getLatitude());
                mData.sdk_lng = String.valueOf(state.getAircraftLocation().getLongitude());
                mData.sdk_altitude = String.valueOf(state.getUltrasonicHeight());
            }
        });
    } else {
        Log.d("DJI", "Flight controller not supported");
        connectDrone();
    }
}
private boolean isFlightControllerSupported() {
    return DJISDKManager.getInstance().getDJIProduct() != null &&
            DJISDKManager.getInstance().getDJIProduct() instanceof DJIAircraft &&
            ((DJIAircraft) DJISDKManager.getInstance().getDJIProduct()).getFlightController() != null;
}












//CALLBACKS

private DJISDKManager.DJISDKManagerCallback mDJISDKManagerCallback = new DJISDKManager.DJISDKManagerCallback() {
    @Override
    public void onGetRegisteredResult(DJIError error) {
        Log.d("DJI", error == null ? "success" : error.getDescription());
        if(error == DJISDKError.REGISTRATION_SUCCESS) {
            connectDrone();
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Log.d("DJI", "DGI Registration Succcessfull!");
                }
            });
        } else {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Log.d("DJI", "Register App Failed! Please enter your App Key and check the network.");
                    Toast.showError((Activity) mContext, "The purplebox failed to connect to a Dji Drone");
                    reInitSdk();
                }
            });
        }
    }
    @Override
    public void onProductChanged(DJIBaseProduct oldProduct, DJIBaseProduct newProduct) {
        mProduct = newProduct;
        if(mProduct != null) {
            Log.d("DJI", "Product found: " + newProduct.getModel());
            mProduct.setDJIBaseProductListener(mDJIBaseProductListener);
            //setup product
            setupCallbacks();
        } else {
            Log.d("DJI", "Product Not Found, reconnecting...");
            //connectDrone();
        }
    }
};

private DJIBaseProduct.DJIBaseProductListener mDJIBaseProductListener = new DJIBaseProduct.DJIBaseProductListener() {
    @Override
    public void onComponentChange(DJIBaseProduct.DJIComponentKey key, DJIBaseComponent oldComponent, DJIBaseComponent newComponent) {
        Log.d("DJI", "Component Changed");
    }
    @Override
    public void onProductConnectivityChanged(boolean isConnected) {
        Log.d("DJI", "DGI Connected: " + isConnected);
    }
};

}

1 个答案:

答案 0 :(得分:0)

似乎你没有打电话给registerApp。 相反,它看起来像你寻找USB连接。