简体   繁体   English

为Android编写USB驱动程序

[英]Writing USB driver for Android

I am developing an android application in which I need to connect to a USB device. 我正在开发一个Android应用程序,我需要连接到USB设备。

I have developed a similar application on windows and I have driver written in c. 我在Windows上开发了类似的应用程序,我用c编写了驱动程序。

I want to develop an USB driver for my android application. 我想为我的Android应用程序开发一个USB驱动程序。 I want to know what things are neccessary to develop USB driver for android. 我想知道为Android开发USB驱动程序需要什么。

or 要么

Can I reuse the code written in c by using ndk. 我可以使用ndk重用c中编写的代码吗?

Thanks, 谢谢,

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". 首先,Android实际上只是Linux,所以如果你在谈论“为我的Android应用程序编写USB驱动程序”,你应该说“为你的特定设备编写一个linux USB驱动程序”。

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/. 为linux编写USB驱动程序后,您将得到的可能是/ 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? 我猜你想要为非标准的USB设备(如鼠标/操纵杆/大容量存储设备)创建一个驱动程序,Android没有提供一个漂亮的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. 在这种情况下,您将不得不编写一个本机库(可能基于您已有的C代码)并使用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文件可以打包到您的Android应用程序中,然后可以使用它与您的USB设备进行通信。

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. usb设备驱动程序 - >创建一个内核模块或将驱动程序嵌入你的linux内核:这是linux usb驱动程序编程的东西,你应该能够在网上找到足够的指南。 You should be able to reuse parts of your c code if you really created a windows usb driver. 如果你真的创建了一个windows usb驱动程序,你应该可以重用c代码的一部分。

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. 用于访问设备的本机包装器库(.so文件,或者.dll,如果您习惯使用Windows术语) - >创建一个NDK项目,打开正确的设备节点并正确读取/写入您的设备。

android app -> include the .so file and access its native (c) methods through jni. android app - >包含.so文件并通过jni访问其原生(c)方法。 You may be able to use tools like javah or swig to generate the jni code from your library's header files. 您可以使用javah或swig等工具从库的头文件生成jni代码。

The previous answer assumes that you have root access to your device and is not suitable for mass deployment. 上一个答案假定您具有对设备的root访问权限,并且不适合大规模部署。 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. 由于api版本12 android提供了一个usb接口,允许您使用低级别控制通道和USB请求缓冲区与某些设备进行交互。

There are two types of device supported usb host devices usb (android) accessory devices 有两种类型的设备支持usb主机设备usb(android)附件设备

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 主机模式通常只支持平板电脑,手机可以并且确实支持它但是它们维持主机模式的能力较低且非常依赖于deviec在问题中的电压需求

accessory mode is odd as it extends usb standards to add an additional interchange that identifies an accessory as an "android" accessory 附件模式是奇怪的,因为它扩展了usb标准以添加额外的交换,将附件标识为“android”附件

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. 如果上述模式对您有用,那么它们是使用低级别c驱动程序的最佳选择,除非您对将要安装的所有设备进行完全控制。

These are both java apis and can be found documented on the google android documentation site, under usb. 这些都是java apis,可以在usb下的google android文档站点上找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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