赞
踩
可以使用开源usb-serial-for-android
库进行串口通信
usb-serial-for-android
依赖项到项目中。在项目的 build.gradle
文件中添加以下内容:dependencies { // 其他依赖项... implementation 'com.github.mik3y:usb-serial-for-android:3.5.1' // 其他依赖项... }
2、在AndroidManifest.xml 文件中添加 USB 权限声明:
<uses-permission android:name="android.permission.USB_PERMISSION" />
3. 创建一个类来处理串口通信:
- import android.content.Context;
- import android.hardware.usb.UsbDevice;
- import android.hardware.usb.UsbDeviceConnection;
- import android.hardware.usb.UsbManager;
-
- import com.hoho.android.usbserial.driver.CdcAcmSerialDriver;
- import com.hoho.android.usbserial.driver.ProbeTable;
- import com.hoho.android.usbserial.driver.UsbSerialDriver;
- import com.hoho.android.usbserial.driver.UsbSerialPort;
- import com.hoho.android.usbserial.driver.UsbSerialProber;
-
- import java.io.IOException;
- import java.util.List;
-
- public class USBSerialHelper {
- private UsbManager usbManager;
- private UsbSerialPort usbSerialPort;
- private UsbDeviceConnection connection;
- private OnDataReceivedListener onDataReceivedListener;
- private ReadThread readThread;
-
- public interface OnDataReceivedListener {
- void onDataReceived(byte[] data);
- }
-
- public USBSerialHelper(Context context, OnDataReceivedListener listener) {
- usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
- onDataReceivedListener = listener;
- }
-
- public void openDevice() throws IOException {
- ProbeTable customTable = new ProbeTable();
- customTable.addProduct(0x2341, 0x0043, CdcAcmSerialDriver.class);
-
- UsbSerialProber prober = new UsbSerialProber(customTable);
- List<UsbSerialDriver> availableDrivers = prober.findAllDrivers(usbManager);
-
- if (!availableDrivers.isEmpty()) {
- UsbSerialDriver driver = availableDrivers.get(0);
- UsbDevice device = driver.getDevice();
-
- connection = usbManager.openDevice(device);
- usbSerialPort = driver.getPorts().get(0);
- usbSerialPort.open(connection);
- usbSerialPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
-
- readThread = new ReadThread();
- readThread.start();
- } else {
- throw new IOException("No USB serial device found");
- }
- }
-
- public void closeDevice() {
- if (readThread != null) {
- readThread.interrupt();
- readThread = null;
- }
-
- if (usbSerialPort != null) {
- try {
- usbSerialPort.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- usbSerialPort = null;
- }
-
- if (connection != null) {
- connection.close();
- connection = null;
- }
- }
-
- public void sendData(byte[] data) throws IOException {
- if (usbSerialPort != null) {
- usbSerialPort.write(data, 1000);
- }
- }
-
- private class ReadThread extends Thread {
- @Override
- public void run() {
- byte[] buffer = new byte[1024];
- int numBytes;
-
- while (!isInterrupted()) {
- try {
- numBytes = usbSerialPort.read(buffer, 1000);
- if (numBytes > 0) {
- byte[] data = new byte[numBytes];
- System.arraycopy(buffer, 0, data, 0, numBytes);
- onDataReceivedListener.onDataReceived(data);
- }
- } catch (IOException e) {
- e.printStackTrace();
- break;
- }
- }
- }
- }

4. 在代码中使用串口通信类:
- public class MainActivity extends AppCompatActivity implements USBSerialHelper.OnDataReceivedListener {
- private USBSerialHelper usbSerialHelper;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- usbSerialHelper = new USBSerialHelper(this, this);
-
- try {
- usbSerialHelper.openDevice();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- // 示例:发送数据
- byte[] sendData = "Hello, USB!".getBytes();
- try {
- usbSerialHelper.sendData(sendData);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
-
- usbSerialHelper.closeDevice();
- }
-
- @Override
- public void onDataReceived(byte[] data) {
- // 处理接收到的数据
- String receivedData = new String(data);
- Log.d("USBSerialHelper", "Received data: " + receivedData);
- }
- }

在 MainActivity
的 onCreate()
方法中创建了一个 USBSerialHelper
实例,并调用 openDevice()
方法打开 USB 设备进行通信。
然后,使用 sendData()
方法发送数据到 USB 设备。将字符串 "Hello, USB!" 转换为字节数组,并发送给 USB 设备。
最后,在 onDataReceived()
方法中处理接收到的数据。将接收到的字节数组转换为字符串,并打印到日志中。
请确保在使用之前已经获取了 USB 权限,并根据实际的 USB 设备进行相应的配置(例如波特率、数据位、停止位、校验位等)。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。