USB-OTG 接收设备返回信息
来源:互联网 发布:blythe淘宝哪家是正品 编辑:程序博客网 时间:2024/06/06 15:35
项目介绍
记录一下学习过程,以前没接触过USB-OTG相关的开发,半学半模仿的做个小DEMO,可以接收到设备的信息。
引用了GitHub上UsbSerialDriver库
地址为:https://github.com/mdjarv/UsbSerialDriver
DeviceListActivity用于刷新定时刷新列表,获取最新的设备。
DeviceInfoActivity用于显示设备的信息,以及接收到的数据。
详细代码
activity_device_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jove.demo.activity.DeviceListActivity"> <TextView android:id="@+id/tv_refresh" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="8dp" android:gravity="center" android:text="@string/refreshing" android:textSize="18sp" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="0dp"/> <ProgressBar android:id="@+id/progressBar" style="@android:style/Widget.Holo.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="8dp" android:indeterminate="true" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="0dp"/> <View android:id="@+id/separator" android:layout_width="match_parent" android:layout_height="1dip" android:background="#eeeeee"/> <android.support.v7.widget.RecyclerView android:id="@+id/deviceList" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
activity_device_info.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jove.demo.activity.DeviceInfoActivity"> <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:gravity="center" android:text="已连接设备,请双击设备上的IDLE按钮"/> <TextView android:id="@+id/tv_info" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" /></LinearLayout>
DeviceListActivity
public class DeviceListActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_device_list); initOTG(); initView(); } /** * 初始化UsbSerialDriver */ private void initOTG() { driver = new UsbSerialDriver(this, null); } /** * 初始化View */ private void initView() { mProgressBarTitle = (TextView) findViewById(R.id.tv_refresh); mProgressBar = (ProgressBar) findViewById(R.id.progressBar); lvDevice = (RecyclerView) findViewById(R.id.deviceList); lvDevice.setLayoutManager(new LinearLayoutManager(DeviceListActivity.this)); lvDevice.addItemDecoration( new DividerItemDecoration(DeviceListActivity.this, DividerItemDecoration.VERTICAL_LIST)); lvDevice.setItemAnimator(new SlideInOutRightItemAnimator(lvDevice)); lvDevice.setAdapter(new CommonAdapter<UsbDevice>(DeviceListActivity.this, R.layout.device_item, driver.getUsbDevices()) { @Override public void convert(ViewHolder holder, UsbDevice usbDevice) { holder.setText(R.id.tv_device, usbDevice.getDeviceName()); holder.setOnClickListener(R.id.lin_device, new OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(DeviceListActivity.this, DeviceInfoActivity.class); startActivity(intent); } }); } }); } /** * 刷新Device列表 */ private void refreshDeviceList() { showProgressBar(); new AsyncTask<Void, Void, List<UsbDevice>>() { @Override protected List<UsbDevice> doInBackground(Void... params) { SystemClock.sleep(1000); final List<UsbDevice> result = driver.getUsbDevices(); return result; } @Override protected void onPostExecute(List<UsbDevice> result) { lvDevice.getAdapter().notifyDataSetChanged(); mProgressBarTitle.setText( String.format("%s device(s) found", driver.getUsbDevices().size())); hideProgressBar(); } }.execute((Void) null); }
DeviceInfoActivity
public class DeviceInfoActivity extends Activity { private TextView tvDevice; private TextView tvInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_device_info); initView(); initData(); } private void initData() { UsbSerialDriver driver = new UsbSerialDriver(this, new IUsbConnectionHandler() { @Override public void onUsbDeviceConnected() { } @Override public void onUsbDeviceDisconnected() { } @Override public void onUsbDeviceMessage(final byte[] message, int responseSize) { DeviceInfoActivity.this.runOnUiThread(new Runnable() { @Override public void run() { tvInfo.setText("接收的数据为:\n" + ByteHelper.BytesToHexString(message).substring(0, 36)); } }); } }); driver.connect(driver.getUsbDevices().get(0)); } private void initView() { tvDevice = (TextView) findViewById(R.id.tv_text); tvInfo = (TextView) findViewById(R.id.tv_info); }}
主要功能都是有UsbSerialDriver库来实现的,回头仔细看一遍源码!
源码地址:http://download.csdn.net/detail/nhh0905/9844211
阅读全文
0 0
- USB-OTG 接收设备返回信息
- Android设备间USB传输(OTG)
- Android设备间USB传输(OTG)
- Android设备间USB传输(OTG)
- Android设备间USB传输(OTG)
- USB-OTG
- usb otg
- USB otg
- USB OTG
- USB OTG
- USB OTG
- USB OTG
- usb otg
- USB-OTG
- USB OTG
- 取USB设备信息
- 取USB设备信息
- 取USB设备信息
- 使用freemarker实现word导出
- 关于RelativeLayout中onMeasure()的理解
- 《Docker:容器与容器云》.pdf
- easyui datagrid一般创建模板
- Python: PS 图像调整--亮度调整
- USB-OTG 接收设备返回信息
- 高并发TrafficServer设置
- Git命令的理解和使用
- Mysql中MyISAM、InnoDB两种表类型的区别
- Java中引用变量与对象
- 插件记录,暂时没有时间写,先记录下来
- linux初学笔记
- 【LeetCode】字符串系列(Anagram)
- SQL语句in中使用子查询