连接.控制转移的含义



我正在开发一款能够通过串口与mbed通信的Android应用程序。但我不知道如何使用connection.controlTransfer,它目前返回负值。这个方法应该设置什么值?我知道这是为了设置android和mbed之间的配置,但是我不明白每个值的含义。请给我一些建议!

代码

安卓应用程序的代码

package com.example.pilot_display;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbDeviceConnection;
import android.os.Bundle;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbEndpoint;
import android.os.Handler;
import android.os.Looper;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
UsbDevice myusbdevice;
UsbManager myusbmanager;
UsbDeviceConnection connection;
UsbEndpoint endpoint;
UsbInterface intf;
UsbEndpoint readEndpoint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onResume() {
super.onResume();
myusbmanager = (UsbManager) getSystemService(USB_SERVICE);
TextView checker = (TextView) findViewById(R.id.check);
if (ConnectCheck(checker)) {
myusbdevice = (UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (myusbmanager.hasPermission(myusbdevice)) {
intf = myusbdevice.getInterface(0);
for (int i = 0; i < intf.getEndpointCount(); i++) {
endpoint = intf.getEndpoint(i);
if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
readEndpoint = endpoint;
}
}
}
connection = myusbmanager.openDevice(myusbdevice);
if (connection.claimInterface(intf, true)) {

int baudrate = 9600;
byte[] msg = new byte[]{(byte)(baudrate & 255), (byte)(baudrate >> 8 & 255), (byte)(baudrate >> 16 & 255), (byte)(baudrate >> 24 & 255), (byte)0, (byte)0, (byte)8};
connection.controlTransfer(33, 32, 0, 0, msg, msg.length, 5000);
//this is the very part that I can't understand!

new Thread(new Runnable() {
@Override
public void run() {
while(true) {
final byte[] buffer = new byte[30];
int bytescounts = connection.bulkTransfer(readEndpoint, buffer, 30, 5000);
if (bytescounts >= 0) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
onRead();
}
});
}
}
}
}).start();
}
}
}
}
public void onPause() {
super.onPause();
myusbmanager = null;
}
public void onRead(){
Toast toast = Toast.makeText(MainActivity.this,"Data received!",Toast.LENGTH_LONG);
toast.show();
}
private boolean ConnectCheck(TextView checker){
HashMap<String, UsbDevice> deviceList = myusbmanager.getDeviceList();
if(deviceList == null || deviceList.isEmpty()){
checker.setText("unconnected");
checker.setTextColor(Color.RED);
return false;
}
else{
checker.setText("connected");
checker.setTextColor(Color.GREEN);
return true;
}
}
}

ARM mbed实现CDC-ACM USB协议。您可以使用usb-serial-for-android这样的库,通过简单的串行接口API访问usb设备,而不是自己实现。

最新更新