我需要通过OTG线缆从接收器接收原始数据(可能是以二进制形式,特别是间隔,可能每5秒一次)。
我需要知道这是否可以在Android核心类或任何外部API / SDK的帮助下完成
注意:
以下是我的代码的一部分:
清单文件:
<activity android:name="com.hoho.android.usbserial.examples.DeviceListActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
</activity>
在/ res / xml
device_filter
我从代码中收到了这些详细信息。
<usb-device
vendor-id="1105"
product-id="5798"
class="2"
subclass="0"
protocol="0"
/>
源代码:
import android.hardware.usb.UsbConstants;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Iterator;
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.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Read_USB_Host_Receiver extends Activity {
Button btnDeviceList;
TextView txtResult;
int iBulk=0;
UsbManager usbManager;
UsbDeviceConnection conn;
BroadcastReceiver mUsbReceiver;
PendingIntent mPermissionIntent;
UsbDevice dev;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
public void run(UsbDevice sDevice) {
UsbDevice dev = sDevice;
txtResult.setText("1");
if (dev == null)
return;
txtResult.setText("2");
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
txtResult.setText("3");
if(usbManager.hasPermission(dev)) {
Log.d("USB", " Permission OK");
} else {
txtResult.setText("4");
Log.d("USB", " Permission faild");
usbManager.requestPermission(dev, mPermissionIntent);
if(usbManager.hasPermission(dev)) {
txtResult.setText("5");
Log.d("USB", "Now is Permission OK");
}
}
conn = usbManager.openDevice(dev);
Log.i("Interface Count: "+conn,""+dev.getInterfaceCount());
Log.i("Using ",""+ String.format("%04X:%04X", sDevice.getVendorId(),sDevice.getProductId()));
txtResult.setText("6");
new AsynReadUCBHostEndPoint().execute();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read__usb__host__receiver);
txtResult = (TextView)findViewById(R.id.textView1);
usbManager = (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 = usbManager.getDeviceList();
Log.i("deviceList size: ", ""+deviceList.size());
Toast.makeText(Read_USB_Host_Receiver.this, ""+deviceList.size(), Toast.LENGTH_LONG).show();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext()) {
UsbDevice dd = deviceIterator.next();
dev = dd;
if(usbManager.hasPermission(dev)) {
Log.d("USB", " Permission OK");
} else {
Log.d("USB", " Permission faild");
usbManager.requestPermission(dev, mPermissionIntent);
if(usbManager.hasPermission(dev)) {
Log.d("USB", "Now is Permission OK");
}
}
}
btnDeviceList = (Button)findViewById(R.id.btnDeviceList);
btnDeviceList.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
Log.i("deviceList size: ", ""+deviceList.size());
Toast.makeText(Read_USB_Host_Receiver.this, ""+deviceList.size(), Toast.LENGTH_LONG).show();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while(deviceIterator.hasNext()) {
UsbDevice dd = deviceIterator.next();
dev = dd;
if (dd.getVendorId()==1105) {
run(dd);
}
}
}
});
mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Log.i("HELLO", "BYE");
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null) {
//call method to set up device communication
Log.i("HELLO1", "BYE1");
}
} else {
// Log.d(TAG, "permission denied for device " + device);
}
}
}
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_read__usb__host__receiver, menu);
return true;
}
public void readUSBHostData() { }
class AsynReadUCBHostEndPoint extends AsyncTask<Void,Integer,String> {
ProgressDialog dialog;
String response, result;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
txtResult.setText("7");
super.onPreExecute();
dialog=new ProgressDialog(Read_USB_Host_Receiver.this);
dialog.setMessage("Reading endpoint...");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
for (int j=0; j < dev.getInterfaceCount(); j++) {
UsbInterface intf = dev.getInterface(j);
UsbEndpoint endpoint = null;
for (int i = 0; i < intf.getEndpointCount(); i++) {
if (intf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN) {
endpoint = intf.getEndpoint(i);
break;
}
}
if(endpoint != null) {
if(usbManager.hasPermission(dev)) {
Log.d("USB", " Permission OK");
} else {
Log.d("USB", " Permission faild");
usbManager.requestPermission(dev, mPermissionIntent);
if(usbManager.hasPermission(dev)) {
Log.d("USB", "Now is Permission OK");
}
}
conn = usbManager.openDevice(dev);
conn.releaseInterface(intf);
byte[] buffer = new byte[64];
if (conn!=null && conn.claimInterface(intf, true)) {
int retVal = conn.bulkTransfer(endpoint, buffer, 64, 5000);
Log.i("return of bluk:", "" + retVal);
while(retVal == 0) {
retVal = conn.bulkTransfer(endpoint, buffer, 64, 200);
Log.i("return of bluk in while loop:", "" + retVal);
}
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
txtResult.setText(result);
}
}
public void GetData(UsbDevice dev) {
for (int j=0; j < dev.getInterfaceCount(); j++) {
UsbInterface intf = dev.getInterface(j);
UsbEndpoint endpoint = null;
for (int i = 0; i < intf.getEndpointCount(); i++) {
if (intf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN) {
endpoint = intf.getEndpoint(i);
break;
}
}
if(endpoint != null) {
conn = usbManager.openDevice(dev);
UsbRequest request = new UsbRequest(); // create an URB
if (conn!=null) {
boolean initilzed = request.initialize(conn, endpoint);
if (!initilzed) {
Log.e("USB CONNECTION FAILED", "Request initialization failed for reading");
return;
}
while (true) {
int bufferMaxLength = endpoint.getMaxPacketSize();
ByteBuffer buffer = ByteBuffer.allocate(bufferMaxLength);
if (request.queue(buffer, bufferMaxLength) == true) {
if (conn.requestWait() == request) {
String result = new String(buffer.array());
Log.i("GELEN DATA : ", result);
}
}
}
}
}
}
}
}