-
Notifications
You must be signed in to change notification settings - Fork 33
/
TerminalActivity.java
277 lines (233 loc) · 8.96 KB
/
TerminalActivity.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
package com.macroyau.blue2serial.demo;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import com.macroyau.blue2serial.BluetoothDeviceListDialog;
import com.macroyau.blue2serial.BluetoothSerial;
import com.macroyau.blue2serial.BluetoothSerialListener;
/**
* This is an example Bluetooth terminal application built using the Blue2Serial library.
*
* @author Macro Yau
*/
public class TerminalActivity extends AppCompatActivity
implements BluetoothSerialListener, BluetoothDeviceListDialog.OnDeviceSelectedListener {
private static final int REQUEST_ENABLE_BLUETOOTH = 1;
private BluetoothSerial bluetoothSerial;
private ScrollView svTerminal;
private TextView tvTerminal;
private EditText etSend;
private MenuItem actionConnect, actionDisconnect;
private boolean crlf = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_terminal);
// Find UI views and set listeners
svTerminal = (ScrollView) findViewById(R.id.terminal);
tvTerminal = (TextView) findViewById(R.id.tv_terminal);
etSend = (EditText) findViewById(R.id.et_send);
etSend.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
String send = etSend.getText().toString().trim();
if (send.length() > 0) {
bluetoothSerial.write(send, crlf);
etSend.setText("");
}
}
return false;
}
});
// Create a new instance of BluetoothSerial
bluetoothSerial = new BluetoothSerial(this, this);
}
@Override
protected void onStart() {
super.onStart();
// Check Bluetooth availability on the device and set up the Bluetooth adapter
bluetoothSerial.setup();
}
@Override
protected void onResume() {
super.onResume();
// Open a Bluetooth serial port and get ready to establish a connection
if (bluetoothSerial.checkBluetooth() && bluetoothSerial.isBluetoothEnabled()) {
if (!bluetoothSerial.isConnected()) {
bluetoothSerial.start();
}
}
}
@Override
protected void onStop() {
super.onStop();
// Disconnect from the remote device and close the serial port
bluetoothSerial.stop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_terminal, menu);
actionConnect = menu.findItem(R.id.action_connect);
actionDisconnect = menu.findItem(R.id.action_disconnect);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_connect) {
showDeviceListDialog();
return true;
} else if (id == R.id.action_disconnect) {
bluetoothSerial.stop();
return true;
} else if (id == R.id.action_crlf) {
crlf = !item.isChecked();
item.setChecked(crlf);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void invalidateOptionsMenu() {
if (bluetoothSerial == null)
return;
// Show or hide the "Connect" and "Disconnect" buttons on the app bar
if (bluetoothSerial.isConnected()) {
if (actionConnect != null)
actionConnect.setVisible(false);
if (actionDisconnect != null)
actionDisconnect.setVisible(true);
} else {
if (actionConnect != null)
actionConnect.setVisible(true);
if (actionDisconnect != null)
actionDisconnect.setVisible(false);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_ENABLE_BLUETOOTH:
// Set up Bluetooth serial port when Bluetooth adapter is turned on
if (resultCode == Activity.RESULT_OK) {
bluetoothSerial.setup();
}
break;
}
}
private void updateBluetoothState() {
// Get the current Bluetooth state
final int state;
if (bluetoothSerial != null)
state = bluetoothSerial.getState();
else
state = BluetoothSerial.STATE_DISCONNECTED;
// Display the current state on the app bar as the subtitle
String subtitle;
switch (state) {
case BluetoothSerial.STATE_CONNECTING:
subtitle = getString(R.string.status_connecting);
break;
case BluetoothSerial.STATE_CONNECTED:
subtitle = getString(R.string.status_connected, bluetoothSerial.getConnectedDeviceName());
break;
default:
subtitle = getString(R.string.status_disconnected);
break;
}
if (getSupportActionBar() != null) {
getSupportActionBar().setSubtitle(subtitle);
}
}
private void showDeviceListDialog() {
// Display dialog for selecting a remote Bluetooth device
BluetoothDeviceListDialog dialog = new BluetoothDeviceListDialog(this);
dialog.setOnDeviceSelectedListener(this);
dialog.setTitle(R.string.paired_devices);
dialog.setDevices(bluetoothSerial.getPairedDevices());
dialog.showAddress(true);
dialog.show();
}
/* Implementation of BluetoothSerialListener */
@Override
public void onBluetoothNotSupported() {
new AlertDialog.Builder(this)
.setMessage(R.string.no_bluetooth)
.setPositiveButton(R.string.action_quit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setCancelable(false)
.show();
}
@Override
public void onBluetoothDisabled() {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, REQUEST_ENABLE_BLUETOOTH);
}
@Override
public void onBluetoothDeviceDisconnected() {
invalidateOptionsMenu();
updateBluetoothState();
}
@Override
public void onConnectingBluetoothDevice() {
updateBluetoothState();
}
@Override
public void onBluetoothDeviceConnected(String name, String address) {
invalidateOptionsMenu();
updateBluetoothState();
}
@Override
public void onBluetoothSerialRead(String message) {
// Print the incoming message on the terminal screen
tvTerminal.append(getString(R.string.terminal_message_template,
bluetoothSerial.getConnectedDeviceName(),
message));
svTerminal.post(scrollTerminalToBottom);
}
@Override
public void onBluetoothSerialWrite(String message) {
// Print the outgoing message on the terminal screen
tvTerminal.append(getString(R.string.terminal_message_template,
bluetoothSerial.getLocalAdapterName(),
message));
svTerminal.post(scrollTerminalToBottom);
}
/* Implementation of BluetoothDeviceListDialog.OnDeviceSelectedListener */
@Override
public void onBluetoothDeviceSelected(BluetoothDevice device) {
// Connect to the selected remote Bluetooth device
bluetoothSerial.connect(device);
}
/* End of the implementation of listeners */
private final Runnable scrollTerminalToBottom = new Runnable() {
@Override
public void run() {
// Scroll the terminal screen to the bottom
svTerminal.fullScroll(ScrollView.FOCUS_DOWN);
}
};
}