stopNotify method

  1. @override
Future<bool?> stopNotify({
  1. required String serviceUuid,
  2. required String characteristicUuid,
})
override

stopNotify stops listening to notifications from a BLE characteristic.

Implementation

@override
Future<bool?> stopNotify({
  required String serviceUuid,
  required String characteristicUuid,
}) async {
  if (_currentConnected == null) {
    log("No device connected");
    return false;
  }

  final services = await _currentConnected!.discoverServices();
  final service = services.firstWhereOrNull((s) => s.uuid.toLowerCase() == serviceUuid.toLowerCase());
  if (service == null) {
    log("Service not found: $serviceUuid");
    return false;
  }

  try {
    final characteristic = await service.getCharacteristic(characteristicUuid);
    if (!characteristic.isNotifying) {
      log("Is not notifying");
      return true;
    }
    await characteristic.stopNotifications();
    if (_notifications.containsKey(characteristicUuid)) {
      _notifications[characteristicUuid]!.cancel();
      _notifications.remove(characteristicUuid);
    }
    return true;
  } catch (e) {
    log("Error getting characteristic: $e");
    return false;
  }
}