安卓串口通訊三

核心代碼如下:

java 复制代码
package com.example.comandroid;

import static android.content.ContentValues.TAG;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.jackiepenghe.serialportlibrary.MultipleSerialPortManager;
import com.jackiepenghe.serialportlibrary.OnSerialPortDataChangedListener;
import com.jackiepenghe.serialportlibrary.SerialPortManager;
import com.sscl.baselibrary.utils.ConversionUtil;
import com.sscl.baselibrary.utils.DebugUtil;
import com.sscl.baselibrary.utils.ToastUtil;

import java.nio.charset.Charset;

/**
* 功能描述:
* 版权所有:
* 版本:1.0
* 作者:
* 创建时间 ${DATE} ${hour}
* 邮箱 :2848444745@qq.com
*
*/

public class MainActivity extends AppCompatActivity {
    private Button btnOpen;
    private Button btnClose;
    private Button btnSend;
    private TextView text;
    private String serialPort="/dev/ttyS1";//串口路徑
//    private String serialPort="/dev/ttyS2";//串口路徑
//    private String serialPort="/dev/ttyS3";//串口路徑
//    private String serialPort="/dev/ttyS4";//串口路徑
    private int baudRate = 9600;//波特率
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnClose=findViewById(R.id.btn_close);
        btnOpen=findViewById(R.id.btn_open);
        btnSend=findViewById(R.id.btn_send);
        text=findViewById(R.id.textView);

        //打開串口
        btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openSerialPort();
            }
        });

        //關閉串口
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MultipleSerialPortManager.closeSerialPort(serialPort);
            }
        });
        //發送數據
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendData("發送測試");
            }
        });

    }

    /**
     * 串口监听
     */
    private final OnSerialPortDataChangedListener onSerialPortDataChangedListener = (data, size) -> {
        byte[] cache = new byte[size];
        System.arraycopy(data, 0, cache, 0, size);
        DebugUtil.warnOut(TAG, "cache = " + ConversionUtil.byteArrayToHexStr(cache));
        DebugUtil.warnOut(TAG, "cache = " + new String(cache));
        String result = ConversionUtil.byteArrayToHexStr(cache);
        text.setText(result);


    };

    /**
     * 打开串口
     */
    private void openSerialPort() {
        boolean open = MultipleSerialPortManager.openSerialPort(serialPort, baudRate, onSerialPortDataChangedListener);
        if (open) {
            ToastUtil.toastLong(this, "串口打開成功");
            DebugUtil.warnOut(TAG, "串口打開成功");
            this.serialPort = serialPort;
        } else {
            DebugUtil.warnOut(TAG, "串口打開失敗");
            ToastUtil.toastLong(this, "串口打開失敗");
        }


    }

    /**
     * 获取串口信息
     */
    private void getSerialPortInfo() {
        String[] allDevices = MultipleSerialPortManager.getAllDevices();
        if (allDevices.length == 0) {
            DebugUtil.warnOut(TAG, "allDevices length == 0");
        }
        for (int i = 0; i < allDevices.length; i++) {
            DebugUtil.warnOut(TAG, "allDevices[" + i + "] = " + allDevices[i]);
        }
        String[] allDevicesPath = MultipleSerialPortManager.getAllDevicesPath();
        if (allDevicesPath.length == 0) {
            DebugUtil.warnOut(TAG, "allDevicesPath length == 0");
        }
        for (int i = 0; i < allDevicesPath.length; i++) {
            DebugUtil.warnOut(TAG, "allDevicesPath[" + i + "] = " + allDevicesPath[i]);
        }

    }

   // 关闭串口
    private void closeSerialPort() {
        if (serialPort == null) {
            return;
        }
        MultipleSerialPortManager.closeSerialPort(serialPort);
    }

    // 發送數據
    private void sendData(String data) {
        boolean succeed= MultipleSerialPortManager.writeData(serialPort, "1234567890", Charset.forName("UTF-8"));
        if (!succeed){
            ToastUtil.toastLong(this, "發生失敗");
        }else{
            ToastUtil.toastLong(this, "發送成功");
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeSerialPort();
    }
}

想要完整的了解,可以看下完整的dome,這我也給出來了完整dome如下:

https://download.csdn.net/download/qq_41733851/89288862?spm=1001.2014.3001.5503

相关推荐
三少爷的鞋9 小时前
Java 线程自上而下,Kotlin 协程自下而上
android
好的佩奇10 小时前
Dart 之增强枚举
android·flutter·dart
梦影_11 小时前
MySQL进阶教程(五)—— SQL调优(下)
android·大数据·数据库·后端·sql·mysql·database
好的佩奇13 小时前
蓝牙之无线电与通信距离
android·flutter·蓝牙
vilya14 小时前
我把一套完整的 Python Agent 运行时塞进手机 APK:依赖闭包、Rust 轮子和三个真机坑
android
呆呆敲代码的小Y16 小时前
【实战精选面试题】C# 面试题详细解答,按难易程度排序
android·java·游戏·c#·游戏开发·unity面试题·c#面试题
思想觉悟17 小时前
SEAndroid实战
android·linux·c++
思想觉悟18 小时前
Aosp开发磨刀石之ADB速查
android·c++
应用市场20 小时前
用本机跑的开源音乐模型,给安卓小游戏做 20 秒无缝循环背景音乐
android·python·游戏开发·soundpool·音频处理
企业数字化笔记20 小时前
表单发布前怎么验收?版本引用、字段兼容和历史数据回归
android·数据挖掘·回归