質問編集履歴
1
質問内容がほとんど関係無い初歩ミスだった
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,147 +40,9 @@
|
|
40
40
|
|
41
41
|
```java
|
42
42
|
|
43
|
-
import android.content.Context;
|
44
|
-
|
45
|
-
import android.hardware.usb.UsbManager;
|
46
|
-
|
47
|
-
import android.os.Bundle;
|
48
|
-
|
49
|
-
import android.support.v7.app.AppCompatActivity;
|
50
|
-
|
51
|
-
import android.view.View;
|
52
|
-
|
53
|
-
import android.widget.Button;
|
54
|
-
|
55
|
-
import android.widget.TextView;
|
56
43
|
|
57
44
|
|
58
45
|
|
59
|
-
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
60
|
-
|
61
|
-
import com.hoho.android.usbserial.driver.UsbSerialProber;
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
import java.io.IOException;
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
public class MainActivity extends AppCompatActivity {
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
@Override
|
76
|
-
|
77
|
-
protected void onCreate(Bundle savedInstanceState) {
|
78
|
-
|
79
|
-
super.onCreate(savedInstanceState);
|
80
|
-
|
81
|
-
setContentView(R.layout.activity_main);
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
Button button1 = (Button) findViewById(R.id.button);
|
86
|
-
|
87
|
-
button1.setOnClickListener(new View.OnClickListener()
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
{
|
92
|
-
|
93
|
-
public void onClick(View v) {
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
98
|
-
|
99
|
-
UsbSerialDriver usb = UsbSerialProber.acquire(manager);
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
try {
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
usb.open();
|
108
|
-
|
109
|
-
usb.setBaudRate(38400);
|
110
|
-
|
111
|
-
String str = "start\r\n";
|
112
|
-
|
113
|
-
usb.write(str.getBytes("ascii"),7);
|
114
|
-
|
115
|
-
usb.close();
|
116
|
-
|
117
|
-
} catch (IOException e) {
|
118
|
-
|
119
|
-
e.printStackTrace();
|
120
|
-
|
121
|
-
}
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
});
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
Button button2 = (Button) findViewById(R.id.button2);
|
134
|
-
|
135
|
-
button2.setOnClickListener(new View.OnClickListener()
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
{public void onClick(View v) {
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
144
|
-
|
145
|
-
final UsbSerialDriver usb = UsbSerialProber.acquire(manager);
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
try {
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
usb.open();
|
154
|
-
|
155
|
-
usb.setBaudRate(38400);
|
156
|
-
|
157
|
-
byte buf[] = new byte[256];
|
158
|
-
|
159
|
-
int num = usb.read(buf, buf.length);
|
160
|
-
|
161
|
-
String str = new String(buf);
|
162
|
-
|
163
|
-
TextView textview = (TextView) findViewById(R.id.textview);
|
164
|
-
|
165
|
-
textview.setText(str);
|
166
|
-
|
167
|
-
} catch (IOException e) {
|
168
|
-
|
169
|
-
e.printStackTrace();
|
170
|
-
|
171
|
-
}
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
});
|
180
|
-
|
181
|
-
}
|
182
|
-
|
183
|
-
}
|
184
46
|
|
185
47
|
```
|
186
48
|
|