如何将命令 Linux 从一个设备发送到另一个通过 usb otg 连接的设备



>我制作了应用程序,当通过ust otg连接在一起时,该应用程序应该将linux命令发送到另一台设备。

如何将我的命令发送到另一台设备?

我进行了通信,我可以阅读例如模型设备,现在我想发送命令以查看IP地址连接的设备

好的,对不起我的错

这里是我的主要活动

   private static final String TAG = "UsbEnumerator";
    private TextView mStatusView, mResultView, showCommand;
    private Button buttonRefresh, sendCommand;
    private UsbManager usbManager;
    private EditText writeCommand;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mStatusView = (TextView) findViewById(R.id.mStatusView);
        mResultView = (TextView) findViewById(R.id.mResultView);
        showCommand = (TextView) findViewById(R.id.showCommand);
        buttonRefresh = (Button) findViewById(R.id.buttonRefresh);
        sendCommand = (Button) findViewById(R.id.sendCommand);
        writeCommand = (EditText) findViewById(R.id.writeCommand);
        mResultView.setMovementMethod(new ScrollingMovementMethod());
        buttonRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, MainActivity.class);
                finish();
                overridePendingTransition(0,0);
                startActivity(i);
                overridePendingTransition(0,0);
            }
        });

        sendCommand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View t) {
                SendCommandExecutor exe = new SendCommandExecutor();
                String command = writeCommand.getText().toString();
                String outp = exe.Executer(command);
                showCommand.setText(outp);
                Log.d("Output", outp);
            }
        });
        usbManager = getSystemService(UsbManager.class);
        IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(mUsbReceiver, filter);
        handleIntent(getIntent());
    }
    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUsbReceiver);
    }
    BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (device != null) {
                    printStatus("Delete");
                    printDeviceDescription(device);
                }
            }
        }
    };
    private void handleIntent(Intent intent) {
        UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if (device != null) {
            printStatus("Adding");
            printDeviceDetails(device);
        } else {
            printStatus("Remove");
            printDeviceList();
        }
    }

我想在通过ust otg连接设备时发送linux命令(例如检查IP地址(。我应该在这里执行命令吗?

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (device != null) {
                    printStatus("Delete");
                    printDeviceDescription(device);
                }
            }
        }
    };

相关内容

最新更新