bluetooth_print icon indicating copy to clipboard operation
bluetooth_print copied to clipboard

Can not connect a Inner blutooth Printer

Open thanaratken opened this issue 2 years ago • 1 comments

My Code

import 'package:bluetooth_print/bluetooth_print.dart';
import 'package:bluetooth_print/bluetooth_print_model.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class PrintPage extends StatefulWidget {
  PrintPage({Key? key}) : super(key: key);
  @override
  State<PrintPage> createState() => _PrintPageState();
}

class _PrintPageState extends State<PrintPage> {
  BluetoothPrint bluetoothPrint = BluetoothPrint.instance;
  List<BluetoothDevice> _devices = [];
  String _deviceMsg = '';
  bool _connected = false;
  final f = NumberFormat("\###,###.00", "en_US");

  final List<Map<String, dynamic>> data = [
    {'title' : 'test1','price': 15,'qty':2},
    {'title' : 'test2','price': 35,'qty':3},
    {'title' : 'test3','price': 25,'qty':8},
    {'title' : 'test4','price': 6,'qty':5},
    {'title' : 'test5','price': 35,'qty':1},
  ];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) => { initPrinter()});
    
  }
  Future<void> initPrinter() async {
    bluetoothPrint.startScan(timeout: Duration(seconds: 2));
    if(!mounted) return;
    bluetoothPrint.scanResults.listen((val) {
      if(!mounted) return;
      setState((){
        _devices = val;
      });
      if(_devices.isEmpty) setState(() {
        _deviceMsg = "No Devices";
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Select Printer'),
        backgroundColor: Colors.blue,
      ),
      body: _devices.isEmpty? Center(child: Text(_deviceMsg != '' ? _deviceMsg : ''),) : 
      ListView.builder(
        itemCount: _devices.length,
        itemBuilder: (c, i){
          return ListTile(
            leading: Icon(Icons.print),
            title: Text(_devices[i].name.toString()),
            subtitle: Text(_devices[i].address.toString()),
            onTap: () {_startPrint(_devices[i]);},
          );
        },
      )
    );
  }

  Future<void> _startPrint(BluetoothDevice device) async{
    // print(device);
    if(device != null && device.address != null) {

      await bluetoothPrint.connect(device);
  

      Map<String, dynamic> config = Map();
      List<LineText> list = [];
         list.add(LineText(type: LineText.TYPE_TEXT, content: 'A Title', weight: 1, align: LineText.ALIGN_CENTER,linefeed: 1));
        list.add(LineText(type: LineText.TYPE_TEXT, content: 'this is conent left', weight: 0, align: LineText.ALIGN_LEFT,linefeed: 1));
        list.add(LineText(type: LineText.TYPE_TEXT, content: 'this is conent right', align: LineText.ALIGN_RIGHT,linefeed: 1));
        list.add(LineText(linefeed: 1));
        list.add(LineText(type: LineText.TYPE_BARCODE, content: 'A12312112', size:10, align: LineText.ALIGN_CENTER, linefeed: 1));
        list.add(LineText(linefeed: 1));
        await bluetoothPrint.printReceipt(config, list);
      }
        

  }
}

Message error E/flutter (25761): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(not connect, state not right, null, null)

thanaratken avatar Jun 20 '22 11:06 thanaratken

did you solve this? or it worked properly?

GithubPranshul avatar Sep 03 '22 09:09 GithubPranshul