【解決問題】FlutterBlue在安卓手機上沒法鏈接藍牙設備,掃描緩慢

如今的FlutterBlue在安卓手機上很難搜索鏈接,在iOS上是沒問題的,進行下列更改能夠快速修復這個問題,可是會損失掉一些功能,不能經過指定Service的UUID搜索到設備(由於uuids數組被改爲了 [])。

懶人能夠直接用我fork以後修改的版本,修改YML文件的flutter_blue地址以下:java

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/android

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

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.github

簡單的說,就是不少手機系統到了新版本,可是對新版本的硬件API支持的不好,強制換成老版本的用法就行了。但願做者加一個字段來控制具體用什麼版本的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());

本文禁止任何類型轉載

相關文章
相關標籤/搜索