Git Product home page Git Product logo

phomemo's Introduction

phomemo

Pub Version analysis Star on Github License: BSD

A Flutter plugin to create information to send to phomemo printers.

Getting started

To get started with phomemo add the package to your pubspec.yaml file.

Generate a widget image

Generates an image to send to the printer to print. Set the size as the size of the label. The example below has a height of 12mm with a infinately long length.

GlobalKey key = GlobalKey();
Size labelSize = const Size(double.infinity,12);

Widget widget = Container(
  width: (labelSize.width * 3)+30,
  height: (labelSize.height * 3)+30,
  margin: const EdgeInsets.only(top:5),
  padding: const EdgeInsets.fromLTRB(15, 15, 15, 15),
  color: Theme.of(context).focusColor,
  child: RepaintBoundary(
    key: key,
    child: child,
  )
);

img.Image? image = await PhomemoHelper.generateImageFromWidget(key);

Generate a text image

Generates an image to send to the printer to print. Set the size as the size of the label. The example below has a height of 12mm with a infinately long length.

img.Image? text = await PhomemoHelper.generateImageFromText(
  TextSpan(
    text: 'text here',
    style: const TextStyle(
      fontSize: 34,
      color: Colors.black
    ),
  ),
  size: Size(double.inifity,12), //Change this to the lable size
);

Send info to the printer

Generate the data to send to the printer. Put the function to send to your printer in the class itself. In this case it is sending to a ble printer usng flutter_blue_plus.

Phomemo label = Phomemo(send: bluetooth.write, read: bluetooth.read);
PhomemoPrinter printer = helper.getPrinterFromName(bluetooth.device!.name);

await label.printLabel(
  [img.decodePng(qrCode!.buffer.asUint8List()),letter],// the images to send to the printer
  printer: printer, //The printer that will be printed on
  spacing: 5, //space between images. Only works for D30, and D35 printers
  rotate: printer != PhomemoPrinter.m220 // do not rotate the image if using the m220 or m110
  labelSize: Size(double.infinity,12), //size of the label
);

Example

Find the example app here.

Contributing

Contributions are welcome. In case of any problems look at existing issues, if you cannot find anything related to your problem then open an issue. Create an issue before opening a pull request for non trivial fixes. In case of trivial fixes open a pull request directly.

Additional Information

This plugin is only for creating the information to send to phomemo printers. This has been tested on P12Pro, D35, D30 and M220.

phomemo's People

Contributors

knightro63 avatar alexander-mayorga avatar

Stargazers

 avatar Joe Diragi avatar Alain Forteza avatar  avatar  avatar Vitaly avatar  avatar  avatar Megaache Younes avatar

Watchers

Lucian avatar  avatar

phomemo's Issues

M832

Good afternoon,

Can model M832 be enabled for this package and at what time?

Issue with Integrating Phomemo D30 Printer in Flutter App

Hi,

I'm reaching out about an issue we're encountering while integrating the Phomemo D30 label printer with our Flutter app. We've followed various methods using packages like phomemo and flutter_blu_plus to establish communication with the printer, but we're currently stuck at a point where we can connect successfully but are unable to print anything meaningful. Instead, we're receiving unexpected byte notifications (e.g., [1, 1]).

We've ensured all necessary permissions for Bluetooth and location services are granted within our Android app.

To summarize, our main concerns are:

  1. We need advice on the recommended way for printing to the Phomemo D30 printer.
  2. Any sample code snippets or examples demonstrating printer connection, sending print data, and handling necessary configurations would be invaluable.

Thank you for your attention to this matter.

Best regards,

photo_2024-05-02_13-54-28



  img.Image? letter = await helper.generateImage(
      const TextSpan(
        text: "test",
        style: TextStyle(fontSize: 24, color: Colors.black),
      ),
      size: size * 8,
    );

    await label.printLabel(
      [letter],
      printer: printer,
      labelSize: const Size(10, 10),
      spacing: 0,
      rotate: false,
    ).then((value) {
      printing = false;
      emit(LabelPrinterPrintingDone(allowPrinting: state.allowPrinting));
    });
  }

my blu file:


import 'dart:async';
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';

enum BluetoothOptions {
  SEARCHING,
  CONNECTING,
  CONNECTED,
  RECEIVED_SERVICES,
  DISCONNECTED,
  DATA_RECEIVED,
  STOP_SEARCHING
}

typedef BluetoothCallback = void Function(BluetoothOptions options);

class Bluetooth {
  Bluetooth({
    required this.onUpdate,
    required this.names,
    required this.deviceChange,
  }) {
    _startScan();
    FlutterBluePlus.adapterState.listen((state) {
      _updateAdapterState(state);
      _startScan();
    });
  }
  final List<String> names;
  late BluetoothCallback onUpdate;
  final Function(BluetoothDevice? device) deviceChange;
  BluetoothDevice? selectedDevice;
  List<BluetoothCharacteristic>? characteristics;
  BluetoothAdapterState adapterState = BluetoothAdapterState.on;
  bool scanning = false;
  bool get isScanning => scanning;

  void _updateAdapterState(BluetoothAdapterState state) {
    adapterState = state;
    if (adapterState == BluetoothAdapterState.on) {
      _startScan();
    }
  }

  void _startScan() async {
    if (!isScanning && adapterState == BluetoothAdapterState.on) {
      onUpdate(BluetoothOptions.SEARCHING);
      scanning = true;
      await FlutterBluePlus.startScan(timeout: const Duration(seconds: 15));

      StreamSubscription<List<ScanResult>>? scanSubscription;

      scanSubscription = FlutterBluePlus.scanResults.listen(
        (results) {
          if (results.isNotEmpty) {
            _processScanResult(results);
          }
        },
        onError: (e, stackTrace) {},
        onDone: () {
          // scanSubscription?.cancel();
          // scanning = false;
          // onUpdate(BluetoothOptions.STOP_SEARCHING);
        },
      );

      Future.delayed(const Duration(minutes: 2), () {
        if (scanning) {
          _stopScan("Timeout");
        }
      });
    }
  }

  void _stopScan(String reason) {
    scanning = false;
    // onUpdate(BluetoothOptions.STOP_SEARCHING);
    FlutterBluePlus.stopScan();
  }

  void _processScanResult(List<ScanResult> scanResult) {
    for (var result in scanResult) {
      if (names.isNotEmpty &&
          result.device.platformName.trim().isNotEmpty &&
          names.contains(result.device.platformName)) {
        _connect(result.device);
        _stopScan("Connecting");
        break;
      }
    }
  }

  void _connect(BluetoothDevice device) {
    onUpdate(BluetoothOptions.CONNECTING);
    selectedDevice = device;
    deviceChange(selectedDevice);

    if (kDebugMode) {
      print(selectedDevice!.platformName);
    }
    device.connect(mtu: null).then((_) {
      onUpdate(BluetoothOptions.CONNECTED);
      _discoverServices();
    });
  }

  void _handleNotificationData(List<int> data) {
    print('Received data =======> ${data.toString()}');
  }

  void _discoverServices() async {
    if (selectedDevice != null) {
      List<BluetoothService> services =
          await selectedDevice!.discoverServices();
      if (services.isNotEmpty) {
        for (var service in services) {
          characteristics = service.characteristics;
          for (var characteristic in service.characteristics) {
            if (characteristic.properties.notify) {
              characteristic.setNotifyValue(true);
              characteristic.lastValueStream.listen(_handleNotificationData,
                  onError: (error) {
                print('Received data =======> ${error.toString()}');
              });
              break;
            }
          }
        }
        onUpdate(BluetoothOptions.RECEIVED_SERVICES);
      }
    }
  }

  void _readCharacteristic() async {
    BluetoothCharacteristic? toSend;
    for (var char in characteristics!) {
      if (char.properties.notify) {
        toSend = char;
      }
    }
    List<int> value = await toSend!.read();
  }

  void readBluetoothData() {
    _readCharacteristic();
  }

  Future<List<int>> read() async {
    BluetoothCharacteristic? toSend;
    for (var char in characteristics!) {
      if (char.properties.notify) {
        toSend = char;
      }
    }
    return await toSend!.read();
  }

  Future<void> write(List<int> data) async {
    BluetoothCharacteristic? toSend;
    for (var char in characteristics!) {
      if (char.properties.write) {
        toSend = char;
      }
    }

    await toSend!.write("HHH".codeUnits,
        withoutResponse: true, allowLongWrite: false, timeout: 200000);
  }

  void destroy() {
    FlutterBluePlus.stopScan();
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.