-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerSupply.java
More file actions
281 lines (232 loc) · 8.26 KB
/
PowerSupply.java
File metadata and controls
281 lines (232 loc) · 8.26 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "bluetooth2";
Button start,on5,off5,on9,off9,on12,off12,stop;
Handler h;
int strt=0;
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private StringBuilder sb = new StringBuilder();
private ConnectedThread mConnectedThread;
// SPP UUID service
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// MAC-address of Bluetooth module (you must edit this line)
private static String address = "98:D3:31:20:7F:B8";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.button7); // button LED ON
stop = (Button) findViewById(R.id.button8); // button LED OFF
on5 = (Button)findViewById(R.id.button);
off5 = (Button)findViewById(R.id.button2);
on9 = (Button)findViewById(R.id.button3);
off9 = (Button)findViewById(R.id.button4);
on12 = (Button)findViewById(R.id.button5);
off12 = (Button)findViewById(R.id.button6);
start();
stop();
on5v();
off5v();
on9v();
off9v();
on12v();
off12v();
btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
checkBTState();
}
public void start(){
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
strt=1;
}
});
}
public void stop(){
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
strt=0;
}
});
}
public void on5v(){
on5.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(strt==1){
mConnectedThread.write(3);
}
}
});
}
public void off5v(){
off5.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(strt==1){
mConnectedThread.write(4);
}
}
});
}
public void on9v(){
on9.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(strt==1){
mConnectedThread.write(5);
}
}
});
}
public void off9v(){
off9.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(strt==1){
mConnectedThread.write(6);
}
}
});
}
public void on12v(){
on12.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(strt==1){
mConnectedThread.write(7);
}
}
});
}
public void off12v(){
off12.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(strt==1){
mConnectedThread.write(8);
}
}
});
}
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
if (Build.VERSION.SDK_INT >= 10) {
try {
final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[]{UUID.class});
return (BluetoothSocket) m.invoke(device, MY_UUID);
} catch (Exception e) {
Log.e(TAG, "Could not create Insecure RFComm Connection", e);
}
}
return device.createRfcommSocketToServiceRecord(MY_UUID);
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "...onResume - try connect...");
// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);
// Two things are needed to make a connection:
// A MAC address, which we got above.
// A Service ID or UUID. In this case we are using the
// UUID for SPP.
try {
btSocket = createBluetoothSocket(device);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}
// Discovery is resource intensive. Make sure it isn't going on
// when you attempt to connect and pass your message.
btAdapter.cancelDiscovery();
// Establish the connection. This will block until it connects.
Log.d(TAG, "...Connecting...");
try {
btSocket.connect();
Log.d(TAG, "....Connection ok...");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}
// Create a data stream so we can talk to server.
Log.d(TAG, "...Create Socket...");
mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "...In onPause()...");
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
}
}
private void checkBTState() {
// Check for Bluetooth support and then check to make sure it is turned on
// Emulator doesn't support Bluetooth and will return null
if (btAdapter == null) {
errorExit("Fatal Error", "Bluetooth not support");
} else {
if (btAdapter.isEnabled()) {
Log.d(TAG, "...Bluetooth ON...");
} else {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
private void errorExit(String title, String message) {
Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
finish();
}
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
/* Call this from the main activity to send data to the remote device */
public void write(int message) {
Log.d(TAG, "...Data to send: " + message + "...");
//byte[] msgBuffer = message.getBytes();
try {
mmOutStream.write(message);
} catch (IOException e) {
Log.d(TAG, "...Error data send: " + e.getMessage() + "...");
}
}
}
}