質問編集履歴

7

IOデータ USB-RSAQ6 に修正。

2019/07/08 07:02

投稿

murakami-kurio
murakami-kurio

スコア15

test CHANGED
File without changes
test CHANGED
@@ -380,7 +380,7 @@
380
380
 
381
381
  ```
382
382
 
383
- 古くて情報がないもの。
383
+ IOデータ USB-RSAQ6
384
384
 
385
385
  usb-device vendor-id="1659" product-id="8963"
386
386
 

6

通信ができた旨、ご報告。

2019/07/08 07:02

投稿

murakami-kurio
murakami-kurio

スコア15

test CHANGED
File without changes
test CHANGED
@@ -456,7 +456,7 @@
456
456
 
457
457
 
458
458
 
459
- ### 送信ができました。(ご報告)
459
+ ### 送信ができました。(ご報告)(2019年6月中旬)
460
460
 
461
461
  usb-serial-for-androidを使わない方法で、送信ができました。
462
462
 
@@ -475,3 +475,19 @@
475
475
  しようと思います。
476
476
 
477
477
  よろしくお願いいたします。
478
+
479
+
480
+
481
+ ### 受信ができませんでした。(ご報告)(2019July04)
482
+
483
+ いろいろ、インターネットをさまよったのですが、
484
+
485
+ serialドライバーを使わない方法での受信ができませんでした。
486
+
487
+
488
+
489
+ ### FTDI社のFT232RL用ドライバーを使ったら、FT234Xは通信ができた。(2019July04)
490
+
491
+ [Fab蔵さんのページ:201. USB Host事前準備](https://sites.google.com/a/gclue.jp/fab-zang-docs/sumafo-lian-xie/2-android-arduino-rogu-chu-li) から続く内容を実行したら動きました。
492
+
493
+ これをもって、自己解決としたいと思います。

5

送信ができた旨、ご報告。

2019/07/04 02:39

投稿

murakami-kurio
murakami-kurio

スコア15

test CHANGED
File without changes
test CHANGED
@@ -453,3 +453,25 @@
453
453
  /dev/bus/001/026
454
454
 
455
455
  と出ました。
456
+
457
+
458
+
459
+ ### 送信ができました。(ご報告)
460
+
461
+ usb-serial-for-androidを使わない方法で、送信ができました。
462
+
463
+ [AndroidのUSBホスト(OTG)機能を使って、接続されたArduinoとシリアル通信してみる話](https://relativelayout.hatenablog.com/entry/2016/08/12/085051)
464
+
465
+ を参考にしました。(ぐぐったり、null pointerに悩まされながら)
466
+
467
+ 受信は、
468
+
469
+ [自作のUSBデバイスを、Androidで動かす](http://blog.kshoji.jp/2012/12/android-usb-host.html)
470
+
471
+ を参考にやろうと思います。
472
+
473
+ 両方できたら、「自己解決(いろいろお世話になっていますが...)」と
474
+
475
+ しようと思います。
476
+
477
+ よろしくお願いいたします。

4

試したことがソースの変更に伴い変わっていたので、変えた。

2019/06/14 08:25

投稿

murakami-kurio
murakami-kurio

スコア15

test CHANGED
File without changes
test CHANGED
@@ -332,9 +332,17 @@
332
332
 
333
333
  ### 試したこと
334
334
 
335
+
336
+
337
+ シリアルの端末がつながると、counter_textに1が入るはずなのに、
338
+
339
+ counter_text2に1が入る。
340
+
341
+
342
+
335
- シリアルの端末がつながると、スイッチの反応がなくなるはずなのに、
343
+ (古いソースでは、スイッチの反応がなくなるはずなのに、
336
-
344
+
337
- ボタンをタップすると数値がインクリメントされる。
345
+ ボタンをタップすると数値がインクリメントされる。
338
346
 
339
347
 
340
348
 

3

コードが乱れたので直しました。

2019/05/30 00:34

投稿

murakami-kurio
murakami-kurio

スコア15

test CHANGED
File without changes
test CHANGED
@@ -34,356 +34,354 @@
34
34
 
35
35
 
36
36
 
37
+ helloworld4/app/src/main/res/xml/device_filter.xml
38
+
39
+ ```xml
40
+
41
+ <?xml version="1.0" encoding="utf-8"?>
42
+
43
+ <resources>
44
+
45
+ <!-- 0x067B / 0x2303: USB serial232c ok -->
46
+
47
+ <usb-device vendor-id="1659" product-id="8963" />
48
+
49
+
50
+
51
+ <!-- 0x067B / 0x2303: USB serial232c -->
52
+
53
+ <usb-device vendor-id="1659" product-id="8963" />
54
+
55
+
56
+
57
+ <!-- 0x0403 / 0x6001: FTDI FT232R UART -->
58
+
59
+ <usb-device vendor-id="1027" product-id="24577" />
60
+
61
+
62
+
63
+ <!-- 0x0403 / 0x6015: FTDI FT231X ok -->
64
+
65
+ <usb-device vendor-id="1027" product-id="24597" />
66
+
67
+
68
+
69
+ <!-- 0x2341 / Arduino -->
70
+
71
+ <usb-device vendor-id="9025" />
72
+
73
+
74
+
75
+ <!-- 0x16C0 / 0x0483: Teensyduino -->
76
+
77
+ <usb-device vendor-id="5824" product-id="1155" />
78
+
79
+
80
+
81
+ <!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
82
+
83
+ <usb-device vendor-id="4292" product-id="60000" />
84
+
85
+
86
+
87
+ <!-- 0x067B / 0x2303: Prolific PL2303 -->
88
+
89
+ <usb-device vendor-id="1659" product-id="8963" />
90
+
91
+
92
+
93
+ <!-- 0x1a86 / 0x7523: Qinheng CH340 -->
94
+
95
+ <usb-device vendor-id="6790" product-id="29987" />
96
+
97
+ </resources>
98
+
37
99
  ```
38
100
 
101
+
102
+
103
+ ### 該当のソースコード(2019/05/29 17:00修正版)
104
+
105
+ MainActivity.java
106
+
107
+ ```java
108
+
109
+ package xx.xx.xxxx.helloworld5;
110
+
111
+
112
+
113
+ import android.content.Context;
114
+
115
+ import android.support.v7.app.AppCompatActivity;
116
+
117
+ import android.os.Bundle;
118
+
119
+ import java.io.*;
120
+
121
+ import android.hardware.usb.*;
122
+
123
+ import android.util.Log;
124
+
125
+ import android.widget.TextView;
126
+
127
+ import com.hoho.android.usbserial.driver.*;
128
+
129
+
130
+
131
+ public class MainActivity extends AppCompatActivity {
132
+
133
+ UsbSerialDriver usb;
134
+
135
+ int counter = 0;
136
+
137
+ int counter2 = 0;
138
+
139
+
140
+
141
+ @Override
142
+
143
+ protected void onCreate(Bundle savedInstanceState) {
144
+
145
+ super.onCreate(savedInstanceState);
146
+
147
+ setContentView(R.layout.activity_main);
148
+
149
+
150
+
151
+ UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
152
+
153
+ usb = UsbSerialProber.acquire(manager);
154
+
155
+ if (usb != null) {
156
+
157
+ try{
158
+
159
+ usb.open();
160
+
161
+ usb.setBaudRate(19200);
162
+
163
+ start_read_thread(); // シリアル通信を読むスレッドを起動
164
+
165
+ }
166
+
167
+ catch(IOException e){
168
+
169
+ e.printStackTrace();
170
+
171
+ }
172
+
173
+ counter++;
174
+
175
+ String str = String.valueOf(counter);
176
+
177
+ TextView my_counter = findViewById(R.id.counter_text);
178
+
179
+ my_counter.setText(str);
180
+
181
+
182
+
183
+ while(true) {
184
+
185
+ try {
186
+
187
+ usb.write("o".getBytes("UTF-8"), 1);
188
+
189
+ } catch (UnsupportedEncodingException e) {
190
+
191
+ // 発生し得ないが、書くしかない
192
+
193
+ // https://qiita.com/niharu/items/662b130149ee180f7f34
194
+
195
+ } catch (IOException e) {
196
+
197
+ e.printStackTrace();
198
+
199
+ }
200
+
201
+ }
202
+
203
+ } else {
204
+
205
+ counter2++;
206
+
207
+ String str2 = String.valueOf(counter2);
208
+
209
+ TextView my_counter2 = findViewById(R.id.counter_text2);
210
+
211
+ my_counter2.setText(str2);
212
+
213
+ }
214
+
215
+ }
216
+
217
+ public void start_read_thread(){
218
+
219
+ new Thread(new Runnable(){
220
+
221
+ public void run(){
222
+
223
+ try{
224
+
225
+ while(true){
226
+
227
+ byte buf[] = new byte[256];
228
+
229
+ int num = usb.read(buf, buf.length);
230
+
231
+ if(num > 0) Log.v("Arduino", new String(buf, 0, num)); // Arduinoから受信した値をlogcat出力
232
+
233
+ Thread.sleep(10);
234
+
235
+ }
236
+
237
+ }
238
+
239
+ catch(IOException e){
240
+
241
+ e.printStackTrace();
242
+
243
+ }
244
+
245
+ catch (InterruptedException e) {
246
+
247
+ e.printStackTrace();
248
+
249
+ }
250
+
251
+ }
252
+
253
+ }).start();
254
+
255
+ }
256
+
257
+ }
258
+
259
+ ```
260
+
261
+ AndroidManifest.xml
262
+
263
+ ```xml
264
+
265
+ <?xml version="1.0" encoding="utf-8"?>
266
+
267
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
268
+
269
+ package="xx.xx.xxxx.helloworld5">
270
+
271
+ <uses-feature android:name="android.hardware.usb.host" />
272
+
273
+ <!-- https://blog.integrityworks.co.jp/2017/12/20/permission-is-only-granted-to-system-app/ -->
274
+
275
+ <uses-permission android:name="android.permission.MANAGE_USB" />
276
+
277
+ <application
278
+
279
+ android:allowBackup="true"
280
+
281
+ android:icon="@mipmap/ic_launcher"
282
+
283
+ android:label="@string/app_name"
284
+
285
+ android:roundIcon="@mipmap/ic_launcher_round"
286
+
287
+ android:supportsRtl="true"
288
+
289
+ android:theme="@style/AppTheme">
290
+
291
+ <activity android:name=".MainActivity">
292
+
293
+ <intent-filter>
294
+
295
+ <action android:name="android.intent.action.MAIN" />
296
+
297
+ <action android:name="android.intent.action.VIEW"/>
298
+
299
+
300
+
301
+ <category android:name="android.intent.category.LAUNCHER" />
302
+
303
+ </intent-filter>
304
+
305
+ <intent-filter>
306
+
307
+ <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
308
+
309
+ </intent-filter>
310
+
311
+ <meta-data
312
+
313
+ android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
314
+
315
+ android:resource="@xml/device_filter" />
316
+
317
+ </activity>
318
+
319
+ </application>
320
+
321
+
322
+
323
+ </manifest>
324
+
325
+ ```
326
+
39
327
  helloworld4/app/src/main/res/xml/device_filter.xml
40
328
 
41
- ```xml
42
-
43
- <?xml version="1.0" encoding="utf-8"?>
44
-
45
- <resources>
46
-
47
- <!-- 0x067B / 0x2303: USB serial232c ok -->
48
-
49
- <usb-device vendor-id="1659" product-id="8963" />
50
-
51
-
52
-
53
- <!-- 0x067B / 0x2303: USB serial232c -->
54
-
55
- <usb-device vendor-id="1659" product-id="8963" />
56
-
57
-
58
-
59
- <!-- 0x0403 / 0x6001: FTDI FT232R UART -->
60
-
61
- <usb-device vendor-id="1027" product-id="24577" />
62
-
63
-
64
-
65
- <!-- 0x0403 / 0x6015: FTDI FT231X ok -->
66
-
67
- <usb-device vendor-id="1027" product-id="24597" />
68
-
69
-
70
-
71
- <!-- 0x2341 / Arduino -->
72
-
73
- <usb-device vendor-id="9025" />
74
-
75
-
76
-
77
- <!-- 0x16C0 / 0x0483: Teensyduino -->
78
-
79
- <usb-device vendor-id="5824" product-id="1155" />
80
-
81
-
82
-
83
- <!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
84
-
85
- <usb-device vendor-id="4292" product-id="60000" />
86
-
87
-
88
-
89
- <!-- 0x067B / 0x2303: Prolific PL2303 -->
90
-
91
- <usb-device vendor-id="1659" product-id="8963" />
92
-
93
-
94
-
95
- <!-- 0x1a86 / 0x7523: Qinheng CH340 -->
96
-
97
- <usb-device vendor-id="6790" product-id="29987" />
98
-
99
- </resources>
329
+ は、そのままです。
330
+
331
+
332
+
333
+ ### 試したこと
334
+
335
+ シリアルの端末がつながると、スイッチの反応がなくなるはずなのに、
336
+
337
+ ボタンをタップすると数値がインクリメントされる。
338
+
339
+
340
+
341
+ ### 補足情報(FW/ツールのバージョンなど)
342
+
343
+ シリアルの端末がつながると、ポップアップで許可を求めてきます。
344
+
345
+ API19
346
+
347
+ Android Studio 3.4です。
348
+
349
+ Q88というAndroid 4.4の実機で試しています。
350
+
351
+
352
+
353
+ #####ここにより詳細な情報を記載してください。
354
+
355
+ ライブラリーとして、
356
+
357
+ usb-serial-for-android-v010.jar(どこで拾ったのかわからなくなっている)
358
+
359
+ を取り込んでいます。
360
+
361
+ usb-serial-for-androidのグーグルコードが消えてしまって、情報が半分くらい
362
+
363
+ 消え去っている。(githubに飛ばされた)
364
+
365
+ githubに公開されている、ライブラリのソースは読んでいない...です。すいません。
366
+
367
+ [usb-serial-for-androidのgithub](https://github.com/mik3y/usb-serial-for-android)
368
+
369
+
370
+
371
+ 次の2つのシリアル通信を試しました。
100
372
 
101
373
  ```
102
374
 
103
-
104
-
105
- ### 該当のソースコード(2019/05/29 17:00修正版)
106
-
107
- MainActivity.java
108
-
109
- ```java
110
-
111
- package xx.xx.xxxx.helloworld5;
112
-
113
-
114
-
115
- import android.content.Context;
116
-
117
- import android.support.v7.app.AppCompatActivity;
118
-
119
- import android.os.Bundle;
120
-
121
- import java.io.*;
122
-
123
- import android.hardware.usb.*;
124
-
125
- import android.util.Log;
126
-
127
- import android.widget.TextView;
128
-
129
- import com.hoho.android.usbserial.driver.*;
130
-
131
-
132
-
133
- public class MainActivity extends AppCompatActivity {
134
-
135
- UsbSerialDriver usb;
136
-
137
- int counter = 0;
138
-
139
- int counter2 = 0;
140
-
141
-
142
-
143
- @Override
375
+ 古くて情報がないもの。
144
-
145
- protected void onCreate(Bundle savedInstanceState) {
376
+
146
-
147
- super.onCreate(savedInstanceState);
148
-
149
- setContentView(R.layout.activity_main);
150
-
151
-
152
-
153
- UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
377
+ usb-device vendor-id="1659" product-id="8963"
154
-
155
- usb = UsbSerialProber.acquire(manager);
378
+
156
-
157
- if (usb != null) {
158
-
159
- try{
160
-
161
- usb.open();
162
-
163
- usb.setBaudRate(19200);
164
-
165
- start_read_thread(); // シリアル通信を読むスレッドを起動
166
-
167
- }
168
-
169
- catch(IOException e){
170
-
171
- e.printStackTrace();
172
-
173
- }
174
-
175
- counter++;
176
-
177
- String str = String.valueOf(counter);
379
+ 秋月電子通商のFT234X 超小型USBシリアル変換モジュール
178
-
179
- TextView my_counter = findViewById(R.id.counter_text);
380
+
180
-
181
- my_counter.setText(str);
182
-
183
-
184
-
185
- while(true) {
186
-
187
- try {
188
-
189
- usb.write("o".getBytes("UTF-8"), 1);
381
+ usb-device vendor-id="1027" product-id="24597"
190
-
191
- } catch (UnsupportedEncodingException e) {
192
-
193
- // 発生し得ないが、書くしかない
194
-
195
- // https://qiita.com/niharu/items/662b130149ee180f7f34
196
-
197
- } catch (IOException e) {
198
-
199
- e.printStackTrace();
200
-
201
- }
202
-
203
- }
204
-
205
- } else {
206
-
207
- counter2++;
208
-
209
- String str2 = String.valueOf(counter2);
210
-
211
- TextView my_counter2 = findViewById(R.id.counter_text2);
212
-
213
- my_counter2.setText(str2);
214
-
215
- }
216
-
217
- }
218
-
219
- public void start_read_thread(){
220
-
221
- new Thread(new Runnable(){
222
-
223
- public void run(){
224
-
225
- try{
226
-
227
- while(true){
228
-
229
- byte buf[] = new byte[256];
230
-
231
- int num = usb.read(buf, buf.length);
232
-
233
- if(num > 0) Log.v("Arduino", new String(buf, 0, num)); // Arduinoから受信した値をlogcat出力
234
-
235
- Thread.sleep(10);
236
-
237
- }
238
-
239
- }
240
-
241
- catch(IOException e){
242
-
243
- e.printStackTrace();
244
-
245
- }
246
-
247
- catch (InterruptedException e) {
248
-
249
- e.printStackTrace();
250
-
251
- }
252
-
253
- }
254
-
255
- }).start();
256
-
257
- }
258
-
259
- }
260
382
 
261
383
  ```
262
384
 
263
- AndroidManifest.xml
264
-
265
- ```xml
266
-
267
- <?xml version="1.0" encoding="utf-8"?>
268
-
269
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
270
-
271
- package="xx.xx.xxxx.helloworld5">
272
-
273
- <uses-feature android:name="android.hardware.usb.host" />
274
-
275
- <!-- https://blog.integrityworks.co.jp/2017/12/20/permission-is-only-granted-to-system-app/ -->
276
-
277
- <uses-permission android:name="android.permission.MANAGE_USB" />
278
-
279
- <application
280
-
281
- android:allowBackup="true"
282
-
283
- android:icon="@mipmap/ic_launcher"
284
-
285
- android:label="@string/app_name"
286
-
287
- android:roundIcon="@mipmap/ic_launcher_round"
288
-
289
- android:supportsRtl="true"
290
-
291
- android:theme="@style/AppTheme">
292
-
293
- <activity android:name=".MainActivity">
294
-
295
- <intent-filter>
296
-
297
- <action android:name="android.intent.action.MAIN" />
298
-
299
- <action android:name="android.intent.action.VIEW"/>
300
-
301
-
302
-
303
- <category android:name="android.intent.category.LAUNCHER" />
304
-
305
- </intent-filter>
306
-
307
- <intent-filter>
308
-
309
- <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
310
-
311
- </intent-filter>
312
-
313
- <meta-data
314
-
315
- android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
316
-
317
- android:resource="@xml/device_filter" />
318
-
319
- </activity>
320
-
321
- </application>
322
-
323
-
324
-
325
- </manifest>
326
-
327
- ```
328
-
329
- helloworld4/app/src/main/res/xml/device_filter.xml
330
-
331
- は、そのままです。
332
-
333
-
334
-
335
- ### 試したこと
336
-
337
- シリアルの端末がつながると、スイッチの反応がなくなるはずなのに、
338
-
339
- ボタンをタップすると数値がインクリメントされる。
340
-
341
-
342
-
343
- ### 補足情報(FW/ツールのバージョンなど)
344
-
345
- シリアルの端末がつながると、ポップアップで許可を求めてきます。
346
-
347
- API19
348
-
349
- Android Studio 3.4です。
350
-
351
- Q88というAndroid 4.4の実機で試しています。
352
-
353
-
354
-
355
- #####ここにより詳細な情報を記載してください。
356
-
357
- ライブラリーとして、
358
-
359
- usb-serial-for-android-v010.jar(どこで拾ったのかわからなくなっている)
360
-
361
- を取り込んでいます。
362
-
363
- usb-serial-for-androidのグーグルコードが消えてしまって、情報が半分くらい
364
-
365
- 消え去っている。(githubに飛ばされた)
366
-
367
- githubに公開されている、ライブラリのソースは読んでいない...です。すいません。
368
-
369
- [usb-serial-for-androidのgithub](https://github.com/mik3y/usb-serial-for-android)
370
-
371
-
372
-
373
- 次の2つのシリアル通信を試しました。
374
-
375
- ```
376
-
377
- 古くて情報がないもの。
378
-
379
- usb-device vendor-id="1659" product-id="8963"
380
-
381
- 秋月電子通商のFT234X 超小型USBシリアル変換モジュール
382
-
383
- usb-device vendor-id="1027" product-id="24597"
384
-
385
- ```
386
-
387
385
 
388
386
 
389
387
  Q88のadbがなくて、ちまちまリリース用のakpをファイル転送してテストしています。

2

追加情報を記載しました。

2019/05/29 08:25

投稿

murakami-kurio
murakami-kurio

スコア15

test CHANGED
File without changes
test CHANGED
@@ -28,9 +28,87 @@
28
28
 
29
29
  MainActivity.java
30
30
 
31
+ activity_main.xml
32
+
33
+ (文字数が...と出たので削除)
34
+
35
+
36
+
37
+ ```
38
+
39
+ helloworld4/app/src/main/res/xml/device_filter.xml
40
+
41
+ ```xml
42
+
43
+ <?xml version="1.0" encoding="utf-8"?>
44
+
45
+ <resources>
46
+
47
+ <!-- 0x067B / 0x2303: USB serial232c ok -->
48
+
49
+ <usb-device vendor-id="1659" product-id="8963" />
50
+
51
+
52
+
53
+ <!-- 0x067B / 0x2303: USB serial232c -->
54
+
55
+ <usb-device vendor-id="1659" product-id="8963" />
56
+
57
+
58
+
59
+ <!-- 0x0403 / 0x6001: FTDI FT232R UART -->
60
+
61
+ <usb-device vendor-id="1027" product-id="24577" />
62
+
63
+
64
+
65
+ <!-- 0x0403 / 0x6015: FTDI FT231X ok -->
66
+
67
+ <usb-device vendor-id="1027" product-id="24597" />
68
+
69
+
70
+
71
+ <!-- 0x2341 / Arduino -->
72
+
73
+ <usb-device vendor-id="9025" />
74
+
75
+
76
+
77
+ <!-- 0x16C0 / 0x0483: Teensyduino -->
78
+
79
+ <usb-device vendor-id="5824" product-id="1155" />
80
+
81
+
82
+
83
+ <!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
84
+
85
+ <usb-device vendor-id="4292" product-id="60000" />
86
+
87
+
88
+
89
+ <!-- 0x067B / 0x2303: Prolific PL2303 -->
90
+
91
+ <usb-device vendor-id="1659" product-id="8963" />
92
+
93
+
94
+
95
+ <!-- 0x1a86 / 0x7523: Qinheng CH340 -->
96
+
97
+ <usb-device vendor-id="6790" product-id="29987" />
98
+
99
+ </resources>
100
+
101
+ ```
102
+
103
+
104
+
105
+ ### 該当のソースコード(2019/05/29 17:00修正版)
106
+
107
+ MainActivity.java
108
+
31
109
  ```java
32
110
 
33
- package xx.xx.xxxx.helloworld4;
111
+ package xx.xx.xxxx.helloworld5;
34
112
 
35
113
 
36
114
 
@@ -44,28 +122,22 @@
44
122
 
45
123
  import android.hardware.usb.*;
46
124
 
125
+ import android.util.Log;
126
+
127
+ import android.widget.TextView;
128
+
47
129
  import com.hoho.android.usbserial.driver.*;
48
130
 
49
- import android.util.Log;
50
-
51
- import android.view.View;
52
-
53
- import android.widget.Button;
54
-
55
- import android.widget.TextView;
56
-
57
131
 
58
132
 
59
133
  public class MainActivity extends AppCompatActivity {
60
134
 
61
135
  UsbSerialDriver usb;
62
136
 
63
- byte my_echo[];
64
-
65
- String result;
66
-
67
137
  int counter = 0;
68
138
 
139
+ int counter2 = 0;
140
+
69
141
 
70
142
 
71
143
  @Override
@@ -78,11 +150,13 @@
78
150
 
79
151
 
80
152
 
81
- // http://shokai.org/blog/archives/6962
153
+ UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
154
+
155
+ usb = UsbSerialProber.acquire(manager);
82
156
 
83
157
  if (usb != null) {
84
158
 
85
- try {
159
+ try{
86
160
 
87
161
  usb.open();
88
162
 
@@ -90,157 +164,103 @@
90
164
 
91
165
  start_read_thread(); // シリアル通信を読むスレッドを起動
92
166
 
167
+ }
168
+
93
- } catch (IOException e) {
169
+ catch(IOException e){
94
170
 
95
171
  e.printStackTrace();
96
172
 
97
173
  }
98
174
 
175
+ counter++;
176
+
177
+ String str = String.valueOf(counter);
178
+
179
+ TextView my_counter = findViewById(R.id.counter_text);
180
+
181
+ my_counter.setText(str);
182
+
183
+
184
+
185
+ while(true) {
186
+
187
+ try {
188
+
189
+ usb.write("o".getBytes("UTF-8"), 1);
190
+
191
+ } catch (UnsupportedEncodingException e) {
192
+
193
+ // 発生し得ないが、書くしかない
194
+
195
+ // https://qiita.com/niharu/items/662b130149ee180f7f34
196
+
197
+ } catch (IOException e) {
198
+
199
+ e.printStackTrace();
200
+
201
+ }
202
+
203
+ }
204
+
205
+ } else {
206
+
207
+ counter2++;
208
+
209
+ String str2 = String.valueOf(counter2);
210
+
211
+ TextView my_counter2 = findViewById(R.id.counter_text2);
212
+
213
+ my_counter2.setText(str2);
214
+
99
215
  }
100
216
 
101
- Button button = findViewById(R.id.button);
102
-
103
- button.setOnClickListener(new View.OnClickListener() {
104
-
105
- @Override
217
+ }
106
-
218
+
107
- public void onClick(View view) {
219
+ public void start_read_thread(){
220
+
108
-
221
+ new Thread(new Runnable(){
222
+
109
- if (usb != null) {
223
+ public void run(){
110
-
111
-
112
-
224
+
113
- try {
225
+ try{
226
+
114
-
227
+ while(true){
228
+
115
- usb.write("s".getBytes("UTF-8"), 1);
229
+ byte buf[] = new byte[256];
116
-
230
+
117
- } catch (UnsupportedEncodingException e) {
231
+ int num = usb.read(buf, buf.length);
118
-
119
- // 発生し得ないが、書くしかない
232
+
120
-
121
- // https://qiita.com/niharu/items/662b130149ee180f7f34
233
+ if(num > 0) Log.v("Arduino", new String(buf, 0, num)); // Arduinoから受信した値をlogcat出力
122
-
123
- } catch (IOException e) {
234
+
124
-
125
- e.printStackTrace();
235
+ Thread.sleep(10);
126
236
 
127
237
  }
128
238
 
129
-
130
-
131
- // https://codeday.me/jp/qa/20190316/423730.html
132
-
133
- try {
134
-
135
- Thread.sleep(100);
136
-
137
- } catch (InterruptedException e) {
138
-
139
- e.printStackTrace();
140
-
141
- }
142
-
143
-
144
-
145
- try {
146
-
147
- usb.read(my_echo, my_echo.length);
148
-
149
- } catch (IOException e) {
150
-
151
- e.printStackTrace();
152
-
153
- }
154
-
155
-
156
-
157
- try {
158
-
159
- result = new String(my_echo, "UTF-8");
160
-
161
- } catch (UnsupportedEncodingException e) {
162
-
163
- // 発生し得ないが、書くしかない
164
-
165
- // https://qiita.com/niharu/items/662b130149ee180f7f34
166
-
167
- }
168
-
169
- } else {
170
-
171
- TextView textView = findViewById(R.id.textView);
172
-
173
- textView.setText("tap!!");
174
-
175
-
176
-
177
- counter++;
178
-
179
- String str = String.valueOf(counter);
180
-
181
- TextView my_counter = findViewById(R.id.my_counter);
182
-
183
- my_counter.setText(str);
184
-
185
-
186
-
187
- UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
188
-
189
- usb = UsbSerialProber.acquire(manager);
190
-
191
239
  }
192
240
 
241
+ catch(IOException e){
242
+
243
+ e.printStackTrace();
244
+
245
+ }
246
+
247
+ catch (InterruptedException e) {
248
+
249
+ e.printStackTrace();
250
+
251
+ }
252
+
193
253
  }
194
254
 
195
- });
255
+ }).start();
196
256
 
197
257
  }
198
258
 
199
- public void start_read_thread(){
200
-
201
- new Thread(new Runnable(){
202
-
203
- public void run(){
204
-
205
- try{
206
-
207
- while(true){
208
-
209
- byte buf[] = new byte[256];
210
-
211
- int num = usb.read(buf, buf.length);
212
-
213
- if(num > 0) Log.v("arduino", new String(buf, 0, num)); // Arduinoから受信した値をlogcat出力
214
-
215
- Thread.sleep(10);
216
-
217
- }
218
-
219
- }
220
-
221
- catch(IOException e){
222
-
223
- e.printStackTrace();
224
-
225
- }
226
-
227
- catch (InterruptedException e) {
228
-
229
- e.printStackTrace();
230
-
231
- }
232
-
233
- }
234
-
235
- }).start();
236
-
237
- }
238
-
239
259
  }
240
260
 
241
261
  ```
242
262
 
243
- activity_main.xml
263
+ AndroidManifest.xml
244
264
 
245
265
  ```xml
246
266
 
@@ -248,9 +268,13 @@
248
268
 
249
269
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
250
270
 
251
- package="jp.co.kurio_t.helloworld4">
271
+ package="xx.xx.xxxx.helloworld5">
272
+
252
-
273
+ <uses-feature android:name="android.hardware.usb.host" />
274
+
253
-
275
+ <!-- https://blog.integrityworks.co.jp/2017/12/20/permission-is-only-granted-to-system-app/ -->
276
+
277
+ <uses-permission android:name="android.permission.MANAGE_USB" />
254
278
 
255
279
  <application
256
280
 
@@ -280,11 +304,9 @@
280
304
 
281
305
  </intent-filter>
282
306
 
283
-
284
-
285
307
  <intent-filter>
286
308
 
287
- <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
309
+ <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
288
310
 
289
311
  </intent-filter>
290
312
 
@@ -306,67 +328,7 @@
306
328
 
307
329
  helloworld4/app/src/main/res/xml/device_filter.xml
308
330
 
309
- ```xml
331
+ は、そのままです。
310
-
311
- <?xml version="1.0" encoding="utf-8"?>
312
-
313
- <resources>
314
-
315
- <!-- 0x067B / 0x2303: USB serial232c ok -->
316
-
317
- <usb-device vendor-id="1659" product-id="8963" />
318
-
319
-
320
-
321
- <!-- 0x067B / 0x2303: USB serial232c -->
322
-
323
- <usb-device vendor-id="1659" product-id="8963" />
324
-
325
-
326
-
327
- <!-- 0x0403 / 0x6001: FTDI FT232R UART -->
328
-
329
- <usb-device vendor-id="1027" product-id="24577" />
330
-
331
-
332
-
333
- <!-- 0x0403 / 0x6015: FTDI FT231X ok -->
334
-
335
- <usb-device vendor-id="1027" product-id="24597" />
336
-
337
-
338
-
339
- <!-- 0x2341 / Arduino -->
340
-
341
- <usb-device vendor-id="9025" />
342
-
343
-
344
-
345
- <!-- 0x16C0 / 0x0483: Teensyduino -->
346
-
347
- <usb-device vendor-id="5824" product-id="1155" />
348
-
349
-
350
-
351
- <!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
352
-
353
- <usb-device vendor-id="4292" product-id="60000" />
354
-
355
-
356
-
357
- <!-- 0x067B / 0x2303: Prolific PL2303 -->
358
-
359
- <usb-device vendor-id="1659" product-id="8963" />
360
-
361
-
362
-
363
- <!-- 0x1a86 / 0x7523: Qinheng CH340 -->
364
-
365
- <usb-device vendor-id="6790" product-id="29987" />
366
-
367
- </resources>
368
-
369
- ```
370
332
 
371
333
 
372
334
 
@@ -429,3 +391,59 @@
429
391
 
430
392
 
431
393
  よろしくお願いいたします。
394
+
395
+
396
+
397
+
398
+
399
+ ### 追加情報
400
+
401
+ ソースコードをリンク先に近い形にしました。
402
+
403
+
404
+
405
+ >通信相手をつないで『アプリの画面の R.id.button ボタンを
406
+
407
+ >押しても』usbがNULLのまま, という解釈であっていますでしょうか.
408
+
409
+ はい。
410
+
411
+
412
+
413
+ >なんのスイッチでしょう.
414
+
415
+ 『アプリの画面の R.id.button ボタン』です。
416
+
417
+
418
+
419
+ >どのような「許可」を求められるのでしょうか
420
+
421
+ Open アプリ名 when this USB device is connected?
422
+
423
+ □Use by default for this USB device
424
+
425
+ 「Cancel」「 OK」
426
+
427
+ で、チェックマークを入れずにOKをタップした後、
428
+
429
+ ボタンの反応しますので、usbがnullと判断しています。
430
+
431
+
432
+
433
+ マニフェストに
434
+
435
+ <uses-permission android:name="android.permission.MANAGE_USB" />
436
+
437
+ を追記してみましたが、変わりませんでした。
438
+
439
+
440
+
441
+ [AndroidのUSBホスト機能を緩く使ってみる](https://relativelayout.hatenablog.com/entry/2016/08/04/221216)
442
+
443
+ に従って、getDeviceListしてみました。
444
+
445
+ シリアル機器をUSBに接続すると
446
+
447
+ /dev/bus/001/026
448
+
449
+ と出ました。

1

githubの「ライブラリーの」ソースを読んでません。と追加。

2019/05/29 08:24

投稿

murakami-kurio
murakami-kurio

スコア15

test CHANGED
File without changes
test CHANGED
@@ -402,7 +402,7 @@
402
402
 
403
403
  消え去っている。(githubに飛ばされた)
404
404
 
405
- githubに公開されている、ソースは読んでいない...です。すいません。
405
+ githubに公開されている、ライブラリのソースは読んでいない...です。すいません。
406
406
 
407
407
  [usb-serial-for-androidのgithub](https://github.com/mik3y/usb-serial-for-android)
408
408