javaandroidusbandroid-usb

Find and open file in a connected USB


So the logic is:

What I have so far:

findUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean foundUsb = false;
            usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
            deviceList = usbManager.getDeviceList();
            deviceIterator = deviceList.values().iterator();
            Log.d("UPDATER", "Pressed");
            while(deviceIterator.hasNext()){

                UsbDevice device = deviceIterator.next();
                Log.d("UPDATER", "Device Attached: " + device.getManufacturerName());

                if (!Objects.equals(device.getManufacturerName(), gv.getManufacturerIDPlaca())) {
                    Log.d("UPDATER", device.getManufacturerName());
                    foundUsb = true;
                    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(SetupActivity.this, 0, new Intent(ACTION_USB_PERMISSION), 0);

                    if (!usbManager.hasPermission(device)) {
                        Log.d("UPDATER", "No access");
                        usbManager.requestPermission(device, mPermissionIntent);
                        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
                    }

                    Log.d("UPDATER", "Has access");

                    usbDevice = device;
                    usbInterface = usbDevice.getInterface(0);
                    usbEndpoint = usbInterface.getEndpoint(0);
                    usbDeviceConnection = usbManager.openDevice(usbDevice);
                    packetSize = usbEndpoint.getMaxPacketSize();

                    new Thread(new Runnable() {
                        public void run() {
                            final byte[] buffer = new byte[packetSize];
                            usbDeviceConnection.claimInterface(usbInterface, forceClaim);
                            usbDeviceConnection.bulkTransfer(usbEndpoint, buffer, packetSize, TIMEOUT);
                            Log.d("UPDATER", String.valueOf(buffer));
                        }
                    }).start();
                }
            }

            if (!foundUsb) {
                Toast.makeText(context, "No USB detected", Toast.LENGTH_SHORT).show();
            }
        }
    });

What this code returns me in console is a buffer (of bytes) converted to a String.

I'm wondering how could I find a file let's say app.apk and open it to actually update the current app (where the button is pressed from).


Solution

  • I finally came up with a solution:

    findUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean foundUsb = false;
                usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
                deviceList = usbManager.getDeviceList();
                deviceIterator = deviceList.values().iterator();
                while(deviceIterator.hasNext()){
    
                    UsbDevice device = deviceIterator.next();
                    Log.d("UPDATER", "Device Attached: " + device.getManufacturerName());
    
                    if (!Objects.equals(device.getManufacturerName(), gv.getManufacturerIDPlaca())) {
                        foundUsb = true;
                        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(SetupActivity.this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    
                        if (!usbManager.hasPermission(device)) {
                            usbManager.requestPermission(device, mPermissionIntent);
                            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
                        }
    
                        try {
                            File file = new File("/storage/usbhost/update-binary.apk");
                            if (file.exists()) {
                                Intent newIntent = new Intent(Intent.ACTION_VIEW);
                                newIntent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                                newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(newIntent);
                            }
                            else {
                                Toast.makeText(getApplicationContext(), R.string.UPDATE_APK_NO_ENCONTRADA, Toast.LENGTH_LONG).show();
                            }
    
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                        }
                    }
                }
    
                if (!foundUsb) {
                    Toast.makeText(context, R.string.USB_NO_CONECTADO, Toast.LENGTH_SHORT).show();
                }
            }
        });