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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章