https://toitlang.org/ logo
Below are Dart code snippets that retrieve the bat...
# general
m
Below are Dart code snippets that retrieve the battery from a BLE device. The flutter_reactive_ble package is used. A subscription to receive data on demand is created: ble.subscribeToCharacteristic(...), and then requests for a specific parameter are sent: _ble.writeCharacteristicWithoutResponse. In this case, the data being sent and received is a vector of bytes: a Uint8List of size 16. How would this snippet look in Toit if using the ble library? How can I implement a similar approach in Toit? How do I create a listener to receive notifications?
Copy code
final Uuid serviceUuid =   Uuid.parse('6e40fff0-b5a3-f393-e0a9-e50e24dcca9e');
final Uuid writeCharUuid = Uuid.parse('6e400002-b5a3-f393-e0a9-e50e24dcca9e');
final Uuid readCharUuid =  Uuid.parse('6e400003-b5a3-f393-e0a9-e50e24dcca9e');

final FlutterReactiveBle _ble = FlutterReactiveBle();

  void _initializeNotifications() {
    final QualifiedCharacteristic readChar = QualifiedCharacteristic(
      serviceId: serviceUuid,
      characteristicId: readCharUuid,
      deviceId: _deviceId!,
    );

    _notifySub = _ble
        .subscribeToCharacteristic(readChar)
        .listen(
          (data) {
            final Uint8List bytes = Uint8List.fromList(data);
            _handleIncomingData(bytes);
          },
          onError: (Object error) {
            print('Notification error: $error');
          },
        );
  requestBatteryLevel();
  }

  Future<void> requestBatteryLevel() async {
    sendRequest('Battery Level', [cmdBattery]);
  }