setNotifyValue для двух характеристик BLE - PullRequest
0 голосов
/ 15 октября 2019

Я не могу установить setNotifyValue для более чем одного признака в одном и том же сервисе BLE. Приложение всегда падает, когда вызывается второй setNotifyValue.

Если я закомментирую setNotifyValue по одной из характеристик и перекомпилирую, приложение будет работать хорошо и будет предоставлять живой поток входящих данных.


class DeviceScreen extends StatelessWidget {
  const DeviceScreen({Key key, this.device}) : super(key: key);
  final BluetoothDevice device;
  static String CHARACTERISTIC_UUID_BTN =
      "0000ab0a-1212-efde-1523-785fef13d123";
  static String CHARACTERISTIC_UUID_TEMP =
      "0000beef-1212-efde-1523-785fef13d123";

  Widget _myService(List<BluetoothService> services) {
    Stream<List<int>> btn_stream;
    Stream<List<int>> temp_stream;
    services.forEach((service) {
      service.characteristics.forEach((character) {
        if (character.uuid.toString() == CHARACTERISTIC_UUID_BTN) {
          character.setNotifyValue(true);
          btn_stream = character.value;
        }
        if (character.uuid.toString() == CHARACTERISTIC_UUID_TEMP) {
          character.setNotifyValue(true);
          temp_stream = character.value;
        }
      });
    });
    return Container(
        //eric code
        child: StreamBuilder<List<int>>(
      stream: btn_stream,
      builder: (BuildContext context, AsyncSnapshot<List<int>> btn_snapshot) {
        return StreamBuilder(
          stream: temp_stream,
          builder:
              (BuildContext context, AsyncSnapshot<List<int>> temp_snapshot) {
            if (btn_snapshot.hasError)
              return Text('Error : ${btn_snapshot.error}');
            if (temp_snapshot.hasError)
              return Text('Error : ${temp_snapshot.error}');

            if ((btn_snapshot.data != null) && (temp_snapshot.data != null)) {
              if ((btn_snapshot.data.length != 0) &&
                  (temp_snapshot.data.length != 0)) {
                int incoming_counts = btn_snapshot.data[0];
                int incoming_temp = temp_snapshot.data[0];
                return Text(incoming_counts.toString() +
                    " " +
                    incoming_temp.toString());
              } else {
                return Text("Synced! Waiting for input");
                //return Text(btn_snapshot.data.toString() +
                //  " " +
                //  temp_snapshot.data.toString());
              }
            } else {
              return Text("Waiting to sync...");
              //return Text(btn_snapshot.data.toString() +
              //    " " +
              //    temp_snapshot.data.toString());
            }
          },
        );
      },
    ));
  }

Я надеюсь, что оба параметра: "messages_counts" и "messages_temp" будут обновляться в режиме реального времени по мере поступления новых данных с периферийного устройства Bluetooth. Однако при назначении потоков во второй раз вызывается character.setNotifyValue (true);выдает ошибку:

Произошло исключение. PlatformException (PlatformException (set_notification_error, ошибка при записи дескриптора, ноль))

...