实验目的:
STM32通过USB转串口向Android Device持续发送数据,并让其显示在Android Device的Editview界面上
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dai.usb_test">
<uses-feature android:name="android.hardware.usb.host"></uses-feature>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".UsbTestActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<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/usb_device_filter" />
</activity>
</application>
</manifest><strong>
</strong>
xml/usb_device_filter.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="6790" product-id="29987" />
</resources>
注:vendor-id和product-id都为十进制数值;
USB_Admin:
package com.example.dai.usb_test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
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.util.Log;
import android.widget.Toast;
/**
* Created by DAI on 2016/1/23.
* Author:Will Smith
* Email:15997135562@163.com
*
* Tip:
*before developing USB on Android,you should configure manifests
* accoriding to Android Developer
*/
public class USB_Admin {
private UsbManager usbManager;
private UsbDevice usbDevice;
private UsbInterface usbInterface;
private UsbEndpoint usbEndpointIn;
private UsbEndpoint usbEndpointOut;
private UsbDeviceConnection usbDeviceConnection;
private static int TIMEOUT = 5000;
/*自己编写的USB管理类的构造函数*/
public USB_Admin(Context context){
usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
Log.v("USBManager:","" + usbManager);
}
/*****************************USB相关的操作**********************************/
/*获得USB的usbManager*/
public UsbManager get_UsbManager(){
return usbManager;
}
/*获得