7
7
import android .content .Intent ;
8
8
import android .os .AsyncTask ;
9
9
import android .os .Bundle ;
10
+ import android .os .Handler ;
10
11
import android .support .annotation .Nullable ;
11
12
import android .support .v7 .app .AppCompatActivity ;
12
13
import android .view .View ;
13
14
import android .widget .Button ;
14
15
import android .widget .CheckBox ;
15
16
import android .widget .EditText ;
17
+ import android .widget .ImageView ;
16
18
import android .widget .Toast ;
17
19
18
20
import java .io .IOException ;
21
23
22
24
public class LEDControlActivity extends AppCompatActivity {
23
25
24
- private Button buttonOn , buttonDisconnect ;
26
+ private Button buttonDisconnect ;
25
27
private CheckBox bulb1Checkbox , bulb2Checkbox ;
28
+ private ImageView buttonOn ;
26
29
private EditText textCommand , startDelayET , stopAfterET ;
27
30
private String address ;
28
31
private ProgressDialog progress ;
@@ -31,6 +34,8 @@ public class LEDControlActivity extends AppCompatActivity {
31
34
private boolean isBTConnected = false ;
32
35
//Default Universally Unique Identifier, HC-05's default
33
36
static final UUID mUUID = UUID .fromString ("00001101-0000-1000-8000-00805F9B34FB" );
37
+ private boolean on = false ;
38
+ private Toast previousToast ;
34
39
35
40
@ Override
36
41
protected void onCreate (@ Nullable Bundle savedInstanceState ) {
@@ -39,7 +44,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
39
44
Intent receivedIntent = getIntent ();
40
45
address = receivedIntent .getStringExtra (MainActivity .EXTRA_ADDRESS );
41
46
42
- textCommand = findViewById (R .id .command_edit_text );
43
47
buttonOn = findViewById (R .id .button_on );
44
48
buttonDisconnect = findViewById (R .id .button_disconnect );
45
49
bulb1Checkbox = findViewById (R .id .bulb1_checkbox );
@@ -54,13 +58,25 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
54
58
public void onClick (View view ) {
55
59
if (bluetoothSocket != null ) {
56
60
try {
57
- String command = textCommand .getText ().toString ();
61
+ String command ;
62
+ //Set on only if one of the bulb has been checked
63
+ if (!on ) {
64
+ command = "ON" ;
65
+ //Keeping it here is required, don't keep it out for proper functioning of app
66
+ if (bulb1Checkbox .isChecked () || bulb2Checkbox .isChecked ())
67
+ buttonOn .setImageResource (R .drawable .bulb_on );
68
+ on = true ;
69
+ } else {
70
+ command = "OFF" ;
71
+ buttonOn .setImageResource (R .drawable .bulb_off );
72
+ on = false ;
73
+ }
58
74
byte [] commandBytes = command .getBytes ();
59
- String startDelay = startDelayET .getText ().toString ();
75
+ final String startDelay = startDelayET .getText ().toString ();
60
76
String stopAfter = stopAfterET .getText ().toString ();
61
77
62
- if (!startDelay .equals ("" ) && !stopAfter .equals ("" )) {
63
- command = "b" + startDelay + "000" + "e" + stopAfter + "000" + command ;
78
+ if (!startDelay .equals ("" ) && !stopAfter .equals ("" ) && on ) {
79
+ command = "b" + startDelay + "000" + "e" + stopAfter + "000" ;
64
80
commandBytes = command .getBytes ();
65
81
}
66
82
@@ -79,6 +95,8 @@ public void onClick(View view) {
79
95
//If the bluetooth socket is busy
80
96
if (bluetoothSocket != null ) {
81
97
try {
98
+ //To prevent functioning of bulb on next start of app
99
+ bluetoothSocket .getOutputStream ().write ("STOP" .getBytes ());
82
100
bluetoothSocket .close (); //Close connection
83
101
} catch (IOException e ) {
84
102
showToast ("Error!!!" );
@@ -103,7 +121,7 @@ public void onBulbCheckBoxClicked(View view) throws IOException{
103
121
} else {
104
122
//If box is unchecked, make pin 0 as output, as nothing is connected there,
105
123
//bulb won't glow
106
- bluetoothOS .write ("0 " .getBytes ());
124
+ bluetoothOS .write ("01 " .getBytes ());
107
125
showToast ("Bulb 1 is not functional" );
108
126
}
109
127
break ;
@@ -115,15 +133,20 @@ public void onBulbCheckBoxClicked(View view) throws IOException{
115
133
} else {
116
134
//If box is unchecked, make pin 0 as output, as nothing is connected there,
117
135
//bulb won't glow
118
- bluetoothOS .write ("0 " .getBytes ());
136
+ bluetoothOS .write ("02 " .getBytes ());
119
137
showToast ("Bulb 1 is not functional" );
120
138
}
121
139
}
122
140
}
123
141
124
142
125
143
private void showToast (String message ) {
126
- Toast .makeText (getApplicationContext (), message , Toast .LENGTH_SHORT ).show ();
144
+ Toast toast = Toast .makeText (getApplicationContext (), message , Toast .LENGTH_SHORT );
145
+ toast .show ();
146
+ //Hide the previous toast
147
+ if (previousToast != null )
148
+ previousToast .cancel ();
149
+ previousToast = toast ;
127
150
}
128
151
129
152
0 commit comments