USB-OTG 接收设备返回信息

项目介绍

记录一下学习过程,以前没接触过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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值