@xbone-3/cordova-zebra-printer
v0.1.1
Published
Zebra Printer Cordova Plugin for iOS and Android, with Bluetooth and Android WiFi/network discovery and printing
Maintainers
Readme
cordova-zebra-printer
A Cordova plugin for Zebra CPCL printers for both iOS and Android with Ionic 3 bindings. This plugin only supports Zebra models that use CPCL printing. Feel free to contribute to this project if you need to support other methods of printing. It has only been tested with Zebra QLn320 printers. Let me know if you use if sucessfully with others.
Also this now requires a minimum of Cordova 9 and cordova-ios 5.0. Current version of link_os_sdk is 1.5.1049
This is a fork of cleversolutions/cordova-zebra-printer that adds Android-only WiFi/network printer discovery and printing alongside the original Bluetooth support. iOS behavior is unchanged.
Get from npm
cordova plugin add @xbone-3/cordova-zebra-printeror get the latest version from git
cordova plugin add https://github.com/cleversolutions/cordova-zebra-printer.gitTo use with Ionic 3
Add the Ionic 3 bindings to your app.module.ts file
import { ZebraPrinter } from '@xbone-3/cordova-zebra-printer/native';
...
providers: [
ZebraPrinter,
...Then inject the ZebraPrinter into the constructor of the class where you wish to use it for example
constructor(public navCtrl: NavController, protected zebraPrinter:ZebraPrinter) {
console.log(">>>>>> Home Constructed <<<<<<<")
}
protected discover(){
console.log("Now Discover");
this.zebraPrinter.discover().then(result => {
console.log(result);
}).catch(err => {
console.error(err);
});
}Discovery: Bluetooth + Android WiFi/network
discover() returns already-bonded Bluetooth devices (iOS and Android) merged with, on Android only, Zebra printers found via network discovery (UDP subnet broadcast). Each entry is tagged with a type field:
interface Printer {
name: string;
address: string; // MAC address for bluetooth, IP address for network
type: 'bluetooth' | 'network';
port?: number; // only present for type === 'network'; the printer's discovered CPCL TCP port (default 6101)
}Network discovery is Android-only — on iOS, discover() continues to return only bonded Bluetooth devices with no type/port fields.
Connecting: connect(address, type?)
connect() now accepts an optional second argument, the printer type. It defaults to 'bluetooth' so existing single-argument callers keep working unchanged.
// Bluetooth (unchanged, type defaults to 'bluetooth')
this.zebraPrinter.connect(macAddress);
// Network (Android only)
this.zebraPrinter.connect(ipAddress, 'network');When type === 'network', the plugin connects over TCP using the SDK's default CPCL port (6101). Bluetooth connections are unaffected.
Realtime scanning: scan() / stopScan() (Android only)
discover() waits for the full discovery batch (including the network broadcast timeout) before returning anything. scan() instead streams each printer to you the moment it's found:
this.scanSubscription = this.zebraPrinter.scan().subscribe(
(printer: Printer) => console.log('found', printer),
(err) => console.error(err)
);
// later
this.scanSubscription.unsubscribe(); // stops the scan (calls stopScan() for you)scan()/stopScan() are Android only — the native iOS plugin does not implement them, so the JS bridge rejects the call immediately with an error on iOS instead of hanging.
On Android, scan() streams, in order:
- Already-bonded Bluetooth devices — emitted immediately.
- Nearby unpaired Bluetooth devices — found live via
BluetoothAdapter.startDiscovery(). - Network printers — emitted live as they respond to discovery, instead of waiting for the whole batch like
discover()does.
Call stopScan() directly (or just unsubscribe from the Observable) to cancel an in-progress scan.
Live Bluetooth scanning needs a runtime permission grant on top of the manifest permissions below — the plugin requests it automatically the first time scan() runs, prompting the user with ACCESS_FINE_LOCATION (Android up to 11) or BLUETOOTH_SCAN/BLUETOOTH_CONNECT (Android 12+). If the user denies it, scan()'s error callback fires and no bluetooth/network scanning happens.
Android permissions
In addition to the existing BLUETOOTH / BLUETOOTH_ADMIN permissions, this plugin now also declares on Android:
android.permission.ACCESS_WIFI_STATEandroid.permission.ACCESS_NETWORK_STATEandroid.permission.CHANGE_WIFI_MULTICAST_STATE(needed for reliable UDP-broadcast discovery on some OEMs)android.permission.ACCESS_FINE_LOCATION(maxSdkVersion="30") — required by Android up to API 30 to receive results from live Bluetooth discovery, requested at runtime byscan()android.permission.BLUETOOTH_SCAN(neverForLocation) andandroid.permission.BLUETOOTH_CONNECT— the Android 12+ replacements for the above, also requested at runtime byscan()
