connect method

Future<bool> connect(
  1. Printer device
)

Implementation

Future<bool> connect(Printer device) async {
  if (device.connectionType == ConnectionType.USB) {
    return await FlutterThermalPrinterPlatform.instance.connect(device);
  } else {
    try {
      bool isConnected = false;
      final bt = BluetoothDevice.fromId(device.address!);
      await bt.connect();
      final stream = bt.connectionState.listen((event) {
        if (event == BluetoothConnectionState.connected) {
          isConnected = true;
        }
      });
      await Future.delayed(const Duration(seconds: 3));
      await stream.cancel();
      return isConnected;
    } catch (e) {
      return false;
    }
  }
}