如何从Android上的USB设备读取数据? [英] How to read data from usb device on Android?

查看:1392
本文介绍了如何从Android上的USB设备读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设备,需要通过USB连接到我的Android手机。我只是想读取此设备正在发送的一些数据,并将其显示在我的应用程序屏幕上。
我已经尝试过一些> https://github.com/felHR85/UsbSerial 这样的API。和 https://github.com/mik3y/usb-serial-for-android在下面找到代码的地方

I have a device which I need to connect via usb to my android phone. 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

    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?

我要读取的设备是血氧仪(Ut-100)。我只需要一些数据就可以显示在屏幕上,是否有一种简单的方法可以简单地返回一个字节数组并由我自己处理呢?感谢您的帮助

The device I'm trying to read from is an oximeter (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

推荐答案

通常缺少UsbManager.requestPermission(...)处理。

Typically the UsbManager.requestPermission(...) handling is missing.

请查看最近增强的示例@ https://github.com/mik3y/usb-serial-for-android

Please look at the recently enhanced examples @ https://github.com/mik3y/usb-serial-for-android

这篇关于如何从Android上的USB设备读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆