简体   繁体   中英

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

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

}

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.

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM