Skip to content

Commit 05d99b0

Browse files
Replaced send button by bulb icon. Implemented delay functionality properly
1 parent e032b49 commit 05d99b0

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

BluetoothBulbControl/app/src/main/java/me/devanshmaurya/bluetoothcontrol/LEDControlActivity.java

+32-9
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import android.content.Intent;
88
import android.os.AsyncTask;
99
import android.os.Bundle;
10+
import android.os.Handler;
1011
import android.support.annotation.Nullable;
1112
import android.support.v7.app.AppCompatActivity;
1213
import android.view.View;
1314
import android.widget.Button;
1415
import android.widget.CheckBox;
1516
import android.widget.EditText;
17+
import android.widget.ImageView;
1618
import android.widget.Toast;
1719

1820
import java.io.IOException;
@@ -21,8 +23,9 @@
2123

2224
public class LEDControlActivity extends AppCompatActivity {
2325

24-
private Button buttonOn, buttonDisconnect;
26+
private Button buttonDisconnect;
2527
private CheckBox bulb1Checkbox, bulb2Checkbox;
28+
private ImageView buttonOn;
2629
private EditText textCommand, startDelayET, stopAfterET;
2730
private String address;
2831
private ProgressDialog progress;
@@ -31,6 +34,8 @@ public class LEDControlActivity extends AppCompatActivity {
3134
private boolean isBTConnected = false;
3235
//Default Universally Unique Identifier, HC-05's default
3336
static final UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
37+
private boolean on = false;
38+
private Toast previousToast;
3439

3540
@Override
3641
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -39,7 +44,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3944
Intent receivedIntent = getIntent();
4045
address = receivedIntent.getStringExtra(MainActivity.EXTRA_ADDRESS);
4146

42-
textCommand = findViewById(R.id.command_edit_text);
4347
buttonOn = findViewById(R.id.button_on);
4448
buttonDisconnect = findViewById(R.id.button_disconnect);
4549
bulb1Checkbox = findViewById(R.id.bulb1_checkbox);
@@ -54,13 +58,25 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5458
public void onClick(View view) {
5559
if (bluetoothSocket != null) {
5660
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+
}
5874
byte[] commandBytes = command.getBytes();
59-
String startDelay = startDelayET.getText().toString();
75+
final String startDelay = startDelayET.getText().toString();
6076
String stopAfter = stopAfterET.getText().toString();
6177

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";
6480
commandBytes = command.getBytes();
6581
}
6682

@@ -79,6 +95,8 @@ public void onClick(View view) {
7995
//If the bluetooth socket is busy
8096
if (bluetoothSocket != null) {
8197
try {
98+
//To prevent functioning of bulb on next start of app
99+
bluetoothSocket.getOutputStream().write("STOP".getBytes());
82100
bluetoothSocket.close(); //Close connection
83101
} catch (IOException e) {
84102
showToast("Error!!!");
@@ -103,7 +121,7 @@ public void onBulbCheckBoxClicked(View view) throws IOException{
103121
} else {
104122
//If box is unchecked, make pin 0 as output, as nothing is connected there,
105123
//bulb won't glow
106-
bluetoothOS.write("0".getBytes());
124+
bluetoothOS.write("01".getBytes());
107125
showToast("Bulb 1 is not functional");
108126
}
109127
break;
@@ -115,15 +133,20 @@ public void onBulbCheckBoxClicked(View view) throws IOException{
115133
} else {
116134
//If box is unchecked, make pin 0 as output, as nothing is connected there,
117135
//bulb won't glow
118-
bluetoothOS.write("0".getBytes());
136+
bluetoothOS.write("02".getBytes());
119137
showToast("Bulb 1 is not functional");
120138
}
121139
}
122140
}
123141

124142

125143
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;
127150
}
128151

129152

BluetoothBulbControl/app/src/main/res/layout/activity_ledcontrol.xml

+2-8
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,11 @@
2828
android:id="@+id/bulb2_checkbox"/>
2929
</LinearLayout>
3030

31-
<EditText
32-
android:layout_width="match_parent"
33-
android:layout_height="wrap_content"
34-
android:hint="Enter the data to send..."
35-
android:id="@+id/command_edit_text"/>
36-
37-
<Button
31+
<ImageView
3832
android:layout_width="wrap_content"
3933
android:layout_height="wrap_content"
4034
android:layout_gravity="center_horizontal"
41-
android:text="Send"
35+
android:src="@drawable/bulb_off"
4236
android:layout_margin="30dp"
4337
android:id="@+id/button_on"/>
4438

0 commit comments

Comments
 (0)