# flutter_zsdk
Flutter Pluggin for Zebra Multiplatform SDK

## Getting Started

Add `flutter_zsdk` as a dependency in your `pubspec.yaml` file.

## Project Setup

### Android
note: tested using `JDK 17.0.2`, `Gradle 8.0`, `AGP 8.1.3`

Tambah / ganti `distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip` di android/gradle/wrapper/gradle-wrapper.properties.

Tambah / ganti `id "com.android.application" version "8.1.3" apply false` di android/settings.gradle.

Tambah ke file `gradle.properties`.
```
org.gradle.jvmargs=-Xmx4G
android.useAndroidX=true
android.enableJetifier=true
android.jetifier.ignorelist=jackson-core-2.17.2.jar
```

## Usage
Contoh lengkap bisa dilihat di `example` folder.  

Pertama buat objek dari plugin
```
import 'package:flutter_zsdk/flutter_zsdk.dart';

final _flutterZsdkPlugin = FlutterZsdk();

_flutterZsdk.findBluetoothPrinter();
// etc
```

### Discover Bluetooth Device
Return nilai dari `_flutterZsdkPlugin.findBluetoothPrinter()` itu `Stream<dynamic>` jadi perlu di handle untuk listen ke stream tsb.

Default event:
- `SOS` : Start of Stream
- `EOS` : End of Stream (`StreamSubscription` harus di cancel jika tidak diperlukan lagi)

contoh:
```
Future<void> discoverBluetoothDevices() async {
    try {
      Stream<dynamic> stream = await _flutterZsdkPlugin.findBluetoothPrinters();
      _bluetoothPrinterSubscription = stream.listen((event) {
        print(event);

        if (event == 'SOS') {
          _isDiscovering = true;

        }

        if (event is List) {
          // add to list logic here...

        }

        if (event == 'EOS') {
          _isDiscovering = false;
          showSnackBar('Bluetooth discovery finished, found ${_discoveredBluetoothPrinters.length} printers');
          _bluetoothPrinterSubscription?.cancel();
        }

        setState(() {});
      });
    
    } on PlatformException catch (e) {
      showSnackBar(e.message.toString());

    } on FlutterZsdkException catch (e) { 
      showSnackBar(e.message);

    } catch (e) {
      showSnackBar('Unexpected error while discovering bluetooth printers');

    }
  }
  ```


### Connect to Printer
Gunakan `_flutterZsdkPlugin.openConnection(<Mac Address>)`, dimana `<Mac Address>` bisa didapatkan dari `.findBluetoothPrinter()`

disarankan `.closeConnection()` sebelum melakukan connect ke printer lain. walaupun sudah dihandle disisi plugin.

### Send ZPL Data to Printer (Print ZPL)
sebelum print, check dulu koneksi ke printer, utk memastikan printer terkoneksi.

`_flutterZsdkPlugin.isConnected()` utk check koneksi ke printer.

jika sudah terkoneksi, gunakan `_flutterZsdkPlugin.printZplOverBluetooth(<zpl_data>)` untuk mengirim data ZPL ke printer.


### Disconnect from Printer
`_flutterZsdkPlugin.closeConnection()` utk disconnect dari printer.

</br>


note: 
- semua aksi harus dipanggil dalam try-catch block
- `_flutterZsdkPlugin.isConnected()` hanya harus dipanggil setelah `_flutterZsdkPlugin.openConnection()` jika tidak app bisa crash (tested ANDROID)