[英]Send data over USB to Android app
我有一個Java應用程序通過USB向Android設備發送字節,問題是它似乎沒有在Android端讀取任何字節
這是信息的發送方式
private static void writeAndRead() throws LibUsbException {
String question = "Hello Android I'll be your host today, how are you?";
byte[] questionBuffer = question.getBytes();
ByteBuffer questionData = BufferUtils.allocateByteBuffer(questionBuffer.length);
IntBuffer transferred = IntBuffer.allocate(1);
int result = 0;
System.out.println("Sending question: " + question);
System.out.println("Length of buffer: " + questionBuffer.length);
result = LibUsb.bulkTransfer(handle, END_POINT_OUT_ACC, questionData, transferred, 5000);
if(result < 0) {
throw new LibUsbException("Bulk write error!", result);
}
}
此方法似乎已完成,並且沒有引發異常,並且報告已發送了51的緩沖區長度。
在Android方面,我有以下內容
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private UsbManager manager;
private UsbAccessory accessory;
private ParcelFileDescriptor accessoryFileDescriptor;
private FileInputStream accessoryInput;
private FileOutputStream accessoryOutput;
private String question = "";
private TextView questionTV;
Runnable updateUI = new Runnable() {
@Override
public void run() {
questionTV.append(question);
}
};
Runnable readQuestionTask = new Runnable() {
@Override
public void run() {
byte[] buffer = new byte[51];
int ret;
try {
ret = accessoryInput.read(buffer);
Log.d(TAG, "Read: " +ret);
if (ret == 51) {
String msg = new String(buffer);
question += " Question: " + msg;
} else {
question += " Read error";
}
Log.d(TAG, question);
} catch (IOException e) {
Log.e(TAG, "Read error ", e);
question += " Read error";
}
questionTV.post(updateUI);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
questionTV = (TextView) findViewById(R.id.hello_world);
questionTV.setMovementMethod(new ScrollingMovementMethod());
Intent intent = getIntent();
manager = (UsbManager) getSystemService(Context.USB_SERVICE);
accessory = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if(accessory == null) {
questionTV.append("Not started by the accessory itself"
+ System.getProperty("line.separator"));
return;
}
Log.d(TAG, accessory.toString());
accessoryFileDescriptor = manager.openAccessory(accessory);
Log.d(TAG, "File Descriptor " +accessoryFileDescriptor.toString());
if (accessoryFileDescriptor != null) {
FileDescriptor fd = accessoryFileDescriptor.getFileDescriptor();
accessoryInput = new FileInputStream(fd);
accessoryOutput = new FileOutputStream(fd);
}
new Thread(readQuestionTask).start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
以下部分代碼
try {
ret = accessoryInput.read(buffer);
Log.d(TAG, "Read: " +ret);
if (ret == 51) {
String msg = new String(buffer);
question += " Question: " + msg;
} else {
question += " Read error";
}
Log.d(TAG, question);
} catch (IOException e) {
Log.e(TAG, "Read error ", e);
question += " Read error";
}
在控制台中將question
字符串的內容報告為
09-18 09:58:30.084 26198-26221/com.example.paulstatham.testusb D/MainActivity﹕ Read: 51
09-18 09:58:30.084 26198-26221/com.example.paulstatham.testusb D/MainActivity﹕ Question: ������������������������������������������������������������������������������������������������������
活動本身只是將其顯示為“問題:”
我假設這些奇怪的字符只是字節數組首次分配時的默認字節值byte[] buffer = new byte[51];
不幸的是,該應用程序的性質使其很難調試。
關於為什么ret = accessoryInput.read(buffer);
任何建議ret = accessoryInput.read(buffer);
似乎什么都沒讀?
編輯:實際上,它正在讀取一些內容,如accessoryInput.read(buffer);
將阻塞,直到有輸入為止,並且沒有阻塞。
那么,為什么byte[]
充滿了垃圾呢?
我認為這可能與發送ByteBuffer的事實有關
LibUsb.bulkTransfer(handle, END_POINT_OUT_ACC, questionData, transferred, 5000);
但是我嘗試在Android中進行轉換
ByteBuffer buf = ByteBuffer.wrap(buffer);
String msg = new String(buf.array());
question += " Question: " + msg;
它給了我同樣的垃圾人物
再次回答我自己的問題,問題就在這里
String question = "Hello Android I'll be your host today, how are you?";
byte[] questionBuffer = question.getBytes();
ByteBuffer questionData = BufferUtils.allocateByteBuffer(questionBuffer.length);
IntBuffer transferred = IntBuffer.allocate(1);
我從未真正將byte[]
放入ByteBuffer
questionData.put(questionBuffer)
解決了這個問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.