使用flutter_bluetooth_serial无法连接移动设备,求助解决连接异常
解决flutter_bluetooth_serial无法连接移动设备的问题
问题概述
使用flutter_bluetooth_serial连接蓝牙设备(地址2C:4D:DE:7E:75:0A)时,触发PlatformException(connect_error, read failed, socket might closed or timeout, read ret: -1)错误,无法建立连接。
关键错误日志
selected 2C:4D:DE:7E:75:0A D/FlutterBluePlugin(15068): Connecting to 2C:4D:DE:7E:75:0A (id: 12) D/BluetoothAdapter(15068): cancelDiscovery(): called by: com.example.blue_classic W/BluetoothAdapter(15068): getBluetoothService() called with no BluetoothManagerCallback I/flutter (15068): Cannot connect, exception occured I/flutter (15068): 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 I/flutter (15068): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:685) I/flutter (15068): at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:697) I/flutter (15068): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:374) I/flutter (15068): at io.github.edufolly.flutterbluetoothserial.BluetoothConnection.connect(BluetoothConnection.java:57) I/flutter (15068): at io.github.edufolly.flutterbluetoothserial.BluetoothConnection.connect(BluetoothConnection.java:64) I/flutter (15068): at io.github.edufolly.flutterbluetoothserial.FlutterBluetoothSerialPlugin$FlutterBluetoothSerialMethodCallHandler.lambda$onMethodCall$4$io-github-edufolly-flutterbluetoothserial-FlutterBluetoothSerialPlugin$FlutterBluetoothSerialMethodCallHandler(FlutterBluetoothSerialPlugin.java:1007) I/flutter (15068): at io.github.edufolly.flutterbluetoothserial.FlutterBluetoothSerialPlugin$FlutterBluetoothSerialMethodCallHandler$$ExternalSyntheticLambda2.r
排查与解决步骤
- 确认蓝牙配对状态:确保两台设备已完成蓝牙配对,且配对未过期。若未配对,先在系统设置中完成配对操作,再尝试连接。
- 检查蓝牙权限:确保应用已获取
BLUETOOTH、BLUETOOTH_ADMIN、ACCESS_FINE_LOCATION(Android 10及以上)权限,且权限已被用户授予。在AndroidManifest.xml中添加对应权限声明,并在运行时请求动态权限。 - 彻底停止扫描再连接:日志显示调用了
cancelDiscovery(),但需确保发起连接前扫描已完全停止。扫描会占用蓝牙资源,可在连接前延迟1-2秒,确保扫描进程结束。 - 使用正确的UUID:连接时需使用SPP协议标准UUID(
00001101-0000-1000-8000-00805F9B34FB),若目标设备使用自定义UUID,需确认两者一致后再连接。 - 排查设备兼容性:部分设备蓝牙模块对SPP协议支持有限,尝试连接其他蓝牙设备测试,排除硬件兼容性问题。
- 重启蓝牙与设备:重启两台设备的蓝牙模块,或直接重启设备,清除蓝牙缓存后再尝试连接。
- 更新插件版本:确认flutter_bluetooth_serial插件为最新版本,旧版本可能存在已知连接bug,更新后可解决部分问题。
内容的提问来源于stack exchange,提问作者Mubashar
相关产品推荐
相关产品推荐

