Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import com.android.future.usb.*

Tags:

android

adk

mbed

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);
        }
    }

}
like image 754
Iancovici Avatar asked Nov 01 '22 02:11

Iancovici


2 Answers

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.

like image 137
Saravanabalagi Ramachandran Avatar answered Nov 23 '22 06:11

Saravanabalagi Ramachandran


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).
like image 32
Andy Malakov Avatar answered Nov 23 '22 08:11

Andy Malakov