如何将命令linux从一台设备发送到通过usb otg联网的另一台设备?

时间:2019-06-17 18:20:32

标签: java android device communication

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

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

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

1 个答案:

答案 0 :(得分:0)

好的,对不起我的错

这里我是我的MainActivity

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