startNotifications method
Request the BluetoothCharacteristic to start notifying. This means that the Bluetooth device will give an event everytime that the value of the characteristic changes.
-
May throw NotSupportedError if the operation is not allowed. Check properties to see if it is supported.
-
May throw SecurityError if the characteristic is blocked from reading using the blocklist.
-
May throw NetworkError if the device is not connected or if there is an error with the communication.
-
May throw StateError if the characteristic is null.
See: stopNotifications, isNotifying, value and lastValue.
Implementation
Future<void> startNotifications() async {
try {
await _characteristic.startNotifications();
_isNotifying = true;
} catch (e) {
final error = e.toString().trim();
if (error.startsWith("NotSupportedError")) {
throw NotSupportedError(uuid);
} else if (error.startsWith("NetworkError")) {
throw NetworkError.withUUid(uuid);
} else if (error.startsWith("SecurityError")) {
throw SecurityError(uuid, error);
} else if (error.startsWith("InvalidStateError")) {
throw StateError("Characteristic is null");
}
rethrow;
}
}