blue_thermal_printer_plugin.dart 6.4 KB
Newer Older
houziyu's avatar
houziyu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
import 'package:blue_thermal_printer/blue_thermal_printer.dart';

class BlueThermalPrinter {
  static const int STATE_OFF = 10;
  static const int STATE_TURNING_ON = 11;
  static const int STATE_ON = 12;
  static const int STATE_TURNING_OFF = 13;
  static const int STATE_BLE_TURNING_ON = 14;
  static const int STATE_BLE_ON = 15;
  static const int STATE_BLE_TURNING_OFF = 16;
  static const int ERROR = -1;
  static const int CONNECTED = 1;
  static const int DISCONNECTED = 0;

  static const String namespace = 'blue_thermal_printer';

  static const MethodChannel _channel =
      const MethodChannel('$namespace/methods');

  static const EventChannel _readChannel =
      const EventChannel('$namespace/read');

  static const EventChannel _stateChannel =
      const EventChannel('$namespace/state');

  final StreamController<MethodCall> _methodStreamController =
      new StreamController.broadcast();

  static BlueThermalPrinter _instance = new BlueThermalPrinter._();

  static BlueThermalPrinter get instance => _instance;

  BlueThermalPrinter._() {
    _channel.setMethodCallHandler((MethodCall call) async {
      _methodStreamController.add(call);
    });
  }

  ///onStateChanged()
  Stream<int?> onStateChanged() =>
      _stateChannel.receiveBroadcastStream().map((buffer) => buffer);

  ///onRead()
  Stream<String> onRead() =>
      _readChannel.receiveBroadcastStream().map((buffer) => buffer.toString());

  Future<bool?> get isAvailable async =>
      await _channel.invokeMethod('isAvailable');

  Future<bool?> get isOn async => await _channel.invokeMethod('isOn');

  Future<bool?> get isConnected async =>
      await _channel.invokeMethod('isConnected');

  Future<bool?> get openSettings async =>
      await _channel.invokeMethod('openSettings');

  ///getBondedDevices()
  Future<List<BluetoothDevice>> getBondedDevices() async {
    final List list = await (_channel.invokeMethod('getBondedDevices'));
    return list.map((map) => BluetoothDevice.fromMap(map)).toList();
  }

  Future<String> getLastDevice() async {
    final String getLastDeviceName =
        await (_channel.invokeMethod('getLastDevice'));
    return getLastDeviceName;
  }

  ///printQRcode(String textToQR, int width, int height, int align)

  Future<List<int>> getQRcode(String textToQR, int width, int height, int align,
      {int? light = 0, int? weight = 0}) async {
    final List<int> QRcode = await (_channel.invokeMethod('getQRcode', {
      'textToQR': textToQR,
      'width': width,
      'height': height,
      'align': align,
      'light': light,
      'weight': weight
    }));
    return QRcode;
  }

  Future<List<int>> getBarCode(
      String textToBar, int width, int height, int align,
      {int? light = 0, int? weight = 0}) async {
    final List<int> barCode = await (_channel.invokeMethod('getBarCode', {
      'textToBar': textToBar,
      'width': width,
      'height': height,
      'align': align,
      'light': light,
      'weight': weight
    }));
    return barCode;
  }

  ///connect(BluetoothDevice device)
  Future<dynamic> connect(BluetoothDevice device) =>
      _channel.invokeMethod('connect', device.toMap());

  ///disconnect()
  Future<dynamic> disconnect() => _channel.invokeMethod('disconnect');

  ///write(String message)
  Future<dynamic> write(String message) =>
      _channel.invokeMethod('write', {'message': message});

  ///writeBytes(Uint8List message)
  Future<dynamic> writeBytes(Uint8List message) =>
      _channel.invokeMethod('writeBytes', {'message': message});

  Future<dynamic> printTSCImage(Map<String, dynamic> config, List<Map<String, dynamic>> data) {
    Map<String, Object> args = Map();
    args['config'] = config;
    args['data'] = data;
    print("---- $args");
    _channel.invokeMethod('printTSCImage', args);
    return Future.value(true);
  }


  ///iOS平台使用
  ///printSampleCode(String qrcode, String barcode,
  ///         String title, String subTitle, String smallSubTitle, String bottomContent)
  Future<dynamic> printSampleCode(String qrcode, String barcode, String title,
          String subTitle, String smallSubTitle, String bottomContent) =>
      _channel.invokeMethod('printSampleCode', {
        'qrcode': qrcode,
        'barcode': barcode,
        'title': title,
        'subTitle': subTitle,
        'smallSubTitle': smallSubTitle,
        'bottomContent': bottomContent,
      });

  ///repeatScan()
  Future<dynamic> repeatScan() => _channel.invokeMethod('repeatScan');

  ///printCustom(String message, int size, int align,{String? charset})
  Future<dynamic> printText(String message, int size, int align,
          {String? charset,
          String? format,
          int? light = 0,
          int? weight = 0,
          bool? bold = false}) =>
      _channel.invokeMethod('printText', {
        'message': message,
        'size': size,
        'bold': bold,
        'align': align,
        'charset': charset,
        'format': format,
        'light': light,
        'weight': weight
      });

  ///printNewLine()
  Future<dynamic> printNewLine() => _channel.invokeMethod('printNewLine');

  ///paperCut()
  Future<dynamic> paperCut() => _channel.invokeMethod('paperCut');

  ///initPrinter()
  Future<dynamic> initPrinter() => _channel.invokeMethod('initPrinter');

  ///initMarginLeft(int left)
  Future<dynamic> printMarginLeft(int left) =>
      _channel.invokeMethod('printMarginLeft', {'left': left});

  ///printImage(String pathImage)
  Future<dynamic> printImage(String pathImage) =>
      _channel.invokeMethod('printImage', {'pathImage': pathImage});

  ///printImageBytes(Uint8List bytes)
  Future<dynamic> printImageBytes(Uint8List bytes) =>
      _channel.invokeMethod('printImageBytes', {'bytes': bytes});

  ///printQRcode(String textToQR, int width, int height, int align)
  Future<dynamic> printQRcode(String textToQR, int width, int height, int align,
          {int? light = 0, int? weight = 0}) =>
      _channel.invokeMethod('printQRcode', {
        'textToQR': textToQR,
        'width': width,
        'height': height,
        'align': align,
        'light': light,
        'weight': weight
      });

  ///printQRcode(String textToQR, int width, int height, int align)
  Future<dynamic> printBarCode(
          String textToBar, int width, int height, int align,
          {int? light = 0, int? weight = 0}) =>
      _channel.invokeMethod('printBarCode', {
        'textToBar': textToBar,
        'width': width,
        'height': height,
        'align': align,
        'light': light,
        'weight': weight
      });
}