dart 我无法在Flutter应用程序中获得iOS中的蓝牙打印机列表

jgwigjjp  于 2023-05-26  发布在  Flutter
关注(0)|答案(1)|浏览(316)

我无法在flutter应用程序中获得iOS中的蓝牙打印机列表。
该应用程序在Android中运行完美,我也能够从Android设备打印。
我正在使用一个名为flutter_blue_plus 1.5.2的软件包,我安装在pubspec.yaml中:

-> flutter_blue_plus: ^1.3.1

我的iOS信息.plist

<key>NSBluetoothAlwaysUsageDescription</key> 
    <string>Need BLE permission</string> 
    <key> NSBluetoothPeripheralUsageDescription</key>
    <string>Need BLE permission</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key> 
    <string> Need Location permission</string>
    <key>NSLocationAlwaysUsageDescription</key> 
    <string>Need Location permission</string> 
    <key NSLocationWhenInUseUsageDescription</key>
    <string>Need Location permission</string>
    <key>NSBluetoothAlwaysUsageDescription</key>
    <string>This app uses bluetooth</string>

我正在扫描蓝牙设备,如下所示:

FlutterBluePlus flutterBlue = FlutterBluePlus.instance;
  List<ScanResult> scanResult;

  @override
  void initState() {
    super.initState();
    findDevices();
  }

  void findDevices() {
    // if(await Permission.b)
    if (flutterBlue.isOn != null) {
      flutterBlue.startScan(timeout: const Duration(seconds: 4));
      flutterBlue.scanResults.listen((results) {
        setState(() {
          scanResult = results;
        });
      });
      flutterBlue.stopScan();
    }
  }

我得到的错误,而试图调试应用程序在我们的iOS

2023-05-26 00:30:01.250916+0530 Runner[1044:250516] [CoreBluetooth] API MISUSE: <CBCentralManager: 0x282d5c000> can only accept this command while in the powered on state
2023-05-26 00:30:01.288553+0530 Runner[1044:250516] [CoreBluetooth] API MISUSE: <CBCentralManager: 0x282d5c000> can only accept this command while in the powered on state
s3fp2yjn

s3fp2yjn1#

从实现的Angular 来看,我建议只在.isOn返回true时才开始扫描,因为即使.isOn返回false,您也可以开始扫描,这表明蓝牙堆栈尚未准备好。这解释了您可以在调试控制台中看到的错误。
为了独立于Widget的初始化时间,我建议依赖于流.state,并且只在状态为BluetoothState.On时对FlutterBluePlus.instance进行操作。

相关问题