【解决问题】FlutterBlue在安卓手机上无法连接蓝牙设备,扫描缓慢

针对FlutterBlue在部分Android设备上搜索连接困难的问题,本文提供了有效解决方案,包括使用较低版本API、修改代码实现及调整搜索策略。这些方法虽牺牲了部分功能,如UUID指定搜索,但显著提升了在特定Android手机上的连接速度和成功率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在的FlutterBlue在安卓手机上很难搜索连接,在iOS上是没问题的,进行下列更改可以快速修复这个问题,但是会损失掉一些功能,不能通过指定Service的UUID搜索到设备(因为uuids数组被改成了 [])。

懒人可以直接用我fork之后修改的版本,修改YML文件的flutter_blue地址如下:

  flutter_blue: 
    git: 
      url: git://github.com/mjl0602/flutter_blue.git

If you can’t use flutterblue connect device with Android Phone. You can try low version Api of Android. These apis are deprecated but worked very well in some Android Phone. These
Phones are bad support with new API: you can call the function successful, but scan and connect will be very slow and easy connect fail. To solve this problem, change file:
android/src/main/java/com/pauldemarco/flutterblue/

see new file on: https://github.com/mjl0602/fl...

This change didn't solve this problem completely. To solve this problem, must add new args to control the api version. but not use Build.VERSION.SDK_INT. It's works bad.

简单的说,就是很多手机系统到了新版本,但是对新版本的硬件API支持的很差,强制换成老版本的用法就好了。希望作者加一个字段来控制具体用什么版本的API来搜索。

  • Change connect way.
// old code,hardly connect device on Red Mi Note 4 
// BluetoothGatt gattServer = device.connectGatt(registrar.activity(), options.getAndroidAutoConnect(), mGattCallback);

// improve MI phone connect speed. If didn't call connect,Mi phone can't connect success
BluetoothGatt gattServer = device.connectGatt(registrar.activity(), false, mGattCallback);
gattServer.connect();
  • Use old version api
// use old version api
private void startScan(MethodCall call, Result result) {
    byte[] data = call.arguments();
    Protos.ScanSettings settings;
    try {
        settings = Protos.ScanSettings.newBuilder().mergeFrom(data).build();
        // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        //     startScan21(settings);
        // } else {
            startScan18(settings);
        // }
        result.success(null);
    } catch (Exception e) {
        result.error("startScan", e.getMessage(), e);
    }
}

private void stopScan() {
    // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    //     stopScan21();
    // } else {
        stopScan18();
    // }
}
  • Remove uuids arg.
// boolean success = mBluetoothAdapter.startLeScan(uuids, getScanCallback18());
boolean success = mBluetoothAdapter.startLeScan(getScanCallback18());

本文禁止任何类型转载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值