nn-printer
v0.2.4
Published
NN Printer
Downloads
24
Maintainers
Readme
NNPrinter Module
Overview
The NNPrinter
module provides functionalities for network service discovery and network printing via TCP connections. It supports discovering devices using mDNS (Bonjour) and printing text to network printers. This module is designed to be used with React Native applications.
Permissions
To use network service discovery and network printing, your application needs to request appropriate permissions. This section covers the necessary permissions for both Android and iOS.
Android Permissions
Add the following permissions to your app.json
file under the android
section:
{
"expo": {
...
"android": {
"permissions": [
"INTERNET",
"ACCESS_NETWORK_STATE",
"CHANGE_NETWORK_STATE",
"ACCESS_WIFI_STATE",
"CHANGE_WIFI_STATE",
"NEARBY_WIFI_DEVICES",
"BLUETOOTH",
"BLUETOOTH_ADMIN",
"BLUETOOTH_SCAN",
"BLUETOOTH_ADVERTISE",
"BLUETOOTH_CONNECT",
"ACCESS_FINE_LOCATION",
"ACCESS_COARSE_LOCATION"
],
"package": "com.yourcompany.yourapp"
},
"ios": {
...
"infoPlist": {
"NSBluetoothAlwaysUsageDescription": "Allow SimpleVen to use bluetooth",
"NSBonjourServices": [
"_http._tcp",
"_ipp._tcp"
],
"NSLocalNetworkUsageDescription": "This app requires access to the local network to discover and communicate with local devices."
}
},
}
}
## Usage
### Import and Initialize the Module
```javascript
import NNPrinter from 'nn-printer';
Example Usage
Start Scanning for Devices
NNPrinter.onScanStart().then((status) => {
console.log('Scan started');
//status : {"status": "started"}
}).catch(error => {
console.error('Scan start failed', error);
});
Stop Scanning for Devices
NNPrinter.onScanStop().then((status) => {
console.log('Scan stopped');
//{"status": "stopped"}
}).catch(error => {
console.error('Scan stop failed', error);
});
Listen for Detected Devices
const subscription = NNPrinter.onScanDetected((result) => {
console.log('Device found:', result.ipAddress);
//{"ipAddress": "192.168.1.2", "serviceName": "EPSON TM-m30III", "serviceType": "._http._tcp", "status": "found"}
});
// Remember to unsubscribe when no longer needed
subscription.remove();
Print Text to a Network Printer
const const printText =
"[C]<img>https://drive.usercontent.google.com/u/0/uc?id=1GQOzB8MP3iWXV3k53pFumA5H5s0cpjha&export=download</img>\n"
+
NNPrinter.prepareColumn(
[24],
['C'],
['SIMPLEVEN TEST'],
{ fontWeight: "bold", fontSize: "big" }
)
+
NNPrinter.prepareColumn(
[48],
['C'],
['07/07/2024 18:30']
) +
NNPrinter.prepareColumn(
[48],
['C'],
['Accepted by']
) +
NNPrinter.prepareColumn(
[24],
['C'],
['Bar'],
{ fontWeight: "bold", fontSize: "big" }
) +
NNPrinter.prepareColumn(
[48],
['C'],
['07/07/2024 18:30']
)
+
NNPrinter.prepareColumn(
[18, 10, 10, 10],
['L', 'R', 'R', 'R'],
['Item', 'Size', 'Qty', 'Price'],
{ fontWeight: "bold" }
) +
NNPrinter.prepareColumn(
[18, 10, 10, 10],
['L', 'R', 'R', 'R'],
['Albabi Odense Pilsner 4.6%', 'Lille', '23', '1058 DKK'],
) +
NNPrinter.prepareColumn(
[18, 10, 10, 10],
['L', 'R', 'R', 'R'],
['Pepsi', 'Mellem', '2', '88 DKK'],
) +
NNPrinter.prepareColumn(
[18, 10, 10, 10],
['L', 'R', 'R', 'R'],
['Premium Party Starter', '', '1', '799 DKK'],
) +
/** */
NNPrinter.prepareColumn(
[48],
['L'],
[' 20x Smirnoff Ice'],
) +
NNPrinter.prepareColumn(
[48],
['L'],
[' 1x Breezer Original'],
) +
NNPrinter.prepareColumn(
[48],
['L'],
[' 2x Breezer Pinapple'],
) +
NNPrinter.prepareColumn(
[48],
['L'],
['All prices include VAT where applicable'],
) +
NNPrinter.prepareColumn(
[38, 10],
['L', 'R'],
['Total', '7147 DKK'],
) +
NNPrinter.prepareColumn(
[48],
['C'],
['!!!Disclaimer!!!'],
{ fontWeight: "bold" }
) +
NNPrinter.prepareColumn(
[48],
['C'],
['This is a staff-only receipt for internal use only. Please provide official receipts to customers if needed.'],
)
NNPrinter.print(
'192.168.1.100', // IP address of the printer
9100, // Port
text, // Text to print
true, // Auto cut
'medium' // Printer type (as defined in PrinterConfig)
).then(() => {
console.log('Print successful');
}).catch(error => {
console.error('Print failed', error);
});
Conclusion
The NNPrinter
module provides easy-to-use functionalities for network service discovery and network printing. Ensure you have the required permissions set up correctly for the module to function as expected on both Android and iOS.