使用flutter_blue

时间:2019-02-06 20:23:06

标签: mobile async-await flutter

我正在尝试flutter_blue连接到蓝牙设备并读取通过它共享的数据。 就我而言,上一个屏幕已经确保设备已连接,并且 将设备发送到下一个屏幕。在下一个屏幕中,我正在尝试阅读 特征。

因此,在新屏幕中,我一直在调用另一个函数来发现服务,并 阅读以下特征。

@override
  Widget build(BuildContext context) {
    // Here is just a placeholder for a list of mock units
    discoverServices();
    .....
  }

  void discoverServices() {
      // device is : connected device passed from another screen.
      device.discoverServices().then((s) {
      services = s;
      for (BluetoothService service in services) {
        for (BluetoothCharacteristic c in service.characteristics) {

          if (c.uuid == new Guid('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')) {
            print('uuid found');
            _readCharacteristic(c)

          }
        }
      }
  }


Future<void> _readCharacteristic(BluetoothCharacteristic c) async {

    try {
            data = await device.readCharacteristic(c);

            setState((){
                   // setting value for different object
                    myDevice.setValue(data);
            });

    } catch(e){
            print(e);

    }

  }
一旦在discoverServices()中找到特征,就会调用

_readCharacteristic。   并且,一旦_readCharacteristic完成,我将尝试为对象设置状态值。   但是,它没有读取任何内容或进行任何设置。

我错过了使用Flutterblue应该实现的任何东西吗?任何想法都会有所帮助。

0 个答案:

没有答案