-
Notifications
You must be signed in to change notification settings - Fork 0
/
BluetoothService.java
394 lines (342 loc) · 12.3 KB
/
BluetoothService.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package cz.zcu.fav.remotestimulatorcontrol.service;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Arrays;
public class BluetoothService extends Service {
// region Constants
private static final Object lock = new Object();
// Logovací tag
@SuppressWarnings("unused")
private static final String TAG = "BluetoothService";
private static final String ACTION_PREFIX = "cz.zcu.fav.remotestimulatorcontrol.action.";
// Akce service
public static final String ACTION_DEVICE_NAME = ACTION_PREFIX + "DEVICE_NAME";
public static final String ACTION_STATE_CHANGE = ACTION_PREFIX + "BLUETOOTH_STATE_CHANGE";
public static final String ACTION_CONNECTION_FAILED = ACTION_PREFIX + "CONNECTION_FAILED";
public static final String ACTION_CONNECTION_LOST = ACTION_PREFIX + "CONNECTION_LOST";
public static final String ACTION_DATA_RECEIVED = ACTION_PREFIX + "DATA_RECEIVED";
public static final String ACTION_REQUEST_STATE_CHANGE = ACTION_PREFIX + "REQUEST_STATE_CHANGE";
public static final String ACTION_SEND_DATA = ACTION_PREFIX + "SEND_DAT";
// Názvy konstant představující proměnné pro různé akce
public static final String DEVICE_NAME = "device_name";
public static final String DEVICE_MAC = "device_mac";
public static final String STATE_CHANGE = "state_change";
public static final String DATA_RECEIVED_BYTES = "data_received_bytes";
public static final String DATA_RECEIVED_BUFFER = "data_received_buffer";
public static final String REQUEST_STATE = "request_state";
public static final String REQUEST_STATE_OFF = "request_state_off";
public static final String REQUEST_STATE_ON = "request_state_on";
public static final String DEVICE = "device";
public static final String DATA_CONTENT = "data_content";
// Stav připojení zařízení
// Výchozí stav
public static final int STATE_NONE = 0;
// Čekám na příchozí spojení
public static final int STATE_LISTEN = 1;
// Připojuji se k zařízení
public static final int STATE_CONNECTING = 2;
// Jsem spojený a můžu komunikovat
public static final int STATE_CONNECTED = 3;
// endregion
// region Variables
private static boolean running = false;
private final BroadcastReceiver mStatusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(ACTION_REQUEST_STATE_CHANGE)) {
final String state = intent.getStringExtra(REQUEST_STATE);
if (state.equals(REQUEST_STATE_OFF)) {
Log.d(TAG, "Odpojuji zařízení");
stop();
} else if (state.equals(REQUEST_STATE_ON)) {
Log.d(TAG, "Připojuji se k zařízení");
connectToDevice((BluetoothDevice) intent.getParcelableExtra(DEVICE));
}
}
}
};
private final BroadcastReceiver mSenderReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(ACTION_SEND_DATA)) {
final byte[] bytes = intent.getByteArrayExtra(DATA_CONTENT);
write(bytes);
}
}
};
// Vlákno udržující spojení
private ConnectedThread mConnectedThread;
// Vlákno ve kterém se vytváří nové spojení se zařízením
private ConnectThread mConnectThread;
// Bluetooth adapter
private BluetoothAdapter mBluetoothAdapter;
// Stav připojení
private int mState = STATE_NONE;
// endregion
// region Private methods
/**
* Nastaví interní stav, ve kterém se spojení nachází
*
* @param state Nový stav
* @see #STATE_NONE
* @see #STATE_LISTEN
* @see #STATE_CONNECTING
* @see #STATE_CONNECTED
*/
private synchronized void setState(int state) {
mState = state;
Intent intent = new Intent(ACTION_STATE_CHANGE);
intent.putExtra(STATE_CHANGE, state);
sendBroadcast(intent);
}
/**
* Ukončí všechna spojení a celou službu
*/
private synchronized void stop() {
setState(STATE_NONE);
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
if (mBluetoothAdapter != null) {
mBluetoothAdapter.cancelDiscovery();
}
}
/**
* Pokud dojde k selhání spojení
*/
private void connectionFailed() {
Log.w(TAG, "Připojení selhalo");
stop();
sendBroadcast(new Intent(ACTION_CONNECTION_FAILED));
}
/**
* Pokud dojde ke ztrátě spojení
*/
private void connectionLost() {
Log.i(TAG, "Připojení bylo ztraceno");
stop();
sendBroadcast(new Intent(ACTION_CONNECTION_LOST));
}
/**
* Zavolá se v případě vytvořeného spojení
*
* @param socket Bluetooth socket
* @param device Bluetooth device
*/
private synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
// Cancel the thread that completed the connection
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
Intent intent = new Intent(ACTION_DEVICE_NAME);
intent.putExtra(DEVICE_NAME, device.getName());
sendBroadcast(intent);
setState(STATE_CONNECTED);
}
/**
* Pokusí se vytvořít spojení s zařízením
*
* @param device {@link BluetoothDevice}
*/
private synchronized void connectToDevice(BluetoothDevice device) {
if (mBluetoothAdapter == null) {
return;
}
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
mConnectThread = new ConnectThread(device);
mConnectThread.start();
setState(STATE_CONNECTING);
}
/**
* Pošle data přes bluetooth
*
* @param out Data
*/
private void write(byte[] out) {
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (lock) {
if (mState != STATE_CONNECTED) {
return;
}
r = mConnectedThread;
}
// Perform the write unsynchronized
r.write(out);
}
// endregion
@Override
public void onCreate() {
Log.d("BluetoothService", "Služba spuštěna");
registerReceiver(mStatusReceiver, new IntentFilter(ACTION_REQUEST_STATE_CHANGE));
registerReceiver(mSenderReceiver, new IntentFilter(ACTION_SEND_DATA));
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
running = true;
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public boolean stopService(Intent name) {
stop();
running = false;
return super.stopService(name);
}
@Override
public void onDestroy() {
stop();
unregisterReceiver(mStatusReceiver);
unregisterReceiver(mSenderReceiver);
Log.d(TAG, "Služba ukončena");
super.onDestroy();
}
// region Public methods
/**
* @return True, pokud služba běží, jinak false
*/
public static boolean isRunning() {
return running;
}
// endregion
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
ConnectThread(BluetoothDevice device) {
this.mmDevice = device;
BluetoothSocket tmp = null;
try {
// Vytvoření nezabezpečeného spojení
// Nikdo neví, proč to nejde jednoduššeji
Class[] clsArr = new Class[STATE_LISTEN];
clsArr[STATE_NONE] = Integer.TYPE;
Method method = device.getClass().getMethod("createRfcommSocket", clsArr);
Object[] objArr = new Object[STATE_LISTEN];
objArr[STATE_NONE] = STATE_LISTEN;
tmp = (BluetoothSocket) method.invoke(device, objArr);
} catch (Exception e) {
e.printStackTrace();
}
mmSocket = tmp;
}
@Override
public void run() {
setName("ConnectThread");
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException e) {
try {
mmSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
connectionFailed();
return;
}
synchronized (BluetoothService.this) {
mConnectThread = null;
}
connected(mmSocket, mmDevice);
}
void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "Selhalo odpojení socketu", e);
}
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "Nepodařilo se vytvořit dočasné sockety", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
@Override
public void run() {
byte[] buffer = new byte[1024];
int count;
while (true) {
try {
Arrays.fill(buffer, (byte)0);
count = mmInStream.read(buffer);
Log.d(TAG, "Received: " + new String(buffer, 0, count));
Intent intent = new Intent(ACTION_DATA_RECEIVED);
intent.putExtra(DATA_RECEIVED_BYTES, count);
intent.putExtra(DATA_RECEIVED_BUFFER, buffer);
sendBroadcast(intent);
} catch (Exception e) {
connectionLost();
break;
}
}
}
void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
Log.d(TAG, "Bylo odesláno: " + Arrays.toString(buffer));
} catch (IOException e) {
Log.e(TAG, "Nastala neočekávaná vyjímka během zápisu dat", e);
}
}
void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "Selhalo odpojení socketu", e);
}
}
}
}