gpt4 book ai didi

java - 如何将linux命令从一个设备发送到通过usb otg连接的另一台设备?

转载 作者:行者123 更新时间:2023-12-02 04:21:04 25 4
gpt4 key购买 nike

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

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

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

最佳答案

好吧,对不起,是我的错

这是我的 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);
}
}
}
};

关于java - 如何将linux命令从一个设备发送到通过usb otg连接的另一台设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56636853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com