简体   繁体   English

如何从 Android 上的 USB 设备读取数据?

[英]How to read data from usb device on Android?

I have a device which I need to connect via usb to my android phone.我有一个设备需要通过 USB 连接到我的 android 手机。 I simply want to read some data this device is sending and present it on my app screen.我只想读取此设备发送的一些数据并将其显示在我的应用程序屏幕上。 I've tried some API's such https://github.com/felHR85/UsbSerial and https://github.com/mik3y/usb-serial-for-android where I found the code below我已经尝试了一些 API,例如https://github.com/felHR85/UsbSerialhttps://github.com/mik3y/usb-serial-for-android我在下面找到了代码

    public class MainActivity extends AppCompatActivity {

    private TextView data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        data = findViewById(R.id.data);

        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
        if (availableDrivers.isEmpty()) {
            return;
        }

// Open a connection to the first available driver.
        UsbSerialDriver driver = availableDrivers.get(0);
        UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
        if (connection == null) {
            // You probably need to call UsbManager.requestPermission(driver.getDevice(), ..)
            return;
        }

// Read some data! Most have just one port (port 0).
        UsbSerialPort port = driver.getPorts().get(0);
        try {
            port.open(connection);
            port.setParameters(57600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);

            byte buffer[] = new byte[8];
            int numBytesRead = port.read(buffer, 1000);
            //Log.d(TAG, "Read " + numBytesRead + " bytes.");
            data.setText(numBytesRead);
        } catch (IOException e) {
            // Deal with error.
        } finally {
            try {
                port.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

It simply does not read anything at all, is there a simpler way to do that?它根本不读取任何内容,有没有更简单的方法来做到这一点?

The device I'm trying to read from is an oximeter (Ut-100).我试图读取的设备是血氧计 (Ut-100)。 I only need some data to show up on the screen, is there a simple way to simple return an array of bytes and handle it by myself?我只需要一些数据显示在屏幕上,有没有一种简单的方法可以简单地返回一个字节数组并自己处理它? Thanks for any help谢谢你的帮助

You can start with Android Developer documentation USB HOST MODE to understand how to communicate over USB. 您可以从Android Developer文档USB HOST MODE开始,以了解如何通过USB进行通信。

You should start with the section USB host overview . 您应该从“ USB主机概述 ”部分开始。

Typically the UsbManager.requestPermission(...) handling is missing.通常缺少 UsbManager.requestPermission(...) 处理。

Please look at the recently enhanced examples @ https://github.com/mik3y/usb-serial-for-android请看最近增强的例子@https ://github.com/mik3y/usb-serial-for-android

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

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