编译并链接到libusb for android

我考虑尝试编译一个C程序,允许通过USB控制Gembird SilverShield电源插座.在我的 Android HDMI电视棒这将是非常有用的.有一个开放的项目为 this.它在Linux下工作,取决于libusb.目标平台是android ICS.我想在Ubuntu Linux上开发.我有机会得到它的工作?需要的步骤是什么安装android SDK,NDK,crosscompiler …有一个较老的问题 here,相关的libusb在android但没有信息如何.将应用程序移植到Android自己的usb库可能更容易吗?
Libusb可以工作在一个无根的机器人(提供的设备支持USB主机…这是非常重要的,因为并非所有的设备).您需要使用标准的android USB协议栈.然后,您可以从USBDevice获取设备描述符,并将其传递给libusb.

不幸的是,你还需要修改libusb.幸运的是,其他人已经解释了如何修改LibUSB.

LibUSB已修改here.

祝你好运!

编辑:

首先你需要定义一个广播接收器:

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() 
{
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) 
        {
            synchronized (this) 
            {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) 
                {
                    if(device != null)
                    {
                        UsbDeviceConnection deviceConnection    = mUsbManager.openDevice( device );
                        Log.d( "USB",  deviceConnection.getSerial() );
                    }
                } 
                else 
                {
                    Log.d( "USB", "permission denied for device " + device);
                }
            }
        }
    }
}

现在您需要创建一个USBManager并枚举这些设备:

mUsbManager         = (UsbManager) getSystemService( Context.USB_SERVICE );
    HashMap< String, UsbDevice > stringDeviceMap    =       mUsbManager.getDeviceList();
    Collection< UsbDevice > usbDevices              = stringDeviceMap.values();

    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent( ACTION_USB_PERMISSION ), 0 );
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver( mUsbReceiver, filter );

    Iterator< UsbDevice > usbDeviceIter             = usbDevices.iterator();
    while( usbDeviceIter.hasNext() )
    {
        if ( USBDeviceValid( usbDevice ) )
        {
            // Request permission to access the device.
            mUsbManager.requestPermission( usbDevice, mPermissionIntent );

            // Open the device.
            UsbDeviceConnection connection = mUsbManager.openDevice( usbDevice );
            int fd = connection.getFileDescriptor();

            // Now pass the file descriptor to libusb through a native call.
        }
    }

编辑2:

获取libusb来构建只是将文件放在某个地方(我将它们放在jni / libusb中),然后在Android.mk中添加以下行:

include $(CLEAR_VARS)
LOCAL_MODULE    := libusb
LOCAL_SRC_FILES := libusb/core.c libusb/descriptor.c libusb/io.c libusb/sync.c libusb/os/linux_usbfs.c

LOCAL_LDLIBS    := -llog
include $(BUILD_SHARED_LIBRARY)
相关文章
相关标签/搜索