我正在开发从USB CDC设备接收数据的Android应用程序。 以为我的android手机是主机。设备应该只从Android手机获得电源。用于数据传输和电源的电缆相同。
但此处数据未从USB CDC设备接收到移动设备。
以下是我试过的代码片段
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Iterator;
public class MainActivity extends Activity {
boolean Run= true;
boolean Communication_Ok;
Button btn;
UsbManager manager;
UsbDevice device;
UsbDeviceConnection connection;
UsbInterface Inf;
UsbEndpoint Data_In_End_Point =null;
UsbEndpoint Data_Out_End_Point =null;
ByteBuffer buffer = ByteBuffer.allocate(255);
UsbRequest request = new UsbRequest();
static int s1,s2,s3;
TextView tv1;
PendingIntent mPermissionIntent;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn);
tv1=(TextView)findViewById(R.id.tv1);
check_devices();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
call_next_Intent();
if (!Receive.isAlive())
Receive.start();
}
});
}
private void call_next_Intent() {
Intent Home=new Intent(MainActivity.this,Home.class);
startActivity(Home);
}
private void check_devices()
{
manager = (UsbManager) getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
HashMap<String , UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
String returnValue = "";
while (deviceIterator.hasNext()) {
device = deviceIterator.next();
manager.requestPermission(device, mPermissionIntent);
returnValue += "Name: " + device.getDeviceName();
returnValue += "\nID: " + device.getDeviceId();
returnValue += "\nProtocol: " + device.getDeviceProtocol();
returnValue += "\nClass: " + device.getDeviceClass();
returnValue += "\nSubclass: " + device.getDeviceSubclass();
returnValue += "\nProduct ID: " + device.getProductId();
returnValue += "\nVendor ID: " + device.getVendorId();
returnValue += "\nInterface count: " + device.getInterfaceCount();
for (int i = 0; i < device.getInterfaceCount(); i++) {
Inf = device.getInterface(i);
returnValue += "\n Interface " + i;
returnValue += "\n\tInterface ID: " + Inf.getId();
returnValue += "\n\tClass: " + Inf.getInterfaceClass();
returnValue += "\n\tProtocol: " + Inf.getInterfaceProtocol();
returnValue += "\n\tSubclass: " + Inf.getInterfaceSubclass();
returnValue += "\n\tEndpoint count: " + Inf.getEndpointCount();
for (int j = 0; j < Inf.getEndpointCount(); j++) {
returnValue += "\n\t Endpoint " + j;
returnValue += "\n\t\tAddress: " + Inf.getEndpoint(j).getAddress();
returnValue += "\n\t\tAttributes: " + Inf.getEndpoint(j).getAttributes();
returnValue += "\n\t\tDirection: " + Inf.getEndpoint(j).getDirection();
returnValue += "\n\t\tNumber: " + Inf.getEndpoint(j).getEndpointNumber();
returnValue += "\n\t\tInterval: " + Inf.getEndpoint(j).getInterval();
returnValue += "\n\t\tType: " + Inf.getEndpoint(j).getType();
returnValue += "\n\t\tMax packet size: " + Inf.getEndpoint(j).getMaxPacketSize();
}
}
}
tv1.setText(returnValue);
}
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action))
{
synchronized (this)
{
device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
{
if(device != null)
{
connection = manager.openDevice(device);
if (!connection.claimInterface(device.getInterface(0), true))
{
return;
}
connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
connection.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
connection.controlTransfer(0x21, 0x22, 0x1, 0, null, 0, 0);//DTR signal
connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
connection.controlTransfer(0x40, 0x03,0x001A, 0, null, 0, 0);//For baudrate
Inf = device.getInterface(0);
for (int i = 0; i < device.getInterfaceCount(); i++) {
// communications device class (CDC) type device
if (device.getInterface(i).getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
Inf = device.getInterface(i);
// find the endpoints
for (int j = 0; j < Inf.getEndpointCount(); j++) {
if (Inf.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT && Inf.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
// from android to device
Data_Out_End_Point = Inf.getEndpoint(j);
}
if (Inf.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN && Inf.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
// from device to android
Data_In_End_Point = Inf.getEndpoint(j);
}
}
}
}
}
}
else
{
Log.d("ERROR", "permission denied for device " + device);
}
}
}
}
};
public Thread Receive = new Thread(new Runnable()
{
@Override
public void run()
{
byte[] Recieved_Data;
int i;
buffer.clear();
while (Run){
request.initialize(connection, Data_In_End_Point);
request.queue(buffer,255);
if (connection.requestWait() !=null)
{
Recieved_Data=buffer.array();
if (Recieved_Data[0]==0x02 && Recieved_Data[2]==0x03){
if (Recieved_Data[1]==0x11){
s1++;
}
else if (Recieved_Data[1]==0x22){
s2++;
}
else if (Recieved_Data[1]==0x33){
s3++;
}
}
}
}
}
});
}