gbartonowen
gbartonowen

Reputation: 25

Getting an instance of UsbManger in a (non activity) class with ADK in android 2.3.4

I am trying to get the android ADK working in a library in another package from my activity, for which I need various permissions/instances of various things. I have tried passing the activity context to a function and then running:

public void setup(Context context)
{
    mManager = UsbManager.getInstance(context);
    UsbAccessory[] accessoryList = mManager.getAccessoryList();
    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0,
            new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    context.registerReceiver(mUsbReceiver, filter);
    mManager.requestPermission(accessoryList[0], mPermissionIntent);
    if (accessoryList[0] != null) {

        mAccessory = accessoryList[0];
        openAccessory(mAccessory);
    }
}

But this crashes the application, even when all but the first line is commented out. I'm not entirely sure what I'm doing wrong and would appreciate any help... (the function is called and passed "this")

Upvotes: 1

Views: 960

Answers (1)

lellobot
lellobot

Reputation: 373

What worked for me was checking if accessoryList is null before assigning element [0] to mAccessory.

    UsbAccessory mAccessory = (accessoryList == null ? null : accessoryList[0]);

Hope you sorted something out in the meantime.

Upvotes: 1

Related Questions