refactor(android): 重构Android端实现使用官方SDK替代反射调用
- 移除反射机制,直接集成CH934XLib.jar官方SDK - 添加CH934XManager初始化逻辑并处理Application上下文兼容性 - 实现设备枚举、串口打开关闭等核心功能的直接调用 - 集成IDataCallback和IModemStatus回调处理 - 更新异常回调机制,支持原生层主动推送异常事件 - 优化设备连接状态管理和USB权限处理 - 为Dart模型类添加相等性比较和哈希码实现 - 更新示例应用UI以适配新的串口索引选择逻辑
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
@@ -30,6 +30,16 @@ class Ch934xSerialPortInfo {
|
||||
driverName: map['driverName'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is Ch934xSerialPortInfo &&
|
||||
other.portIndex == portIndex &&
|
||||
other.devicePath == devicePath &&
|
||||
other.driverName == driverName;
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(portIndex, devicePath, driverName);
|
||||
}
|
||||
|
||||
/// 文档 4.1.4 中 `UsbHelper.getCH934XDeviceList` 返回的设备信息。
|
||||
@@ -104,4 +114,28 @@ class Ch934xDeviceInfo {
|
||||
serialPorts: ports,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
other is Ch934xDeviceInfo &&
|
||||
other.deviceId == deviceId &&
|
||||
other.vendorId == vendorId &&
|
||||
other.productId == productId &&
|
||||
other.deviceType == deviceType &&
|
||||
other.serialNumber == serialNumber &&
|
||||
other.productName == productName &&
|
||||
other.manufacturerName == manufacturerName &&
|
||||
other.interfaceCount == interfaceCount;
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(
|
||||
deviceId,
|
||||
vendorId,
|
||||
productId,
|
||||
deviceType,
|
||||
serialNumber,
|
||||
productName,
|
||||
manufacturerName,
|
||||
interfaceCount,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user