Хорошо, извините, моя вина
Здесь я - моя основная деятельность
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();
}
}
Я хотел бы отправить команду linux (например, проверить IP-адрес), когда я подключил устройство через ust otg. Должен ли я выполнить команду здесь?
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);
}
}
}
};