回答編集履歴

2

追加

2023/11/06 14:19

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -140,3 +140,14 @@
140
140
  ![Make Module](https://ddjkaamml8q8x.cloudfront.net/questions/2023-11-06/2135ecbf-7d60-4863-89f9-93a9257e7858.png)
141
141
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-11-06/02189b9a-11cb-4a26-8cdf-036e69842f58.png)
142
142
  "-debug" と付いているのは build が debug だからです。 release にするには普通のアプリと同様に認証情報を入れないといけないかもしれません。
143
+
144
+ app 側 (テストアプリ) の AndroidManifest.xml の activity タグ (name=".MainActivity") 内に質問本文と同様に
145
+ ```xml
146
+ <intent-filter>
147
+ <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
148
+ </intent-filter>
149
+ <meta-data
150
+ android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
151
+ android:resource="@xml/device_filter" />
152
+ ```
153
+ を入れ res/xml/device_filter.xml に手持ちの機器の情報を定義しましたら、選択確認後にテストアプリが起動できました。

1

AAR を作ってみた流れを追加

2023/11/06 13:58

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -8,3 +8,135 @@
8
8
  ```
9
9
 
10
10
  USBLib クラスは Activity じゃないですね。
11
+
12
+ ---
13
+ USBLib を AAR 化し、テスト用のアプリから使用するまでをやってみました。
14
+ 使っているのは "Android Studio Flamingo | 2022.2.1 Patch 1" です。
15
+ どこか一箇所でも「これやってない」みたいな所が見つかると良いのですが…。
16
+
17
+ new project で "Phone and Tablet" の "Empty Views Activity" を選んで [Next] 、Name="q_kuopgz3a19cr8a" 、 package="com.teratail.q_kuopgz3a19cr8a" としました。(名前はこちらの都合でこのようにしているだけです。)
18
+
19
+ 一通りプロジェクトの生成が終わったら、メニューから [New Module] を実行します。
20
+ ![New Module 選択](https://ddjkaamml8q8x.cloudfront.net/questions/2023-11-06/278f4fbd-29f4-4f43-a98c-edd1903a90fe.png)
21
+
22
+ 表示されたダイアログで "Android Library" を選択し、名前等を入力し [Finish]。
23
+ !["Create New Module" Dialog](https://ddjkaamml8q8x.cloudfront.net/questions/2023-11-06/20226496-c23a-41b4-b876-6bb121a81a6f.png)
24
+
25
+ "app" と同じ高さにモジュールの名前のフォルダ ("usblib") が出来ますので、 app と usblib のそれぞれにコードを置いていきます。(AndroidManifest.xml は触りません。)
26
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-11-06/790a5254-416f-4afc-bb10-59bd2e183760.png)
27
+
28
+ app 側 (テストアプリ)
29
+ MainActivity.java
30
+ ```java
31
+ package com.teratail.q_kuopgz3a19cr8a;
32
+
33
+ import androidx.appcompat.app.AppCompatActivity;
34
+
35
+ import android.os.Bundle;
36
+ import android.widget.TextView;
37
+
38
+ import com.teratail.usblib.USBLib;
39
+
40
+ public class MainActivity extends AppCompatActivity {
41
+ @Override
42
+ protected void onCreate(Bundle savedInstanceState) {
43
+ super.onCreate(savedInstanceState);
44
+ setContentView(R.layout.activity_main);
45
+
46
+ USBLib reader = new USBLib();
47
+ reader.setContext(this);
48
+
49
+ TextView textView = findViewById(R.id.textView);
50
+
51
+ findViewById(R.id.button).setOnClickListener(v -> {
52
+ String dump;
53
+ try {
54
+ dump = reader.GetSts();
55
+ } catch (Exception e) {
56
+ dump = e.getLocalizedMessage();
57
+ }
58
+ textView.setText(dump);
59
+ });
60
+ }
61
+ }
62
+ ```
63
+ res/layout/activity_main.xml
64
+ ```xml
65
+ <?xml version="1.0" encoding="utf-8"?>
66
+ <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
67
+ xmlns:app="http://schemas.android.com/apk/res-auto"
68
+ xmlns:tools="http://schemas.android.com/tools"
69
+ android:layout_width="match_parent"
70
+ android:layout_height="match_parent"
71
+ tools:context=".MainActivity">
72
+
73
+ <Button
74
+ android:id="@+id/button"
75
+ android:layout_width="wrap_content"
76
+ android:layout_height="wrap_content"
77
+ android:text="check"
78
+ app:layout_constraintBottom_toTopOf="@id/textView"
79
+ app:layout_constraintEnd_toEndOf="parent"
80
+ app:layout_constraintStart_toStartOf="parent"
81
+ app:layout_constraintTop_toTopOf="parent" />
82
+
83
+ <TextView
84
+ android:id="@+id/textView"
85
+ android:layout_width="wrap_content"
86
+ android:layout_height="wrap_content"
87
+ android:textSize="30dp"
88
+ app:layout_constraintBottom_toBottomOf="parent"
89
+ app:layout_constraintEnd_toEndOf="parent"
90
+ app:layout_constraintStart_toStartOf="parent"
91
+ app:layout_constraintTop_toBottomOf="@id/button" />
92
+
93
+ </androidx.constraintlayout.widget.ConstraintLayout>
94
+ ```
95
+
96
+ usblib 側 (ライブラリ)
97
+ USBLib.java
98
+ ```java
99
+ package com.teratail.usblib;
100
+
101
+ import android.content.Context;
102
+ import android.hardware.usb.*;
103
+
104
+ public class USBLib {
105
+ private Context myContext;
106
+
107
+ public int setContext(Context context) {
108
+ myContext = context;
109
+ return 0;
110
+ }
111
+
112
+ /* UnityPlayer クラスが無くてエラーになるためコメント化
113
+ public int setContext() {
114
+ int result = 0;
115
+ if (myContext == null) {
116
+ result = setContext(UnityPlayer.currentActivity.getApplicationContext());
117
+ }
118
+ return result;
119
+ }
120
+ */
121
+
122
+ public String GetSts() {
123
+ UsbManager manager = (UsbManager) myContext.getSystemService(Context.USB_SERVICE);
124
+ for (UsbDevice device : manager.getDeviceList().values()) {
125
+ return device.getDeviceName();
126
+ }
127
+ return "nothing";
128
+ }
129
+ }
130
+ ```
131
+
132
+ MainActiviry.java で USBLib のエラーが出るので、 "Gradle Scripts" の "build.gradle (Module :app)" 内の dependencies に以下を追加します。
133
+ ```
134
+ implementation project(path: ':usblib')
135
+ ```
136
+
137
+ これで、テストアプリを実行してボタンを押すと USB の最初の情報 ( もしくは "nothing") が表示されるはずです。( XperiaXZ3 Android10 で確認 )
138
+
139
+ Project で usblib を選択してメニューから [Make Module(~)] を実行すれば AAR ファイルが生成されました。
140
+ ![Make Module](https://ddjkaamml8q8x.cloudfront.net/questions/2023-11-06/2135ecbf-7d60-4863-89f9-93a9257e7858.png)
141
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-11-06/02189b9a-11cb-4a26-8cdf-036e69842f58.png)
142
+ "-debug" と付いているのは build が debug だからです。 release にするには普通のアプリと同様に認証情報を入れないといけないかもしれません。