質問編集履歴

2

コード表示の```を修正

2019/12/25 09:06

投稿

niawniwa
niawniwa

スコア5

test CHANGED
File without changes
test CHANGED
@@ -10,300 +10,298 @@
10
10
 
11
11
  ### 発生している問題・エラーメッセージ
12
12
 
13
+ androidのエラー終了時の表示「問題が発生したため、usbhostを終了します。」
14
+
15
+
16
+
17
+ private void connectDevice() の
18
+
19
+ if (!connection.claimInterface(mUsbDevice.getInterface(1), true)) {
20
+
21
+ }
22
+
23
+
24
+
25
+ ### 該当のソースコード
26
+
27
+
28
+
29
+ JAVA
30
+
13
31
 
14
32
 
15
33
  ```
16
34
 
35
+ package com.android.usbhost;
36
+
37
+
38
+
39
+ import android.app.PendingIntent;
40
+
41
+ import android.content.Intent;
42
+
43
+ import android.hardware.usb.UsbConstants;
44
+
45
+ import android.hardware.usb.UsbDevice;
46
+
17
- androidのエラー終了時の表示「問題が発生したため、usbhostを終了します。」
47
+ import android.hardware.usb.UsbDeviceConnection;
48
+
18
-
49
+ import android.hardware.usb.UsbEndpoint;
50
+
19
-
51
+ import android.hardware.usb.UsbInterface;
52
+
20
-
53
+ import android.hardware.usb.UsbManager;
54
+
55
+ import android.os.Bundle;
56
+
57
+ import android.support.v7.app.AppCompatActivity;
58
+
59
+ import android.view.View;
60
+
61
+ import android.widget.Button;
62
+
63
+ import android.widget.TextView;
64
+
65
+
66
+
67
+ import java.util.HashMap;
68
+
69
+
70
+
71
+ public class MainActivity extends AppCompatActivity {
72
+
73
+
74
+
75
+ private TextView mTextView;
76
+
77
+
78
+
79
+ private Button mButton;
80
+
81
+
82
+
83
+ private UsbManager mUsbManager;
84
+
85
+
86
+
87
+ private UsbDevice mUsbDevice;
88
+
89
+
90
+
91
+ @Override
92
+
93
+ protected void onCreate(Bundle savedInstanceState) {
94
+
95
+ super.onCreate(savedInstanceState);
96
+
97
+ setContentView(R.layout.activity_main);
98
+
99
+ }
100
+
101
+
102
+
103
+ public void onResume() {
104
+
105
+ super.onResume();
106
+
107
+
108
+
109
+ mTextView = (TextView)findViewById(R.id.textview);
110
+
111
+ mButton = (Button)findViewById(R.id.button);
112
+
113
+ mUsbManager = (UsbManager)getSystemService(USB_SERVICE);
114
+
115
+
116
+
21
- ### 該当のソースコード
117
+ updateList();
118
+
22
-
119
+ mButton.setOnClickListener(new View.OnClickListener() {
120
+
121
+ @Override
122
+
123
+ public void onClick(View v) {
124
+
125
+ if (mUsbDevice == null) {
126
+
127
+ return;
128
+
129
+ }
130
+
131
+
132
+
133
+ if (!mUsbManager.hasPermission(mUsbDevice)) {
134
+
135
+ mUsbManager.requestPermission(mUsbDevice,
136
+
137
+ PendingIntent.getBroadcast(MainActivity.this, 0, new Intent("なにか"), 0));
138
+
139
+ return;
140
+
141
+ }
142
+
143
+
144
+
145
+ connectDevice();
146
+
147
+ }
148
+
149
+ });
150
+
151
+ }
152
+
153
+
154
+
155
+ public void onPause() {
156
+
157
+ super.onPause();
158
+
159
+ mUsbDevice = null;
160
+
161
+ mButton.setOnClickListener(null);
162
+
163
+ }
164
+
165
+
166
+
167
+ private void updateList() {
168
+
169
+ HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
170
+
171
+
172
+
173
+ if (deviceList == null || deviceList.isEmpty()) {
174
+
175
+ mTextView.setText("no device found");
176
+
177
+ } else {
178
+
179
+ String string = "";
180
+
181
+
182
+
183
+ for (String name : deviceList.keySet()) {
184
+
185
+ string += name;
186
+
187
+
188
+
189
+ if (deviceList.get(name).getVendorId() == 9025) {
190
+
191
+ string += " (Arduino)\n";
192
+
193
+ mUsbDevice = deviceList.get(name);
194
+
195
+ } else {
196
+
197
+ string += "\n";
198
+
199
+ }
200
+
201
+ }
202
+
203
+
204
+
205
+ mTextView.setText(string);
206
+
207
+ }
208
+
209
+ }
210
+
211
+
212
+
23
- private void connectDevice()
213
+ private void connectDevice() {
214
+
215
+ new Thread(new Runnable() {
216
+
217
+ @Override
218
+
219
+ public void run() {
220
+
221
+ UsbDeviceConnection connection = mUsbManager.openDevice(mUsbDevice);
222
+
223
+
224
+
225
+ if (!connection.claimInterface(mUsbDevice.getInterface(1), true)) {
226
+
227
+ connection.close();
228
+
229
+ return;
230
+
231
+ }
232
+
233
+
234
+
235
+ connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
236
+
237
+ connection.controlTransfer(0x21, 32, 0, 0, new byte[] {
238
+
239
+ (byte)0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08
240
+
241
+ }, 7, 0);
242
+
243
+
244
+
245
+ UsbEndpoint epIN = null;
246
+
247
+ UsbEndpoint epOUT = null;
248
+
249
+
250
+
251
+ UsbInterface usbIf = mUsbDevice.getInterface(1);
252
+
253
+ for (int i = 0; i < usbIf.getEndpointCount(); i++) {
254
+
255
+ if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
256
+
257
+ if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
258
+
259
+ epIN = usbIf.getEndpoint(i);
260
+
261
+ else
262
+
263
+ epOUT = usbIf.getEndpoint(i);
264
+
265
+ }
266
+
267
+ }
268
+
269
+
270
+
271
+ connection.bulkTransfer(epOUT, "1".getBytes(), 1, 0);
272
+
273
+ connection.close();
274
+
275
+ }
276
+
277
+ }).start();
278
+
279
+ }
280
+
281
+ }
282
+
283
+ ```
284
+
285
+
286
+
287
+ ### 試したこと
288
+
289
+
290
+
291
+ コメント化して、どの部分でエラー終了するか調べたところ、下記の部分でした。
292
+
293
+ 下記の部分までは、問題なく作動していました。
294
+
295
+
24
296
 
25
297
  if (!connection.claimInterface(mUsbDevice.getInterface(1), true)) {
26
298
 
299
+ ※中身
300
+
27
301
  }
28
302
 
29
303
 
30
304
 
31
- JAVA
32
-
33
-
34
-
35
- ```
36
-
37
- package com.android.usbhost;
38
-
39
-
40
-
41
- import android.app.PendingIntent;
42
-
43
- import android.content.Intent;
44
-
45
- import android.hardware.usb.UsbConstants;
46
-
47
- import android.hardware.usb.UsbDevice;
48
-
49
- import android.hardware.usb.UsbDeviceConnection;
50
-
51
- import android.hardware.usb.UsbEndpoint;
52
-
53
- import android.hardware.usb.UsbInterface;
54
-
55
- import android.hardware.usb.UsbManager;
56
-
57
- import android.os.Bundle;
58
-
59
- import android.support.v7.app.AppCompatActivity;
60
-
61
- import android.view.View;
62
-
63
- import android.widget.Button;
64
-
65
- import android.widget.TextView;
66
-
67
-
68
-
69
- import java.util.HashMap;
70
-
71
-
72
-
73
- public class MainActivity extends AppCompatActivity {
74
-
75
-
76
-
77
- private TextView mTextView;
78
-
79
-
80
-
81
- private Button mButton;
82
-
83
-
84
-
85
- private UsbManager mUsbManager;
86
-
87
-
88
-
89
- private UsbDevice mUsbDevice;
90
-
91
-
92
-
93
- @Override
94
-
95
- protected void onCreate(Bundle savedInstanceState) {
96
-
97
- super.onCreate(savedInstanceState);
98
-
99
- setContentView(R.layout.activity_main);
100
-
101
- }
102
-
103
-
104
-
105
- public void onResume() {
106
-
107
- super.onResume();
108
-
109
-
110
-
111
- mTextView = (TextView)findViewById(R.id.textview);
112
-
113
- mButton = (Button)findViewById(R.id.button);
114
-
115
- mUsbManager = (UsbManager)getSystemService(USB_SERVICE);
116
-
117
-
118
-
119
- updateList();
120
-
121
- mButton.setOnClickListener(new View.OnClickListener() {
122
-
123
- @Override
124
-
125
- public void onClick(View v) {
126
-
127
- if (mUsbDevice == null) {
128
-
129
- return;
130
-
131
- }
132
-
133
-
134
-
135
- if (!mUsbManager.hasPermission(mUsbDevice)) {
136
-
137
- mUsbManager.requestPermission(mUsbDevice,
138
-
139
- PendingIntent.getBroadcast(MainActivity.this, 0, new Intent("なにか"), 0));
140
-
141
- return;
142
-
143
- }
144
-
145
-
146
-
147
- connectDevice();
148
-
149
- }
150
-
151
- });
152
-
153
- }
154
-
155
-
156
-
157
- public void onPause() {
158
-
159
- super.onPause();
160
-
161
- mUsbDevice = null;
162
-
163
- mButton.setOnClickListener(null);
164
-
165
- }
166
-
167
-
168
-
169
- private void updateList() {
170
-
171
- HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
172
-
173
-
174
-
175
- if (deviceList == null || deviceList.isEmpty()) {
176
-
177
- mTextView.setText("no device found");
178
-
179
- } else {
180
-
181
- String string = "";
182
-
183
-
184
-
185
- for (String name : deviceList.keySet()) {
186
-
187
- string += name;
188
-
189
-
190
-
191
- if (deviceList.get(name).getVendorId() == 9025) {
192
-
193
- string += " (Arduino)\n";
194
-
195
- mUsbDevice = deviceList.get(name);
196
-
197
- } else {
198
-
199
- string += "\n";
200
-
201
- }
202
-
203
- }
204
-
205
-
206
-
207
- mTextView.setText(string);
208
-
209
- }
210
-
211
- }
212
-
213
-
214
-
215
- private void connectDevice() {
216
-
217
- new Thread(new Runnable() {
218
-
219
- @Override
220
-
221
- public void run() {
222
-
223
- UsbDeviceConnection connection = mUsbManager.openDevice(mUsbDevice);
224
-
225
-
226
-
227
- if (!connection.claimInterface(mUsbDevice.getInterface(1), true)) {
228
-
229
- connection.close();
230
-
231
- return;
232
-
233
- }
234
-
235
-
236
-
237
- connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
238
-
239
- connection.controlTransfer(0x21, 32, 0, 0, new byte[] {
240
-
241
- (byte)0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08
242
-
243
- }, 7, 0);
244
-
245
-
246
-
247
- UsbEndpoint epIN = null;
248
-
249
- UsbEndpoint epOUT = null;
250
-
251
-
252
-
253
- UsbInterface usbIf = mUsbDevice.getInterface(1);
254
-
255
- for (int i = 0; i < usbIf.getEndpointCount(); i++) {
256
-
257
- if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
258
-
259
- if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
260
-
261
- epIN = usbIf.getEndpoint(i);
262
-
263
- else
264
-
265
- epOUT = usbIf.getEndpoint(i);
266
-
267
- }
268
-
269
- }
270
-
271
-
272
-
273
- connection.bulkTransfer(epOUT, "1".getBytes(), 1, 0);
274
-
275
- connection.close();
276
-
277
- }
278
-
279
- }).start();
280
-
281
- }
282
-
283
- }
284
-
285
- ```
286
-
287
-
288
-
289
- ### 試したこと
290
-
291
-
292
-
293
- コメント化して、どの部分でエラー終了するか調べたところ、下記の部分でした。
294
-
295
- 下記の部分までは、問題なく作動していました。
296
-
297
-
298
-
299
- if (!connection.claimInterface(mUsbDevice.getInterface(1), true)) {
300
-
301
- ※中身
302
-
303
- }
304
-
305
-
306
-
307
305
  ※中身の部分をコメント化しても、しなくてもエラー終了をするため、
308
306
 
309
307
  if (!connection.claimInterface(mUsbDevice.getInterface(1), true))

1

追記

2019/12/25 09:06

投稿

niawniwa
niawniwa

スコア5

test CHANGED
File without changes
test CHANGED
@@ -28,9 +28,11 @@
28
28
 
29
29
 
30
30
 
31
- ```JAVA
31
+ JAVA
32
+
33
+
34
+
32
-
35
+ ```
33
-
34
36
 
35
37
  package com.android.usbhost;
36
38
 
@@ -316,6 +318,30 @@
316
318
 
317
319
 
318
320
 
321
+
322
+
323
+ 追記
324
+
325
+
326
+
327
+ if (!connection.claimInterface(mUsbDevice.getInterface(1), true))
328
+
329
+ この部分をコメント化して、除外して進めたところ
330
+
331
+
332
+
333
+ UsbInterface usbIf = mUsbDevice.getInterface(1);
334
+
335
+ この箇所で、またエラーになりました。
336
+
337
+
338
+
339
+ 共通する、mUsbDevice.getInterface(1)が原因のようです。
340
+
341
+
342
+
343
+
344
+
319
345
  ### 補足情報(FW/ツールのバージョンなど)
320
346
 
321
347