-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArdcon.java
More file actions
222 lines (198 loc) · 8.65 KB
/
Ardcon.java
File metadata and controls
222 lines (198 loc) · 8.65 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.Bluetooth.ardcon;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.UUID;
public class Ardcon extends AppCompatActivity {
public final static String MODULE_MAC = "00:0E:EA:CF:45:15"; //"98:D3:34:90:6F:A1";
public final static int REQUEST_ENABLE_BT = 1;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
BluetoothAdapter bta;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
ConnectedThread btt = null;
Button btnFwd, btnRight, btnLeft, btnBack, btnStop;
TextView response;
boolean lightflag = false;
boolean relayFlag = true;
public Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("[BLUETOOTH]", "Creating listeners");
response = (TextView) findViewById(R.id.response);
btnFwd = (Button) findViewById(R.id.forward_btn);
btnRight= (Button) findViewById(R.id.right_btn);
btnLeft= (Button) findViewById(R.id.left_btn);
btnBack = (Button) findViewById(R.id.rear_btn);
btnStop = (Button) findViewById(R.id.stop_btn);
btnFwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("[BLUETOOTH]", "Attempting to send data");
if (mmSocket.isConnected() && btt != null) { //if we have connection to the bluetoothmodule
if (!lightflag) {
String sendtxt = "F";
btt.write(sendtxt.getBytes());
response.setText("Car move forward");
lightflag = true;
} else {
String sendtxt = "S";
btt.write(sendtxt.getBytes());
response.setText("Car Stop");
lightflag = false;
}
} else {
Toast.makeText(Ardcon.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
});
btnRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("[BLUETOOTH]", "Attempting to send data");
if (mmSocket.isConnected() && btt != null) { //if we have connection to the bluetoothmodule
if (!lightflag) {
String sendtxt = "R";
btt.write(sendtxt.getBytes());
response.setText("Car turn Right");
lightflag = true;
} else {
String sendtxt = "S";
btt.write(sendtxt.getBytes());
response.setText("Car Stop");
lightflag = false;
}
} else {
Toast.makeText(Ardcon.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
});
btnLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("[BLUETOOTH]", "Attempting to send data");
if (mmSocket.isConnected() && btt != null) { //if we have connection to the bluetoothmodule
if (!lightflag) {
String sendtxt = "L";
btt.write(sendtxt.getBytes());
response.setText("Car turn Left");
lightflag = true;
} else {
String sendtxt = "S";
btt.write(sendtxt.getBytes());
response.setText("Car Stop");
lightflag = false;
}
} else {
Toast.makeText(Ardcon.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("[BLUETOOTH]", "Attempting to send data");
if (mmSocket.isConnected() && btt != null) { //if we have connection to the bluetoothmodule
if (!lightflag) {
String sendtxt = "B";
btt.write(sendtxt.getBytes());
response.setText("Car move Back");
lightflag = true;
} else {
String sendtxt = "S";
btt.write(sendtxt.getBytes());
response.setText("Car Stop");
lightflag = false;
}
} else {
Toast.makeText(Ardcon.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("[BLUETOOTH]", "Attempting to send data");
if (mmSocket.isConnected() && btt != null) { //if we have connection to the bluetoothmodule
if (!lightflag) {
String sendtxt = "S";
btt.write(sendtxt.getBytes());
response.setText("Car Stop");
lightflag = true;
} else {
String sendtxt = "S";
btt.write(sendtxt.getBytes());
response.setText("Car Stop");
lightflag = false;
}
} else {
Toast.makeText(Ardcon.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
});
bta = BluetoothAdapter.getDefaultAdapter();
//if bluetooth is not enabled then create Intent for user to turn it on
if(!bta.isEnabled()){
Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBTIntent, REQUEST_ENABLE_BT);
}else{
initiateBluetoothProcess();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == REQUEST_ENABLE_BT){
initiateBluetoothProcess();
}
}
public void initiateBluetoothProcess(){
if(bta.isEnabled()){
//attempt to connect to bluetooth module
BluetoothSocket tmp = null;
mmDevice = bta.getRemoteDevice(MODULE_MAC);
//create socket
try {
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
mmSocket = tmp;
mmSocket.connect();
Log.i("[BLUETOOTH]","Connected to: "+mmDevice.getName());
}catch(IOException e){
try{mmSocket.close();}catch(IOException c){return;}
}
Log.i("[BLUETOOTH]", "Creating handler");
mHandler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
//super.handleMessage(msg);
if(msg.what == ConnectedThread.RESPONSE_MESSAGE){
String txt = (String)msg.obj;
if(response.getText().toString().length() >= 30){
response.setText("");
response.setText(txt);
}else{
response.setText("\n" + txt);
}
}
}
};
Log.i("[BLUETOOTH]", "Creating and running Thread");
btt = new ConnectedThread(mmSocket,mHandler);
btt.start();
}
}
}