本人手头的板子是Esp32,因为工作需要要把板子与手机连线,通过手机Otg功能进行串口通信,开发了一个简单的串口通信助手,现在记录下:
这里为了方便直接用lib。用到的是usb-serial-for-android
1.在project的build.gradle里添加:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
2.在app/module的build.gradle里添加:
dependencies {
implementation 'com.github.mik3y:usb-serial-for-android:3.4.0'
}
3.Sync project
4.在AndroidManifest.xml添加:
<activity
android:name="..."
...>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
其中,在res目录下新建xml目录,添加device_filter.xml文件,实例如下:
<resources>
<usb-device vendor-id="1027" product-id="24577" />
<usb-device vendor-id="1027" product-id="24592" />
<usb-device vendor-id="1027" product-id="24593" />
<usb-device vendor-id="1027" product-id="24596" />
<usb-device vendor-id="1027" product-id="24597" />
<usb-device vendor-id="4292" product-id="60000" />
<usb-device vendor-id="4292" product-id="60016" />
<usb-device vendor-id="4292" product-id="60017" />
<usb-device vendor-id="1659" product-id="8963" />
<usb-device vendor-id="1659" product-id="9123" />
<usb-device vendor-id="1659" product-id="9139" />
<usb-device vendor-id="1659" product-id="9155" />
<usb-device vendor-id="1659" product-id="9171" />
<usb-device vendor-id="1659" product-id="9187" />
<usb-device vendor-id="1659" product-id="9203" />
<usb-device vendor-id="6790" product-id="21795" />
<usb-device vendor-id="6790" product-id="29987" />
<usb-device vendor-id="9025" />
<usb-device vendor-id="5824" product-id="1155" />
<usb-device vendor-id="1003" product-id="8260" />
<usb-device vendor-id="7855" product-id="4" />
<usb-device vendor-id="3368" product-id="516" />
<usb-device vendor-id="1155" product-id="22336" />
resources>
注: 这里的vendor-id和product-id每个设备都不一样,电脑上连接设备后通过设备管理器-端口-右键属性-详细信息-选择硬件id查看对应的vendor-id和product-id后,按照格式后进行添加。
需要先允许应用权限:
// Find all available drivers from attached devices.
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
if (availableDrivers.isEmpty()) {
return;
}
// Open a connection to the first available driver.
UsbSerialDriver driver = availableDrivers.get(0);
UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
if (connection == null) {
// add UsbManager.requestPermission(driver.getDevice(), ..) handling here
Toast.makeText(this, "Permission Error", Toast.LENGTH_SHORT).show();
return;
}
port = driver.getPorts().get(0); // Most devices have just one port (port 0)
try {
port.open(connection);
} catch (IOException e) {
e.printStackTrace();
}
try {
//这里设置
port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
} catch (IOException e) {
e.printStackTrace();
}
if(port.isOpen()){
//progressBar.setVisibility(View.VISIBLE);
//output("Set port Success!");
mSerialIoManager = new SerialInputOutputManager(port, mListener);//添加监听
mSerialIoManager.start();//开始
}
这里我是连上后就立刻开始监听,Listener设置如下:
private final SerialInputOutputManager.Listener mListener =
new SerialInputOutputManager.Listener() {
@Override
public void onRunError(Exception e) {
Log.d("TAG", "Runner stopped.");
}
@Override
public void onNewData(final byte[] data) {
//TODO 新的数据
runOnUiThread(()->{output(new String(data));});
//这里每次听到数据都直接打印出结果
}
};
try {
//这里1000可以设置为0,表示一直等到数据发送成功
port.write(strSend.getBytes(),1000);
Toast.makeText(this, "Write Successful", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
try {
port.close();
} catch (IOException e) {
e.printStackTrace();
}
if(!port.isOpen()){
//output("Close Port!");
//progressBar.setVisibility(View.INVISIBLE);
}
这样就实现了一个简单的串口通信app,后续还需要针对需求进行修改。