Advertisement
rajath_pai

BluetoothDevice

Aug 4th, 2021
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.47 KB | None | 0 0
  1. part of flutter_bluetooth_serial;
  2.  
  3. /// Represents information about the device. Could be out-of-date. // @TODO . add updating the info via some fn
  4. class BluetoothDevice {
  5.   /// Broadcasted friendly name of the device.
  6.   final String? name;
  7.  
  8.   /// MAC address of the device or identificator for platform system (if MAC addresses are prohibited).
  9.   final String address;
  10.  
  11.   /// Type of the device (Bluetooth standard type).
  12.   final BluetoothDeviceType type;
  13.  
  14.   /// Class of the device.
  15.   //final BluetoothClass bluetoothClass // @TODO . !BluetoothClass!
  16.  
  17.   /// Describes is device connected.
  18.   final bool isConnected;
  19.  
  20.   /// Bonding state of the device.
  21.   final BluetoothBondState bondState;
  22.  
  23.   /// Tells whether the device is bonded (ready to secure connect).
  24.   @Deprecated('Use `isBonded` instead')
  25.   bool get bonded => bondState.isBonded;
  26.  
  27.   /// Tells whether the device is bonded (ready to secure connect).
  28.   bool get isBonded => bondState.isBonded;
  29.  
  30.   /// Construct `BluetoothDevice` with given values.
  31.   const BluetoothDevice({
  32.     this.name,
  33.     required this.address,
  34.     this.type = BluetoothDeviceType.unknown,
  35.     this.isConnected = false,
  36.     this.bondState = BluetoothBondState.unknown,
  37.   });
  38.  
  39.   /// Creates `BluetoothDevice` from map.
  40.   ///
  41.   /// Internally used to receive the object from platform code.
  42.   factory BluetoothDevice.fromMap(Map map) {
  43.     return BluetoothDevice(
  44.       name: map["name"],
  45.       address: map["address"]!,
  46.       type: map["type"] != null
  47.           ? BluetoothDeviceType.fromUnderlyingValue(map["type"])
  48.           : BluetoothDeviceType.unknown,
  49.       isConnected: map["isConnected"] ?? false,
  50.       bondState: map["bondState"] != null
  51.           ? BluetoothBondState.fromUnderlyingValue(map["bondState"])
  52.           : BluetoothBondState.unknown,
  53.     );
  54.   }
  55.  
  56.   /// Creates map from `BluetoothDevice`.
  57.   Map<String, dynamic> toMap() => {
  58.         "name": this.name,
  59.         "address": this.address,
  60.         "type": this.type.toUnderlyingValue(),
  61.         "isConnected": this.isConnected,
  62.         "bondState": this.bondState.toUnderlyingValue(),
  63.       };
  64.  
  65.   /// Compares for equality of this and other `BluetoothDevice`.
  66.   ///
  67.   /// In fact, only `address` is compared, since this is most important
  68.   /// and unchangable information that identifies each device.
  69.   operator ==(Object other) {
  70.     return other is BluetoothDevice && other.address == this.address;
  71.   }
  72.  
  73.   @override
  74.   int get hashCode => address.hashCode;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement