You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter使用flutter_bluetooth_serial连接设备抛出java.io.IOException错误

Flutter Bluetooth Serial 连接错误排查及替代方案

错误详情

PlatformException(connect_error, read failed, socket might closed or timeout, read ret: -1, java.io.IOException: read failed, socket might closed or timeout, read ret: -1
          at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:952)
          at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:966)
          at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:522)
at io.github.edufolly.flutterbluetoothserial.BluetoothConnection.connect(BluetoothConnection.java:57)
at io.github.edufolly.flutterbluetoothserial.BluetoothConnection.connect(BluetoothConnection.java:64)
at io.github.edufolly.flutterbluetoothserial.FlutterBluetoothSerialPlugin$FlutterBluetoothSerialMethodCallHandler.lambda$onMethodCall$4$io-github-edufolly-flutterbluetoothserial-FlutterBluetoothSerialPlugin$FlutterBluetoothSerialMethodCallHandler(FlutterBluetoothSerialPlugin.java:1007)
at io.github.edufolly.flutterbluetoothserial.FlutterBluetoothSerialPlugin$FlutterBluetoothSerialMethodCallHandler$$ExternalSyntheticLambda7.r

连接多款不同蓝牙设备均触发该错误。

触发错误的代码

List<BluetoothDevice> bondedDevices = await fbs.getBondedDevices();
for(BluetoothDevice device in bondedDevices.where((x)=>x.address == device.device.address)){
  BluetoothConnection.toAddress(device.address).then(
    (connection){
      print('connected');
      _connection =connection;
    }
  ).catchError((onError)=>{
    print("caughtError:"),
    print(onError)
  });
}

问题修复步骤

  • 修正过滤逻辑错误:代码里的x.address == device.device.address存在逻辑问题,device是循环变量,device.device未定义,相当于用空值匹配设备地址,根本找不到目标设备。替换为实际要连接的设备MAC地址:
    // 替换成你要连接的设备MAC地址
    const targetMac = "XX:XX:XX:XX:XX:XX";
    final bondedDevices = await fbs.getBondedDevices();
    for (final device in bondedDevices.where((d) => d.address == targetMac)) {
      try {
        final connection = await BluetoothConnection.toAddress(device.address);
        print('connected');
        _connection = connection;
      } catch (e) {
        print("caughtError: $e");
      }
    }
    
  • 补全蓝牙权限:Android端必须配置足够的权限,在AndroidManifest.xml里添加:
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- Android 12及以上额外需要 -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    
    同时要在代码中动态申请这些权限,否则系统会拒绝蓝牙操作。
  • 检查设备状态:确保手机蓝牙已开启,目标设备处于可连接状态(未被其他设备占用、电量充足)。
  • 添加超时控制:给连接操作设置超时,避免长时间卡在等待状态:
    try {
      final connection = await BluetoothConnection.toAddress(device.address)
          .timeout(const Duration(seconds: 10));
      print('connected');
      _connection = connection;
    } on TimeoutException catch (_) {
      print("连接超时");
    } catch (e) {
      print("caughtError: $e");
    }
    

非BLE蓝牙替代库

  • flutter_blue_plus:社区活跃度高,支持经典蓝牙(部分功能)和BLE,维护更新及时。
  • bluetooth_serial_port:专注经典蓝牙串口通信,API简洁,适配安卓和iOS平台。
  • flutter_bluetooth:覆盖经典蓝牙的搜索、配对、连接、数据传输全流程,功能完整。

内容的提问来源于stack exchange,提问作者JokeyLo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 15:15:36