首先我想说谢谢 和你这样的人在一起,我们很难理解这些事情 ....
我使用UsbRequest和Requestwiat来读取5字节的固定数据包大小数据,它对我来说非常有效(4.1.2),但不幸的是它没有(4.3和4.4)。 通过我的搜索,我发现更改了更新的版本>> 2.2,http://code.google.com/p/android/issues/detail?id=28023
与我的问题有关吗? 这是我的毕业设计,我不是那个专业的程序员,所以任何通知,想法或建议都会帮助我开始搜索。
阿齐兹
问候
公共类UsbSerialService扩展了服务{
static LinkedBlockingQueue<Double> serialQueueForUI = new LinkedBlockingQueue<Double>();
public static UsbManager mUsbManager;
public static UsbDevice mUsbDevice;
private volatile UsbDeviceConnection mUsbConnection = null;
private volatile UsbEndpoint mInUsbEndpoint = null;
int bufferMaxLength=5;
public static Handler UIHandler;
private UsbRunnable mLoop;
private Thread mUsbThread;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void onCreate(){
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!initDevice()) {
stopSelf();
return Service.START_REDELIVER_INTENT;
}
mLoop = new UsbRunnable();
mUsbThread = new Thread(mLoop);
mUsbThread.start();
return START_STICKY;
@Override
public void onDestroy() {
mUsbDevice = null;
if (mUsbConnection != null) {
mUsbConnection.close();
}
super.onDestroy();
}
private class UsbRunnable implements Runnable{
public void run() {
{
ByteBuffer inBuffer = ByteBuffer.allocate(bufferMaxLength);
while(mUsbDevice != null )
{
UsbRequest inRequest = new UsbRequest();
inRequest.initialize(mUsbConnection, mInUsbEndpoint);
if(inRequest.queue(inBuffer, bufferMaxLength) == true)
{
mUsbConnection.requestWait();
// wait for this request to be completed
// at this point buffer contains the data received
UsbSerialService.updateReceivedData(inBuffer.array());
inBuffer.clear();
}
}
} }}
public static void updateReceivedData(final byte[] Data) {
List<Character> doubleBuilder = new ArrayList<Character>();
Double d =null;
char c;
for(int i=0;i<Data.length;i++)
{c = (char)Data[i];
doubleBuilder.add(c);}
char charArray[] = new char[doubleBuilder.size()];
for (int j=0; j < charArray.length; j++)
charArray[j] = doubleBuilder.get(j);
String stringVal = String.copyValueOf(charArray);
try {d = Double.valueOf(stringVal);
serialQueueForUI.offer(d);
}
catch (NumberFormatException e){}
doubleBuilder.clear();
}
private boolean initDevice() {
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mUsbConnection = mUsbManager.openDevice(mUsbDevice);
if (!mUsbConnection.claimInterface(mUsbDevice.getInterface(1), true))
{Toast.makeText(getBaseContext(), "opening_device_failed", Toast.LENGTH_LONG).show();
return false;
}
if (mUsbConnection == null) {
Toast.makeText(getBaseContext(), "opening_device_failed", Toast.LENGTH_LONG).show();
return false;
}
UsbInterface usbInterface = mUsbDevice.getInterface(1);
if (!mUsbConnection.claimInterface(usbInterface, true)) {
Toast.makeText(getBaseContext(), "claimning_interface_failed", Toast.LENGTH_LONG).show();
mUsbConnection.close();
return false;
}
// USB serial converter setup
// Set control line state
mUsbConnection.controlTransfer(0x21, 0x22, 0, 0, null, 0, 0);
// Set line encoding.
setBaudrate(115200);
for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
if (usbInterface.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (usbInterface.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN) {
mInUsbEndpoint = usbInterface.getEndpoint(i);
}
}
}
if (mInUsbEndpoint == null) {
Toast.makeText(getBaseContext(), "no_in_endpoint_found", Toast.LENGTH_LONG).show();
mUsbConnection.close();
return false;
}
return true;
}
public void setBaudrate(int baudrate) {
byte[] baudByte = new byte[4];
baudByte[0] = (byte) (baudrate & 0x000000FF);
baudByte[1] = (byte) ((baudrate & 0x0000FF00) >> 8);
baudByte[2] = (byte) ((baudrate & 0x00FF0000) >> 16);
baudByte[3] = (byte) ((baudrate & 0xFF000000) >> 24);
mUsbConnection.controlTransfer(0x21, 0x20, 0, 0, new byte[] {
baudByte[0], baudByte[1], baudByte[2], baudByte[3], 0x00, 0x00,
0x08}, 7, 15);
}
static void show(Context context, UsbDevice device) {
mUsbDevice = device;
final Intent intent = new Intent(context, UsbSerialService.class);
context.startService(intent);
}
}