728x90

안드로이드 Serial 통신 관련 게시글은 https://developer.android.com/guide/topics/connectivity/usb/host 에 나와 있다.


When your Android-powered device is in USB host mode, it acts as the USB host, powers the bus, and enumerates connected USB devices. USB host mode is supported in Android 3.1 and higher.

안드로이드 전력 장치가 USB host 모드일 때, USB host로서 역할을 수행하고, 버스에 전력을 공급하고, 연결된 USB 장치를 열거한다. USB host 모드는 안드로이드 3.1 이상에서 지원된다.


"android.hardware.usb" 패키지안에 USB host API들이 있다.

Class

 설 명

 UsbManager

 연결 USB 장치들을 열거하고 통신하도록 허용한다.

 UsbDevice

 연결된 USB 장치를 표시하고

 식별 정보, 인터페이스, endpoint에 접속하기 위한 메소드들을 포함한다.

 UsbInterface

 USB 장치의 인터페이스를 표시하고, 장치의 기능 셋이 정의되어 있다.

 장치는 하나 또는 이상의 통신 인터페이스를 갖고 있다.

 UsbEndpoint

 인터페이스 endpoint를 표시하며, 이 인터페이스에 대한 통신 채널이다.

 하나의 인터페이스는 하나 이상의 endpoint를 갖고 있으며,

 통상적으로 장치와 양방향통신을 위한 input, output endpint를 갖고 있다.

 UsbDeviceConnection

 장치에 연결을 표시하며, endpoint에 데이터를 전송한다.

 이 클래스는 데이터를 순방향, 역방향 동기/비동기 전송을 허용한다.

 UsbRequest

 UsbDeviceConnection을 통한 장치와의 통신을 위한 비동기 요청을 나타낸다.

 UsbConstants

 리눅스 커널의 linux/usb/ch9.h 에 정의되어져 있는 연관된 상수들이 정의되어져 있다.



How to Monitor USB Events on Android

https://github.com/yushulx/android-usb-monitor 를 받으면 Eclipse 기반으로 된 코드를 받게 된다.

이걸 Android Studio 에서 복사 및 붙여넣기 하면 된다.


Android Manifest.xml 파일 내용 수정

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tistory.link2me.usbdevice">

    <uses-feature android:name="android.hardware.usb.host" />

    <!-- android.hardware.usb.host 를 쓴다고 알려 줘야 된다. -->

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

                <!-- USB Device 가 연결될때의 Action이라고 알려준다 -->
            </intent-filter>
            <meta-data
                android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />

                <!-- USB Device 정보가 res\xml\device_filter.xml 에 있다고  알려 준다 -->
        </activity>
    </application>

</manifest> 


device_filter.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Application이 원하는 장치에 대해서만 통보를 받을 수 있다. -->
    <!-- 0x0403 FTDI -->
    <usb-device vendor-id="1027" product-id="24577" /> <!-- 0x6001: FT232R -->
    <usb-device vendor-id="1027" product-id="24592" /> <!-- 0x6010: FT2232H -->
    <usb-device vendor-id="1027" product-id="24593" /> <!-- 0x6011: FT4232H -->
    <usb-device vendor-id="1027" product-id="24596" /> <!-- 0x6014: FT232H -->
    <usb-device vendor-id="1027" product-id="24597" /> <!-- 0x6015: FT231X -->

    <!-- 0x2341 / Arduino -->
    <usb-device vendor-id="9025" />

    <!-- 0x16C0 / 0x0483: Teensyduino  -->
    <usb-device vendor-id="5824" product-id="1155" />

    <!-- 0x10C4 / 0xEAxx: Silabs CP210x -->
    <usb-device vendor-id="4292" product-id="60000" />
    <usb-device vendor-id="4292" product-id="60016" />
    <usb-device vendor-id="4292" product-id="60032" />
    <usb-device vendor-id="4292" product-id="60033" />
    <usb-device vendor-id="4292" product-id="60048" />

    <!-- 0x1a86 / 0x7523: Qinheng CH340 -->
    <usb-device vendor-id="6790" product-id="29987" />

    <!-- 0x067B / 0x2303: Prolific PL2303 -->
    <usb-device vendor-id="1659" product-id="8963" />
    <usb-device vendor-id="1659" product-id="8964" />
    <usb-device vendor-id="1659" product-id="41216" />

    <!-- 0x0d28 / 0x0204: ARM mbed -->
    <usb-device vendor-id="3368" product-id="516" />
</resources>


까지 하고 컴파일하면 USB 케이블 장치를 자동 인식한다.

여기까지 하면 USB 장치가 인식되는 걸 확인할 수 있다.

블로그 이미지

Link2Me

,