【问题标题】:Flutter bluetooth serial plugin issue on iOS: MissingPluginExceptioniOS 上的 Flutter 蓝牙串行插件问题:MissingPluginException
【发布时间】:2021-05-07 17:59:20
【问题描述】:

我正在使用名为 flutter_bluetooth_serial 的 Flutter 蓝牙插件,虽然在为 Android 构建应用时它可以正常工作,但在 iOS 上却不能正常工作。

我得到的错误如下:

发生了异常。 MissingPluginException(MissingPluginException(在通道flutter_bluetooth_serial/methods上找不到方法getState的实现))

在这一行:

bluetoothSerial.state.then((state) => setState(() {

这是完整的代码:

  class _MainProgrammingScreenState extends State<MainProgrammingScreen> {
  FlutterBluetoothSerial bluetoothSerial = FlutterBluetoothSerial.instance;
  BluetoothConnection deviceConnection;
  bool bluetoothOn = false;
  bool get deviceConnected =>
      (deviceConnection != null && deviceConnection.isConnected);

  Future<bool> enableBluetooth() async {
    var btEnabled = await bluetoothSerial.requestEnable();
    setState(() {});
    return btEnabled;
  }

  Future<bool> connectToDevice() async {
    if (!(await enableBluetooth())) {
      return false;
    }

    //get bounded devices
    var boundedDevices = await bluetoothSerial.getBondedDevices();

    if (await bluetoothSerial.isDiscovering) {
      await bluetoothSerial.cancelDiscovery();
    }

    //bt scan
    for (int scanAttempt = 0; scanAttempt < 3; scanAttempt++) {
      await for (var discoveryResult in bluetoothSerial.startDiscovery()) {
        BluetoothDevice device = discoveryResult.device;
        //find device
        if (device.name != null && device.name.contains("DEVICE")) {
          BluetoothDevice device = device;
          //if device not just paired
          if (!boundedDevices.contains(device)) {
            //pair to device
            try {
              var deviceBounded =
                  await bluetoothSerial.bondDeviceAtAddress(device.address);
              if (!deviceBounded) {
                return false;
              }
            } catch (error) {
              print("bound device error: $error");
              return false;
            }
          }

          //try connect to device
          for (var deviceConnAttempt = 0;
              deviceConnAttempt < 3;
              deviceConnAttempt++) {
            try {
              deviceConnection =
                  await BluetoothConnection.toAddress(device.address);
              if (deviceConnection.isConnected) {
                deviceConnection.input.listen(btReceaveData);
                setState(() {});
                return true;
              }
            } catch (error) {
              print("device connection failed: $error");
              return false;
            }
          }
        }
      }
    }

    return false;
  }

  int intChar(str) => str.codeUnitAt(0);

  BtObj btObj = new BtObj();

  void btReceaveData(Uint8List buffer) {
    for (var byte in buffer) {
      if (byte == intChar('<')) {
        btObj.clear();
      } else if (byte == intChar('>')) {
        //decode obj
        btDecoder(btObj);
      } else {
        btObj.pushByte(byte);
      }
    }
  }

  Future<void> sendPack(Uint8List data, BtObjType dataType) async {
    try {
      BtObj tempObj = BtObj.fromList(data, dataType);
      deviceConnection.output.add(tempObj.list);
      //await deviceConnection.output.done;
    } catch (error) {
      print("error sending data to device: $error");
    }
  }

  void initState() {
    super.initState();

    bluetoothSerial.state.then((state) => setState(() {
          bluetoothOn = state == BluetoothState.STATE_ON;
        }));
    bluetoothSerial.onStateChanged().listen((state) => setState(() {
          bluetoothOn = state == BluetoothState.STATE_ON;
        }));
  }

  @override
  Widget build(BuildContext context) {
    return ...

知道 iOS 上会发生什么吗?

非常感谢。

【问题讨论】:

  • iOS 上的应用无法使用旧版蓝牙串行端口和 RFComm 配置文件。此插件不支持 iOS。
  • @Paulw11 哦,我明白了。谢谢!

标签: ios flutter bluetooth flutter-plugin


【解决方案1】:

该软件包不支持 IOS。 :(

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2019-05-18
    • 2023-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多