connect method
Connects to the device and returns its new DeviceStatus.
Does nothing if already connecting or connected, or if not configured. Sets status to DeviceStatus.disconnected if permissions are missing or onConnect throws.
Implementation
@nonVirtual
Future<DeviceStatus> connect() async {
// Fast out if already connecting or connected to the device.
if (isConnecting) return status;
if (!isConfigured) {
warning('$runtimeType has not been configured - cannot connect to it.');
return status;
}
status = DeviceStatus.connecting;
if (!(await hasPermissions())) {
warning(
'$runtimeType has not the permissions required to connect. '
'Call requestPermissions() before calling connect.',
);
return status = DeviceStatus.disconnected;
}
info('$runtimeType - Trying to connect to device of type: $typeName.');
try {
status = await onConnect();
} catch (error) {
warning('$runtimeType - Error connecting to device of type: $typeName. $error');
status = DeviceStatus.disconnected;
}
return status;
}