通过USB从Android应用程序发送和接收数据

时间:2014-08-19 07:03:29

标签: android usb

我正在尝试通过usb将PIC微处理器连接到平板电脑应用程序。发送按钮应触发向PIC发送包含"$$$"的字符串。通过消除过程,它似乎可以解决为" requestWait"在底部的线。按钮保持突出显示,应用程序似乎挂起。我怀疑requestWait没有得到答案,所以它无限期地等待。我将不胜感激。我查看了AdbTest和Missile Launcher应用程序,但是由于我的Android专业知识水平,它们的内容太多了。

稍后添加:

查看UsbRequest queue(..)result = native_queue_direct(...)

的代码

我认为这是本机代码。我在哪里可以找到它?

import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Iterator;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.view.View;
import android.widget.Toast;

    @Override
    protected void onResume() {
        super.onResume();

        mManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        // check for existing devices
        UsbDevice device;

        HashMap<String, UsbDevice> deviceList = mManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while (deviceIterator.hasNext()) {
            device = deviceIterator.next();
            if (device.getVendorId() == 1027) {
                UsbInterface intf = device.getInterface(0);
                if (setUsbInterface(device, intf)) {
                    break;
                }
            }
        }

        private UsbManager          mManager;
        private UsbDevice           mDevice;
        private UsbDeviceConnection mDeviceConnection;
        private UsbInterface        mInterface;

        private ScorerDevice        mScorerDevice ;

        // ***********************************

        // Sets the current USB device and interface
        private boolean setUsbInterface(UsbDevice device, UsbInterface intf) {
            if (mDeviceConnection != null) {
                if (mInterface != null) {
                    mDeviceConnection.releaseInterface(mInterface);
                    mInterface = null;
                }
                mDeviceConnection.close();
                mDevice = null;
                mDeviceConnection = null;
            }

            if (device != null && intf != null) {
                UsbDeviceConnection connection = mManager.openDevice(device);
                if (connection != null) {
                    if (connection.claimInterface(intf, true)) {

                        toast = Toast.makeText(getApplicationContext(), "claim interface succeeded", Toast.LENGTH_SHORT);
                        toast.show();

                        mDevice = device;
                        mDeviceConnection = connection;
                        mInterface = intf;
                        mScorerDevice = new ScorerDevice(this, mDeviceConnection, intf);

                        toast = Toast.makeText(getApplicationContext(), "USB started", Toast.LENGTH_SHORT);
                        toast.show();

                        mScorerDevice.start();
                        return true;
                    } else {

                        toast = Toast.makeText(getApplicationContext(), "claim interface failed", Toast.LENGTH_SHORT);
                        toast.show();

                        connection.close();
                    }
                } else {

                    toast = Toast.makeText(getApplicationContext(), "USB failed", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }

            if (mDeviceConnection == null && mScorerDevice != null) {
                mScorerDevice.stop();
                mScorerDevice = null;
            }
            return false;
        }

        // ***********************************

        public int onClick_Send(View view) {

            String dollars = "$$$";
            byte[] bytes = dollars.getBytes();

            int len = bytes.length;
            int offset = 0;

            int PacketSize = mScorerDevice.mEndpointOut.getMaxPacketSize();

            if ((len > PacketSize) || (offset < 0) || (len < 0) || ((offset + len) > bytes.length))
                throw new IndexOutOfBoundsException();

            ByteBuffer sendBuffer = ByteBuffer.allocate(PacketSize);

            UsbRequest request = new UsbRequest();
            Boolean ret = request.initialize(mDeviceConnection, mScorerDevice.mEndpointOut);
            if (!ret ) {
                return -1;
            }

            ret = request.queue(sendBuffer, len);
            if (!ret ) {
                return -1;
            }

            request = mDeviceConnection.requestWait();
            if (request == null) {
                return -1;
            }

        }        

1 个答案:

答案 0 :(得分:1)

这在大多数设备上都是不可能的,因为它们不支持USB主机模式。有些设备具有 USB OTG支持或USB主机端口,但到目前为止这些设备都是比较少见的设备。

理论上你可以在Win / Linux上编写一个应用程序来通过USB访问手机,但不是相反。

但你可以Read This并尝试。

要与设备通信,请参阅 - How to establish communication between android device and usb.

愿它有助于......