refactor(android): 重构Android端实现使用官方SDK替代反射调用

- 移除反射机制,直接集成CH934XLib.jar官方SDK
- 添加CH934XManager初始化逻辑并处理Application上下文兼容性
- 实现设备枚举、串口打开关闭等核心功能的直接调用
- 集成IDataCallback和IModemStatus回调处理
- 更新异常回调机制,支持原生层主动推送异常事件
- 优化设备连接状态管理和USB权限处理
- 为Dart模型类添加相等性比较和哈希码实现
- 更新示例应用UI以适配新的串口索引选择逻辑
This commit is contained in:
Developer
2026-07-06 16:48:14 +08:00
parent e0d1a1775d
commit 90de9121ff
4 changed files with 468 additions and 267 deletions
+30
View File
@@ -21,6 +21,22 @@ class MethodChannelCh934xSerial extends Ch934xSerialPlatform {
final StreamController<Ch934xException> _exceptionController =
StreamController<Ch934xException>.broadcast();
/// 接收来自原生层主动调用的 `onException` 消息。
Future<void> _onNativeMethodCall(MethodCall call) async {
if (call.method == 'onException') {
final args = call.arguments;
if (args is Map) {
_exceptionController.add(
Ch934xException(
type: (args['type'] as int?) ?? Ch934xExceptionType.unknown,
message: args['message'] as String?,
cause: args['cause'] as String?,
),
);
}
}
}
@override
Future<List<Ch934xDeviceInfo>> getDeviceList() async {
final raw = await methodChannel.invokeMethod<List<dynamic>>('getDeviceList');
@@ -86,6 +102,18 @@ class MethodChannelCh934xSerial extends Ch934xSerialPlatform {
return result ?? false;
}
@override
Future<bool> setActivePort(int serialPortIndex) async {
if (serialPortIndex < 0) {
return false;
}
final result = await methodChannel.invokeMethod<bool>(
'setActivePort',
<String, Object>{'serialPortIndex': serialPortIndex},
);
return result ?? false;
}
@override
Future<Uint8List> read(int length) async {
if (length <= 0) {
@@ -156,6 +184,8 @@ class MethodChannelCh934xSerial extends Ch934xSerialPlatform {
Future<void> setExceptionCallback(
void Function(Ch934xException exception) onException,
) async {
// 注册接收原生层主动推送的 `onException` 事件。
methodChannel.setMethodCallHandler(_onNativeMethodCall);
_exceptionController.stream.listen(onException);
await methodChannel.invokeMethod<void>('setExceptionCallback');
}