-
Notifications
You must be signed in to change notification settings - Fork 26
/
MainActivity.java
82 lines (69 loc) · 2.14 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package me.aflak.libraries;
import android.hardware.usb.UsbDevice;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;
import me.aflak.arduino.Arduino;
import me.aflak.arduino.ArduinoListener;
public class MainActivity extends AppCompatActivity implements ArduinoListener {
private Arduino arduino;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
textView.setMovementMethod(new ScrollingMovementMethod());
arduino = new Arduino(this);
display("Please plug an Arduino via OTG.\nOn some devices you will have to enable OTG Storage in the phone's settings.\n\n");
}
@Override
protected void onStart() {
super.onStart();
arduino.setArduinoListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
arduino.unsetArduinoListener();
arduino.close();
}
@Override
public void onArduinoAttached(UsbDevice device) {
display("Arduino attached!");
arduino.open(device);
}
@Override
public void onArduinoDetached() {
display("Arduino detached");
}
@Override
public void onArduinoMessage(byte[] bytes) {
display("> "+new String(bytes));
}
@Override
public void onArduinoOpened() {
String str = "Hello World !";
arduino.send(str.getBytes());
}
@Override
public void onUsbPermissionDenied() {
display("Permission denied... New attempt in 3 sec");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
arduino.reopen();
}
}, 3000);
}
public void display(final String message){
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.append(message+"\n");
}
});
}
}