在连接的USB中查找并打开文件

时间:2018-01-04 11:34:05

标签: java android usb android-usb

所以逻辑是:

  • 用户将USB连接到Android设备。
  • 用户按“查找更新”按钮。
  • 应用程序连接到USB并在主文件夹中找到任何.apk文件。
  • 该应用程序打开USB中的.apk文件。
  • .apk运行并更新当前的应用程序。

到目前为止我所拥有的:

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();
            }
        }
    });

这段代码在控制台中返回的是一个转换为String的缓冲区(字节)。

我想知道如何找到一个文件,让我们说app.apk并打开它来实际更新当前的应用程序(按下按钮的位置)。

1 个答案:

答案 0 :(得分:0)

我终于想出了一个解决方案:

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();
            }
        }
    });