Send data through USB from Android App to PC

前端 未结 1 1141
-上瘾入骨i
-上瘾入骨i 2020-12-15 00:07

I would like to create an app to send data through USB from Android-app to PC. My code is as follows :

package com.sample.dummy.app.senddatathoughserialport;         


        
1条回答
  •  一个人的身影
    2020-12-15 00:09

    In our Company we use the following code to open a device with specific ID. Also It only works on OTG compatible devices. In your Manifest you don't need USB_DETACHED, as the intent-filters in manifest are meant to open/launch you app, unless if you want to open your apk when usb detach. Just call ConnectUSB on click;

        UsbInterface usbInterface;
        UsbEndpoint usbEndpointIN, usbEndpointOUT;
        UsbDeviceConnection usbDeviceConnection;
        UsbDevice deviceFound = null;
        USB USB;
    
        ArrayList listInterface;
        ArrayList listUsbInterface;
        ArrayList listEndPoint;
        ArrayList listUsbEndpoint;
    
        private static final int targetVendorID = 8260;
        private static final int ProductID = 1000;    
    
            private boolean connectUsb() {
                boolean result = false;     
                switch(checkDeviceInfo())
                {
                    case ProductID:
                            result = StartUSB();
                        break;
                }
                return result;
            }
    
            private int checkDeviceInfo() {
    
                deviceFound = null;
    
                UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
                HashMap deviceList = manager.getDeviceList();
                Iterator deviceIterator = deviceList.values().iterator();
    
                while (deviceIterator.hasNext()) {
                    UsbDevice device = deviceIterator.next();
    
                    if(device.getVendorId()==targetVendorID) {
                        deviceFound = device;
    
                        switch(device.getProductId()) {
                            case ProductID:
                                GetInterface(deviceFound);
                                GetEndpoint(deviceFound);
                                return ProductID;
    
                            default:
                                Toast.makeText(this, getString(R.string.err_unknow_device), Toast.LENGTH_LONG).show();
                                break;
                        }
                        return -1;
                    }
                }
                return -1;
            }
    
            private void GetInterface(UsbDevice d) {
                listInterface = new ArrayList();
                listUsbInterface = new ArrayList();
                for(int i=0; i 0) 
                {   
                    usbInterface = listUsbInterface.get(1);
                }
                else usbInterface = null;
            }
    
            private void GetEndpoint(UsbDevice d) {
                int EndpointCount = usbInterface.getEndpointCount();    
                listEndPoint = new ArrayList();
                listUsbEndpoint = new ArrayList();
    
                for(int i=0; i 0) {
                    usbEndpointIN = usbInterface.getEndpoint(0);
                    usbEndpointOUT = usbInterface.getEndpoint(1);
                }           
                else {
                    usbEndpointIN = null;
                    usbEndpointOUT = null;
                }
            }
    
            private boolean StartUSB() {
                boolean result = false;
                UsbDevice deviceToRead = deviceFound;
                UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    
                Boolean permitToRead = manager.hasPermission(deviceToRead);
    
                if(permitToRead) {
                    result = OpenDevice(deviceToRead);
                }else {
                    Toast.makeText(this, 
                            getString(R.string.err_no_permission) + permitToRead, 
                            Toast.LENGTH_LONG).show();
                }
    
                return result;
            }
    
            private boolean OpenDevice(UsbDevice device){
    
                boolean forceClaim = true;
    
                UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
                usbDeviceConnection = manager.openDevice(device);
    
                if(usbDeviceConnection != null){
                    usbDeviceConnection.claimInterface(usbInterface, forceClaim);
                    return true; 
                }else{
                    Toast.makeText(this, 
                            getString(R.string.err_no_open_device), 
                            Toast.LENGTH_LONG).show();
                }
    
                return false;
            }
    

    0 讨论(0)
提交回复
热议问题