0

I'm trying to subscribe to the acceleration characteristic and aftherwards send a value to the write characteristic in order to get values out of it.

however the following error keeps occuring. I've already tried the different solutions from: https://github.com/pauldemarco/flutter_blue/issues/295 but things don't seem to work.

discoverServices(BluetoothDevice device) async {
    if (device == null) return;
    print('Services looked for');
    late bool check;

    List<BluetoothService> services = await device.discoverServices();
    for (BluetoothService service in services) {
      // do something with service
      if (service.uuid.toString().toLowerCase() == serviceUUID) {
        for (BluetoothCharacteristic characteristic
            in service.characteristics) {
          if (characteristic.uuid.toString().toLowerCase() ==
              characteristicReadUUID) {
            targetReadCharacteristic = characteristic;
            _readData(targetReadCharacteristic);
            print('Notify is: $targetReadCharacteristic');
          }
          await Future.delayed(Duration(seconds: 3));
          if (characteristic.uuid.toString().toLowerCase() ==
              characteristicWriteUUID) {
            targetWriteCharacteristic = characteristic;
            _writeData(
                targetWriteCharacteristic,
                Uint8List.fromList(
                    [1, 99, 47, 77, 101, 97, 115, 47, 69, 67, 71, 47, 49, 51]));
            print('Write is: $targetWriteCharacteristic');
          }
        }
      }
    }
  }

  _writeData(characteristic, data) async {
    if (characteristic == null) return e;
    try {
      List<int> bytes = data;
      await characteristic.write(bytes, withoutResponse: false);
      print(characteristic);
      print("data: Send: $data");
    } catch (e) {}
  }

  _readData(characteristic) async {
    if (!characteristic.isNotifying) {
      await characteristic.setNotifyValue(true);
      print(characteristic);
      characteristic.value.listen((value) {
        print('read value is $value');
        List<int> readData = new List.from(value);

        if (readData.isNotEmpty && readData != []) {
          print('BLE read data: $readData');
        }
      });
    }
  }

I've already tried the different solutions from: https://github.com/pauldemarco/flutter_blue/issues/295 but things don't seem to work.

0 Answers0