PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 18, 2022

[FIXED] How to save found bluetooth printer as default in Flutter?

 September 18, 2022     bluetooth, flutter, printing     No comments   

Issue

I'm currently developing app in Flutter using these lib:

esc_pos_bluetooth: ^0.2.8
flutter_bluetooth_basic: ^0.1.5

I am planning to save found printer as text in SharedPreference so the user don't need select the printer each time they want to print (saving it as default printer), I'm thinking about saving it's MAC address

ListView.builder(
    itemBuilder: (context, position) => ListTile(
        onTap: () {
            _startPrint(_devices[position]);
        },
        leading: Icon(Icons.print),
        title: Text(_devices[position].name),
        subtitle: Text(_devices[position].address),
    ),
    itemCount: _devices.length,
)

every device MAC address can be acquired from _devices.address.

Now come in to the question, I can save it as String in SharedPreferences, but how to call it as a device?

Future<void> _startPrint(PrinterBluetooth printer) async {
    _printerManager.selectPrinter(printer);
    final myTicket = await _ticket(PaperSize.mm58);
    final result = await _printerManager.printTicket(myTicket);
    print(result);
}

I haven't found any references to call device via it's MAC address. From what I found, the device need to be called using PrinterBluetooth instance as parameter of .selectPrinter() method.

Sorry for my English, thanks in advance.


Solution

Well, there is no method here that helps us to select a printer using mac address.

There are two ways you can go for.

1) Easy One and a good one that checks the availability: Store the mac address, Scan for available printers and get the instance of printer whose mac address matches.

     printerManager.scanResults.listen((devices) async {
      
      devices.forEach((printer) => 
        print(printer);
        //get saved printer
        if(printer.address == "your stored string"){
        //store the element.
        }

        );
      });

2. Complicated one which needs a lot of thought, store the complete BluetoothPrinter as a JSON string in the shared preference and use flutter_bluetooth_basic package's fromJson method to pass into PrinterBluetooth, You can explore further. Note this way won't ensure that your printer is available or not.

So if you don't find your stored device in Approach One you can show in the UI that the saved printer is not available.



Answered By - Krish Bhanushali
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing