Advertisement
rajath_pai

BluetoothState

Aug 4th, 2021
1,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.89 KB | None | 0 0
  1. part of flutter_bluetooth_serial;
  2.  
  3. class BluetoothState {
  4.   final int underlyingValue;
  5.   final String stringValue;
  6.  
  7.   const BluetoothState.fromString(String string)
  8.       : this.underlyingValue = (string == 'STATE_OFF'
  9.                 ? 10
  10.                 : string == 'STATE_TURNING_ON'
  11.                     ? 11
  12.                     : string == 'STATE_ON'
  13.                         ? 12
  14.                         : string == 'STATE_TURNING_OFF'
  15.                             ? 13
  16.                             :
  17.  
  18.                             //ring == 'STATE_BLE_OFF'         ? 10 :
  19.                             string == 'STATE_BLE_TURNING_ON'
  20.                                 ? 14
  21.                                 : string == 'STATE_BLE_ON'
  22.                                     ? 15
  23.                                     : string == 'STATE_BLE_TURNING_OFF'
  24.                                         ? 16
  25.                                         : string == 'ERROR'
  26.                                             ? -1
  27.                                             : -2 // Unknown, if not found valid
  28.             ),
  29.         this.stringValue = ((string == 'STATE_OFF' ||
  30.                     string == 'STATE_TURNING_ON' ||
  31.                     string == 'STATE_ON' ||
  32.                     string == 'STATE_TURNING_OFF' ||
  33.  
  34.                     //ring == 'STATE_BLE_OFF'         ||
  35.                     string == 'STATE_BLE_TURNING_ON' ||
  36.                     string == 'STATE_BLE_ON' ||
  37.                     string == 'STATE_BLE_TURNING_OFF' ||
  38.                     string == 'ERROR')
  39.                 ? string
  40.                 : 'UNKNOWN' // Unknown, if not found valid
  41.             );
  42.  
  43.   const BluetoothState.fromUnderlyingValue(int value)
  44.       : this.underlyingValue = (((value >= 10 && value <= 16) || value == -1)
  45.                 ? value
  46.                 : -2 // Unknown, if not found valid
  47.             ),
  48.         this.stringValue = (value == 10
  49.                 ? 'STATE_OFF'
  50.                 : value == 11
  51.                     ? 'STATE_TURNING_ON'
  52.                     : value == 12
  53.                         ? 'STATE_ON'
  54.                         : value == 13
  55.                             ? 'STATE_TURNING_OFF'
  56.                             :
  57.  
  58.                             //lue == 10 ? 'STATE_BLE_OFF'         : // Just for symetry in code :F
  59.                             value == 14
  60.                                 ? 'STATE_BLE_TURNING_ON'
  61.                                 : value == 15
  62.                                     ? 'STATE_BLE_ON'
  63.                                     : value == 16
  64.                                         ? 'STATE_BLE_TURNING_OFF'
  65.                                         : value == -1
  66.                                             ? 'ERROR'
  67.                                             : 'UNKNOWN' // Unknown, if not found valid
  68.             );
  69.  
  70.   String toString() => 'BluetoothState.$stringValue';
  71.  
  72.   int toUnderlyingValue() => underlyingValue;
  73.  
  74.   static const STATE_OFF = BluetoothState.fromUnderlyingValue(10);
  75.   static const STATE_TURNING_ON = BluetoothState.fromUnderlyingValue(11);
  76.   static const STATE_ON = BluetoothState.fromUnderlyingValue(12);
  77.   static const STATE_TURNING_OFF = BluetoothState.fromUnderlyingValue(13);
  78.  
  79.   //atic const STATE_BLE_OFF = BluetoothState.STATE_OFF; // Just for symetry in code :F
  80.   static const STATE_BLE_TURNING_ON = BluetoothState.fromUnderlyingValue(14);
  81.   static const STATE_BLE_ON = BluetoothState.fromUnderlyingValue(15);
  82.   static const STATE_BLE_TURNING_OFF = BluetoothState.fromUnderlyingValue(16);
  83.  
  84.   static const ERROR = BluetoothState.fromUnderlyingValue(-1);
  85.   static const UNKNOWN = BluetoothState.fromUnderlyingValue(-2);
  86.  
  87.   operator ==(Object other) {
  88.     return other is BluetoothState &&
  89.         other.underlyingValue == this.underlyingValue;
  90.   }
  91.  
  92.   @override
  93.   int get hashCode => underlyingValue.hashCode;
  94.  
  95.   bool get isEnabled => this == STATE_ON;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement