startNotifications method

  1. @override
Future<bool> startNotifications(
  1. String deviceId,
  2. String service,
  3. String characteristic,
  4. int timeout,
  5. void callback(
    1. String result
    ),
)
override

Implementation

@override
Future<bool> startNotifications(
    String deviceId,
    String service,
    String characteristic,
    int timeout,
    void Function(String result) callback) async {
  final key =
      "notification|$deviceId|${service.toLowerCase()}|${characteristic.toLowerCase()}";
  _notificationStreams[key] = _startNotificationsEventChannel
      .receiveBroadcastStream()
      .listen((event) {
    if (event == null || event["status"] == "error") {
      _notificationStreams[key]?.cancel();
      return;
    }
    if (event[key] != null) {
      callback(event[key]["value"]);
    }
  });

  final result = await _methodChannel
      .invokeMethod<Map<dynamic, dynamic>?>('startNotifications', {
    'deviceId': deviceId,
    'service': service,
    'characteristic': characteristic,
    'timeout': timeout,
  });
  if (result == null || result["value"] == null) {
    throw PlatformException(
        code: "startNotifications(): error retrieving value");
  }
  return result["value"];
}