Flutter用easy_onvif连EZVIZ H8C摄像头遇DioException超时求助
解决EZVIZ H8C摄像头Flutter easy_onvif连接超时问题
针对你遇到的easy_onvif连接超时但RTSP流正常的问题,可按以下步骤排查解决:
1. 指定正确的OnVIF端口与服务路径
EZVIZ摄像头的OnVIF服务通常不使用默认80端口,你扫描到的8000或9010大概率是OnVIF服务端口。同时部分设备需要指定特定的服务路径,修改连接代码如下:
Future<void> connect({required String ip}) async { try { onvif = await Onvif.connect( host: ip, port: 8000, // 优先尝试8000,失败再换9010 path: '/onvif/device_service', // 多数OnVIF设备的标准路径 username: 'admin', password: 'password', ); log('💚 OnVIFService: connected to $ip'); await getToken(); } catch (error) { log('💚 OnVIFService: connection error: $error'); } }
2. 延长Dio超时时间
easy_onvif底层依赖Dio,默认20秒超时可能不足以应对摄像头的响应速度,自定义Dio实例延长超时:
Future<void> connect({required String ip}) async { try { final customDio = Dio(); customDio.options.connectTimeout = const Duration(seconds: 60); customDio.options.receiveTimeout = const Duration(seconds: 60); onvif = await Onvif.connect( host: ip, port: 8000, path: '/onvif/device_service', username: 'admin', password: 'password', dio: customDio, // 传入自定义Dio实例 ); log('💚 OnVIFService: connected to $ip'); await getToken(); } catch (error) { log('💚 OnVIFService: connection error: $error'); } }
3. 检查摄像头OnVIF权限设置
EZVIZ设备可能需要单独开启OnVIF服务,并设置独立的OnVIF账号密码(与RTSP账号可能不同):
- 打开萤石云APP,找到对应设备的「设置」-「高级设置」-「OnVIF设置」
- 确认OnVIF服务已开启,核对用户名密码是否正确,同时确保PTZ控制权限已启用
4. 用工具验证OnVIF服务可用性
使用OnVIF Device Manager等桌面工具连接摄像头,验证设备的OnVIF服务是否正常:
- 如果工具也无法连接,说明是摄像头端的设置问题,需重新检查设备配置
- 如果工具能正常连接并控制PTZ,说明问题出在Flutter代码的参数配置上,回到步骤1调整端口/路径
5. 修复调用逻辑的错误
你的按钮点击逻辑存在问题:连接失败时仍会调用getToken(),导致token为空。调整为:
onPressed: () async { await onvifService.connect(ip: '192.168.0.103'); if (onvifService.onvif != null) { await onvifService.moveLeft(); debugPrint("working"); } else { debugPrint("connection failed"); } }
内容的提问来源于stack exchange,提问作者Shahriar Saem
相关产品推荐
相关产品推荐

