Flutter蓝牙设备连接失败:Android错误码133求助
蓝牙连接错误(Android-code:133)排查与解决
我在开发蓝牙设备连接功能时,能正常获取周边可用设备列表,但点击设备后无法完成连接,出现以下错误:
FlutterBluePlusException | connect | android-code: 133 | ANDROID_SPECIFIC_ERROR
相关代码
状态类定义
import 'package:flutter/material.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:hydration_monitor/Helper/CustomWidgets.dart'; import 'dart:io' show Platform; class BluetoothScreen extends StatefulWidget { const BluetoothScreen({Key? key}) : super(key: key); @override State<BluetoothScreen> createState() => _BluetoothScreenState(); } class _BluetoothScreenState extends State<BluetoothScreen> { bool isScan = false; List deviceDataSend =[]; BluetoothDevice? connectedDevice;
扫描设备代码
void startScanning() async { deviceDataSend.clear(); setState(() { isScan = true; }); await FlutterBluePlus.startScan(); FlutterBluePlus.onScanResults.listen((results) { for (ScanResult result in results) { bool isDuplicate = deviceDataSend.any((device) => device['remoteId'] == result.device.remoteId); if (!isDuplicate) { if (!deviceDataSend.contains(result.device)) { setState(() { deviceDataSend.add(result.device); }); } } } }); }
停止扫描代码
stopScan()async{ setState(() { isScan = false; }); await FlutterBluePlus.stopScan(); }
设备连接代码
connectDevice(BluetoothDevice device)async{ try { await device.connect(); } catch (e) { print("Error connecting to device: $e"); } }
页面构建代码
@override Widget build(BuildContext context) { double _height = MediaQuery.of(context).size.height; double _width = MediaQuery.of(context).size.width; return Scaffold( appBar: BaseAppBar( appBar: AppBar(), title: CustomLabelText(Text: "BluetoothScreen",bgColor: Colors.white,size: _width / 22,Bold: true), ), body: BgContainer( child: Stack( children: [ Padding( padding: EdgeInsets.symmetric(horizontal: _width / 25), child: Column( children: [ SizedBox(height: _height / 35,), isScan ? CustomButton(Text: 'Stop', width: 3, button: (){stopScan();},) : CustomButton(Text: 'Pair', width: 3, button: (){startScanning();},), SizedBox(height: _height / 35,), Divider(), Container( height: _height / 1.5, child: ListView.builder( shrinkWrap: true, itemCount: deviceDataSend.length, itemBuilder: (context, index) { return ListTile( onTap: (){ connectDevice(deviceDataSend[index]); }, title: CustomLabelText( Text: deviceDataSend[index]['remoteId'].toString(), ), trailing: CustomLabelText( Text: deviceDataSend[index]['platformName'], ), ); }, ) ) ], ), ) ], ), ), ); } }
解决方案
连接前必须停止扫描:Android系统中,蓝牙扫描会占用底层资源,不停止扫描直接连接大概率会失败。修改连接方法,先停止扫描再执行连接:
connectDevice(BluetoothDevice device) async { if (isScan) { await stopScan(); } try { await device.connect( autoConnect: false, timeout: const Duration(seconds: 10), ); setState(() { connectedDevice = device; }); } catch (e) { print("Error connecting to device: $e"); } }修正扫描重复判断逻辑:原代码中用
deviceDataSend.contains(result.device)判断重复不准确,因为对象引用不同,改用设备唯一标识remoteId判断:void startScanning() async { deviceDataSend.clear(); setState(() { isScan = true; }); // 添加扫描超时,避免持续扫描占用资源 await FlutterBluePlus.startScan(timeout: const Duration(seconds: 10)); FlutterBluePlus.onScanResults.listen((results) { for (ScanResult result in results) { bool isDuplicate = deviceDataSend.any((existingDevice) => (existingDevice as BluetoothDevice).remoteId == result.device.remoteId ); if (!isDuplicate) { setState(() { deviceDataSend.add(result.device); }); } } }); }检查权限与蓝牙状态:Android 12及以上版本,需在
AndroidManifest.xml中申请BLUETOOTH_SCAN、BLUETOOTH_CONNECT权限,并且确保设备蓝牙处于开启状态。处理设备配对需求:如果目标蓝牙设备需要配对,需提前在系统设置完成配对,或者监听FlutterBluePlus的配对请求事件,在App内处理配对流程。
内容的提问来源于stack exchange,提问作者Ah Zeem khatib
相关产品推荐
相关产品推荐

