5

I'm following the steps to use ADK to control mbed via the Android Studio

however their mbed adkport code (Scroll down to adkport hyperlink) requires these imports

import com.android.future.usb.UsbAccessory;
import com.android.future.usb.UsbManager;

I've noticed another thread that suggested the developer's solution was to switch it to android.hardware.usb, but when I do that, 3 different lines won't work because the hardware based package doesn't support getAccessory and getInstance symbols

Any solutions to this problem? Can't get my head around it

I tried following the steps for replacing the code to use android.hardware.usb instead but I still get an issue with their own android developer routine

     //mManager = UsbManager.getInstance(context);
    UsbManager mManager = (UsbManager) getSystemService(Context.USB_SERVICE);

however now it doesn't recognize getSystemService

Here's where it fails.

public void setup(Context context)
{

    //mManager = UsbManager.getInstance(context);
    UsbManager mManager = (UsbManager) getSystemService(Context.USB_SERVICE);  //<-----
    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];
        if(mManager.hasPermission(mAccessory))
        {
        openAccessory(mAccessory);
        }
    }

}
Community
  • 1
  • 1
Iancovici
  • 4,946
  • 6
  • 33
  • 54
  • 2
    Search http://developer.android.com/guide/topics/connectivity/usb/accessory.html for the add-on symbols and you will find their replacements. – Morrison Chang Mar 30 '14 at 20:41
  • Thank, seems to be helping make progress, question edited for further difficulty encountered with this approach. – Iancovici Mar 30 '14 at 23:33
  • "getSystemService" is quite generic beyond just the USB apis. But it is a method of Context, and perhaps you are trying to call it within a class that does not extend Context? – Chris Stratton Mar 30 '14 at 23:42
  • @ChrisStratton I'm using context easily in other parts of the same code. I'll update the question with that section that doesn't work well – Iancovici Mar 30 '14 at 23:48
  • 1
    If you are passing in the valid Context as an argument because the class in which this occurs is not a subclass of Context, then you should call the method of the context passed as an argument, ie in your case, `context.getSystemService()` rather than trying to call this (non-existent?) method of your current class, which is what happens when you just type the method without specifying a Context. – Chris Stratton Mar 30 '14 at 23:53

2 Answers2

1

getSystemService() is a method that can be called from inside an activity or from inside a non-activity class using context of an activity.

Since your function setup() should be called by passing a Context, usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); will solve your problem.

1

One of the comments (from Morrison Chang) should actually be copied as an answer (worked for me in similar situation).

His suggestion to read USB Accessory is spot on. Distilled instructions:

  1. Make sure your manifested minSdkVersion is high enough (For me it was 19).
  2. Replace UsbManager.getInstance(this) with (UsbManager) getSystemService(Context.USB_SERVICE).
  3. Replace UsbManager.getAccessory(intent) with intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY).
Andy Malakov
  • 607
  • 6
  • 6