我是android USB通讯新手。在我的项目中,android设备必须与外部设备通信。我跟着这个
http://stackoverflow.com/questions/16916950/android-rs232-communication
但不适合我。 所以我尝试了另一种方法
MainActivity是
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
try {
if (driver != null) {
driver.open();
Toast.makeText(this, "device opened", Toast.LENGTH_LONG).show();
try {
driver.setBaudRate(115200);
int byteRead = driver.write("#".getBytes(), 1);
Toast.makeText(this, "wrote bytes "+byteRead, Toast.LENGTH_LONG).show();
byte buffer[] = new byte[16];
int numBytesRead = driver.read(buffer, 1000);
Log.d("read operation", "Read " + numBytesRead + " bytes.");
} catch (IOException e) {
// Deal with error.
} finally {
driver.close();
}
}
else
Toast.makeText(this, "device is null", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Android清单文件是
<uses-sdk
android:minSdkVersion="12"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.usbandroid.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<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>
</application>
设备过滤器
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="0403" product-id="6001" />
</resources>
显示设备为空。我哪里出错?