123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package cn.minbb.serial.activities;
- import android.os.Bundle;
- import android.os.Environment;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.File;
- import java.util.Arrays;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import cn.minbb.serial.R;
- import cn.minbb.serialport.SerialPortManager;
- import cn.minbb.serialport.listener.OnDataProgressListener;
- import cn.minbb.serialport.listener.OnOpenSerialPortListener;
- import cn.minbb.serialport.listener.OnSerialPortDataListener;
- public class MainActivity extends AppCompatActivity {
- @BindView(R.id.open_serial_port)
- Button openSerial;
- @BindView(R.id.close_serial_port)
- Button closeSerial;
- @BindView(R.id.send_string)
- Button sendString;
- @BindView(R.id.send_file)
- Button sendFile;
- @BindView(R.id.edit_text)
- EditText editText;
- @BindView(R.id.text_view)
- TextView textView;
- private String TAG = "MainActivity";
- private SerialPortManager serialPortManager = SerialPortManager.getInstance();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ButterKnife.bind(this);
- closeSerial.setEnabled(false);
- sendString.setEnabled(false);
- sendFile.setEnabled(false);
- serialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
- @Override
- public void onSuccess(File device) {
- Toast.makeText(getApplicationContext(), "串口打开成功", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onFail(File device, Status status) {
- Toast.makeText(getApplicationContext(), "串口打开失败:" + status.getDescription(), Toast.LENGTH_SHORT).show();
- }
- });
- serialPortManager.setOnSerialPortDataListener(new OnSerialPortDataListener() {
- @Override
- public void onDataReceived(byte[] bytes, String data) {
- runOnUiThread(() -> {
- Toast.makeText(getApplicationContext(), data, Toast.LENGTH_SHORT).show();
- // setText(data, data.length());
- setText(Arrays.toString(bytes), bytes.length);
- // try {
- // if (serialPortManager.sendBytes(bytes1)) {
- // Toast.makeText(getApplicationContext(), "发送成功 = " + data, Toast.LENGTH_SHORT).show();
- // } else {
- // Toast.makeText(getApplicationContext(), "发送失败", Toast.LENGTH_SHORT).show();
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // Toast.makeText(getApplicationContext(), "发送失败 " + e.getMessage(), Toast.LENGTH_SHORT).show();
- // }
- });
- }
- @Override
- public void onDataSent(byte[] bytes) {
- }
- });
- }
- private void setText(String data, double size) {
- String string = "data = " + data + ": " + size + "\n" + textView.getText();
- textView.setText(string);
- }
- public void onClick(View view) throws Exception {
- switch (view.getId()) {
- case R.id.open_serial_port:
- if (null != serialPortManager.openDefaultSerialPort()) {
- Toast.makeText(getApplicationContext(), "串口打开成功", Toast.LENGTH_SHORT).show();
- openSerial.setEnabled(false);
- closeSerial.setEnabled(true);
- sendString.setEnabled(true);
- sendFile.setEnabled(true);
- } else {
- Toast.makeText(getApplicationContext(), "串口打开失败", Toast.LENGTH_SHORT).show();
- }
- break;
- case R.id.close_serial_port:
- Toast.makeText(getApplicationContext(), "串口已关闭", Toast.LENGTH_SHORT).show();
- serialPortManager.closeSerialPort();
- openSerial.setEnabled(true);
- closeSerial.setEnabled(false);
- sendString.setEnabled(false);
- sendFile.setEnabled(false);
- break;
- case R.id.clear_send:
- editText.setText("");
- break;
- case R.id.clear_text:
- textView.setText("");
- break;
- case R.id.send_string:
- // byte[] reportID = {0x0};
- // byte[] data = ByteUtil.addBytes(reportID, editText.getText().toString().getBytes());
- String s = editText.getText().toString();
- serialPortManager.sendString(s, (progress, index, total, bytes) -> runOnUiThread(() -> setText(Arrays.toString(bytes), bytes.length)));
- setText("发送字符:" + s, s.length());
- Toast.makeText(getApplicationContext(), "数据已发送", Toast.LENGTH_SHORT).show();
- break;
- case R.id.send_file:
- File file = new File(Environment.getExternalStorageDirectory(), "Documents/image.jpg");
- Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_SHORT).show();
- serialPortManager.sendFile(file, (progress, index, total, bytes) -> runOnUiThread(() -> setText("发送文件进度", progress)));
- break;
- }
- }
- }
|