USB disconnect/reconnect
I have usb accessory code that detects usb connection. Everything works if I start the app by plugging in usb, or I plug in the usb when the app is already running. Things seem to break when I unplug usb, then try to plug it back in again while the app is still running.
-
On usb disconnect BaseSDKManagerCallback.onProductDisconnect does not seem to fire, even when I explicitly call DJISDKManager.getInstance().stopConnectionToProduct();
-
On usb reconnect onProductConnect fires but says the product is not connected.
Aircraft aircraft = (Aircraft) baseProduct;
Log.i("TEST", "" + aircraft.isConnected());
aircraft.getName(new CommonCallbacks.CompletionCallbackWith<String>() {
@Override
public void onSuccess(String s) {
Log.i("TEST", s);
}
@Override
public void onFailure(DJIError djiError) {
Log.i("TEST", djiError.getDescription());
}
});
aircraft.isConnected() is false and getName errors (SDK error, please contact [email protected] for help).
dji sdk 4.11 Mavic Pro Platinum Android 10 AndroidX
Public comment from Luce Luo in Zendesk ticket #32211:
Dear Customer,
Thank you for contacting DJI.
Please send the broadcast 'USB_ACCESSORY_ATTACHED' when you reconnect the USB.
It's recommended to refer to the logic of the official sample.
https://github.com/dji-sdk/Mobile-SDK-Android
Thanks,
Luce Luo
DJI Dev Team
inline-687040633.png
Hi we are already doing this. I have also ensured that we are sending the sendBroadcast to the same context object.
Boolean hasSDKRegistered = DJISDKManager.getInstance().hasSDKRegistered();
if (hasSDKRegistered) {
Intent attachedIntent = new Intent();
attachedIntent.setAction(DJISDKManager.USB_ACCESSORY_ATTACHED);
context.sendBroadcast(attachedIntent);
DJISDKManager.getInstance().startConnectionToProduct();
}
I tested the sample application and it seems to work so I'm not sure where the issue is in my application. I noticed the sample application doesnt call startConnectionToProduct to trigger the event but in my code if I don't call the startConnectionToProduct then onProductConnect is never fired.
Public comment from Luce Luo in Zendesk ticket #32211:
Dear Customer,
Thank you for contacting DJI. I'm not sure about your code. Maybe you can refer to the logic of the sample.
Thanks,
Luce Luo DJI Dev Team
Hi I had the same problem and solved this on my app by ensuring that the android:launchMode="singleTop" is set for my main activity in AndroidManifest.xml, and adding this method to my main activity:
@Override
protected void onNewIntent(@NonNull Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
Intent attachedIntent = new Intent();
attachedIntent.setAction(DJISDKManager.USB_ACCESSORY_ATTACHED);
sendBroadcast(attachedIntent);
}
super.onNewIntent(intent);
}
The "singleTop" mode ensures that the incoming USB_ACCESSORY_ATTACHED intent is received by the existing instance of the activity. If you are more curious, read about singleTop mode in the Android developer guide: https://developer.android.com/guide/topics/manifest/activity-element.
This also appears to be the same as issue #232, by the way.
While struggling with the same issue, I learned that you must also have Bluetooth permissions in order to use the Android Open Accessory service which controls the USB under the hood.
I.e. add this to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.BLUETOOTH" />