【android】usb-serial-for-android その1
usb-serial-for-android
その名の通り、タブレットやスマートフォンのUSB端子に、OTGのUSBケーブルを介して、シリアル通信を実現する。具体的には、Arduino 等とつなぐ事ができる。
これつかって、PCデスク回りのIoT化をしてやろうと思いつきプロジェクトを開始。照明のON/OFFや2ディスプレイのHDMIのセレクタ、USBのセレクタをアンドロイドの画面をタッチしてコントロール。同じへ部屋のエアコンなんかも操作できる(赤外線LED使って・)とか妄想段階。
アンドロイドのシリアル通信ライブラリー各種
シリアル通信を実現するためのライブラリーとしてはいくつか調べると出てくる。
・UsbSerial
https://github.com/felHR85/UsbSerial
・PhysicaloidLibrary
https://github.com/ksksue/PhysicaloidLibrary
・usb-serial-for-android
https://github.com/mik3y/usb-serial-for-android
ここでは、usb-serial-for-androidが情報量が多そうだったので、選択。
基本、ReadMeに書いてある通りにすればOKですが、ハマるところをメモしておく。dependencies {
implementation 'com.github.mik3y:usb-serial-for-android:3.8.1'
implementation libs.appcompat
implementation libs.material
implementation libs.activity
implementation libs.constraintlayout
testImplementation libs.junit
androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core
}
デバイスフィルター
device_filter.xml
をダウンロードしてきたzipから取り出して、res/xmlの下に格納する。
このリストにあるIDを参照して、繋ぐことになる。大抵載っているので問題はないと思うが、VID、PID(ベンダーIDとプロダクトID)これは、ArduinoIDEで調べられるので、やってはいないが、追加すればどんなマイコンでもいけそう。USBの動作の設定
AndroidManifest.xml.を開いて
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Serial_test"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<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/device_filter" />
</activity>
</application>
</manifest>
<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/device_filter" />
コメント
コメントを投稿