Writing USB driver for Android

https://stackoverflow.com/questions/12741162/writing-usb-driver-for-android


Q

I am developing an android application in which I need to connect to a USB device.

I have developed a similar application on windows and I have driver written in c.

I want to develop an USB driver for my android application.I want to know what things are neccessary to develop USB driver for android.

or

Can I reuse the code written in c by using ndk.

Thanks,


A

First, Android is actually just Linux, so if you are talking about "writing a USB driver for my Android application" you should rather say "writing a linux USB driver for your specific device".

Next, you will have to access your device in some way. What you will get when you have written the USB driver for linux is probably some file node in /dev/. I'm guessing you want to create a driver for a non-standard USB device (like a mouse/joystick/mass storage) for which Android does not provide a nice JAVA api? In that case you will have to write a native library (probably based on the c code you already have) and compile it with the NDK. The .so file you will get out of it can be packaged in you Android application, which can then use it to talk to your USB device.

So to sum it up:

usb device driver -> create a kernel module or embed a driver into your linux kernel: this is linux usb driver programming stuff, for which you should be able to find enough guides on the web. You should be able to reuse parts of your c code if you really created a windows usb driver.

native wrapper library to access your device (.so file, or .dll if you are used to windows terminology) -> create an NDK project that opens the right device node and correctly reads from/writes to your device.

android app -> include the .so file and access its native (c) methods through jni. You may be able to use tools like javah or swig to generate the jni code from your library's header files.

A

The previous answer assumes that you have root access to your device and is not suitable for mass deployment. Since api version 12 android has provided a usb interface that allows you to interact with certain devices using the low level control channels and usb request buffers.

There are two types of device supportedusb host devicesusb (android) accessory devices

host mode is typically only support well on tablets, handsets can and do support it but their ablity to sustain host mode is less and very dependent upon the voltage demands of the deviec in questions

accessory mode is odd as it extends usb standards to add an additional interchange that identifies an accessory as an "android" accessory

if the above modes are usable for you then they are by far the better option to using a low level c driver unless you havve complete control over all the devices it will be installed on to.

These are both java apis and can be found documented on the google android documentation site, under usb.



原文地址:https://www.cnblogs.com/ztguang/p/12644426.html