@rejaul/react-native-printer-kit 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,105 @@
1
1
  import { TurboModuleRegistry, type TurboModule, Platform } from 'react-native';
2
2
 
3
+ /** How sure the library is that a paired Bluetooth device is a printer. */
4
+ export type PrinterConfidence = 'CONFIDENT' | 'LIKELY' | 'UNKNOWN';
5
+
6
+ /** `ALL` lists every paired device, `LIKELY_PRINTERS` hides non-printers, `STRICT` keeps only certain ones. */
7
+ export type PrinterFilter = 'ALL' | 'LIKELY_PRINTERS' | 'STRICT';
8
+
9
+ export type PrinterConnectionType = 'BLUETOOTH' | 'NETWORK' | 'USB';
10
+
3
11
  export type BluetoothPrinterDevice = {
12
+ /** `null` for a Wi-Fi/LAN printer. */
4
13
  name: string | null;
14
+ /** Bluetooth MAC, the host for a Wi-Fi/LAN printer, or `"vendorId:productId"` for a USB printer. */
5
15
  address: string;
16
+ confidence: PrinterConfidence;
17
+ /** Why it got that confidence, e.g. "Printer-like name + SPP service". May be `null`. */
18
+ reason: string | null;
19
+ };
20
+
21
+ export type GetBondedPrintersParams = {
22
+ filter?: PrinterFilter;
23
+ /** Extra name fragments to treat as printers, for printers with unusual names. */
24
+ extraNameKeywords?: string[];
25
+ };
26
+
27
+ export type UsbPrinterDevice = {
28
+ name: string | null;
29
+ vendorId: number;
30
+ productId: number;
31
+ deviceName: string;
32
+ };
33
+
34
+ export type UsbDeviceParams = {
35
+ vendorId: number;
36
+ productId: number;
6
37
  };
7
38
 
8
39
  export type PrinterImageType = 'PNG' | 'JPEG';
9
40
 
10
41
  export type ConnectPrinterParams = {
42
+ /** Bluetooth MAC address, or the IP/host for `NETWORK`. Ignored (use `""`) for `USB`. */
11
43
  address: string;
44
+ /** Default `BLUETOOTH`. */
45
+ type?: PrinterConnectionType;
46
+ /** `NETWORK` only. Default 9100. */
47
+ port?: number;
48
+ /** `NETWORK` connect timeout in ms. Default 5000. */
49
+ timeoutMs?: number;
50
+ /** `USB` only, from `getUsbPrinters()`. */
51
+ vendorId?: number;
52
+ /** `USB` only, from `getUsbPrinters()`. */
53
+ productId?: number;
54
+ /** Title of the ongoing "connected" notification. Default "Printer connected". */
55
+ notificationTitle?: string;
56
+ /** Title of the notification shown if the printer drops later. Default "Printer disconnected". */
57
+ disconnectedNotificationTitle?: string;
58
+ };
59
+
60
+ export type PrinterStatusParams = {
61
+ /** How long to wait for the printer to answer, in ms. Default 800. */
62
+ timeoutMs?: number;
63
+ };
64
+
65
+ /**
66
+ * `null` means "unknown" - the printer did not answer or does not report that condition. It is NOT "ok".
67
+ */
68
+ export type PrinterStatus = {
69
+ connected: boolean;
70
+ /** `false` means the printer ignored the status request and every field below is `null`. */
71
+ responded: boolean;
72
+ online: boolean | null;
73
+ paperOut: boolean | null;
74
+ paperNearEnd: boolean | null;
75
+ coverOpen: boolean | null;
76
+ hasError: boolean | null;
77
+ };
78
+
79
+ export type FileLocation =
80
+ 'CACHE' | 'APP_FILES' | 'DOWNLOADS' | 'DOCUMENTS' | 'CUSTOM_FOLDER';
81
+
82
+ export type SaveFileParams = {
83
+ sourcePath: string;
84
+ /** Defaults to the source file's name. */
85
+ fileName?: string;
86
+ /** Default `DOWNLOADS`. */
87
+ location?: FileLocation;
88
+ /** Required for `CUSTOM_FOLDER`: a `content://` tree Uri string or a directory path. */
89
+ customFolder?: string;
90
+ subFolder?: string;
91
+ /** Guessed from the file extension when not given. */
92
+ mimeType?: string;
93
+ overwrite?: boolean;
94
+ deleteSource?: boolean;
95
+ };
96
+
97
+ export type SaveFileResult = {
98
+ success: boolean;
99
+ path: string | null;
100
+ uri: string | null;
101
+ fileName: string | null;
102
+ error: string | null;
12
103
  };
13
104
 
14
105
  export type PrintTextParams = {
@@ -73,6 +164,12 @@ export type PrintHtmlParams = {
73
164
  bandDelayMs?: number;
74
165
  };
75
166
 
167
+ export type OpenFileParams = {
168
+ path: string;
169
+ /** Inferred from the file extension if not given. */
170
+ mimeType?: string;
171
+ };
172
+
76
173
  export interface Spec extends TurboModule {
77
174
  /**
78
175
  * Whether the device's Bluetooth adapter itself is turned on. Needs no runtime
@@ -94,16 +191,35 @@ export interface Spec extends TurboModule {
94
191
  requestBluetoothPermission(): Promise<boolean>;
95
192
 
96
193
  /**
97
- * Returns all Bluetooth devices already paired (bonded) with this device.
194
+ * Returns the Bluetooth devices already paired (bonded) with this device, best printer matches first.
98
195
  * On Android 12+ this requires the BLUETOOTH_CONNECT permission to be granted.
196
+ * Pass `{}` for every paired device, or `{ filter: 'LIKELY_PRINTERS' }` to hide non-printers.
99
197
  */
100
- getBondedBluetoothPrinters(): BluetoothPrinterDevice[];
198
+ getBondedBluetoothPrinters(
199
+ params: GetBondedPrintersParams
200
+ ): BluetoothPrinterDevice[];
101
201
 
102
202
  /**
103
- * Opens a Bluetooth SPP connection to the paired printer at the given address.
104
- * Closes any existing printer connection first. Remembers the address so the
203
+ * USB printers currently plugged in (USB printer class). Needs a phone with USB host (OTG).
204
+ */
205
+ getUsbPrinters(): UsbPrinterDevice[];
206
+
207
+ /**
208
+ * Whether the user already granted this app access to that USB printer.
209
+ */
210
+ hasUsbPermission(params: UsbDeviceParams): boolean;
211
+
212
+ /**
213
+ * Shows the system USB permission dialog and resolves with the user's answer.
214
+ * Call this before `connectPrinter` with `type: 'USB'`. The grant is lost when the app is reinstalled.
215
+ */
216
+ requestUsbPermission(params: UsbDeviceParams): Promise<boolean>;
217
+
218
+ /**
219
+ * Connects over Bluetooth (default), Wi-Fi/LAN or USB depending on `params.type`.
220
+ * Closes any existing printer connection first. Remembers the printer so the
105
221
  * app reconnects automatically next time it's opened (see the README's
106
- * "Persistent connection" section).
222
+ * "Persistent connection" section). Resolves `false` (does not reject) if it could not connect.
107
223
  */
108
224
  connectPrinter(params: ConnectPrinterParams): Promise<boolean>;
109
225
 
@@ -122,6 +238,29 @@ export interface Spec extends TurboModule {
122
238
  */
123
239
  getConnectedPrinter(): Promise<BluetoothPrinterDevice | null>;
124
240
 
241
+ /**
242
+ * Whether a printer is connected AND currently accepts data (it may be switched off or out of range).
243
+ * Does not check paper or the cover - use `getPrinterStatus` for that.
244
+ */
245
+ isPrinterReady(): Promise<boolean>;
246
+
247
+ /**
248
+ * Asks the printer for its state (online, paper, cover, error). Many cheap printers do not answer:
249
+ * then `responded` is `false` and every other field is `null` (unknown, NOT ok).
250
+ */
251
+ getPrinterStatus(params: PrinterStatusParams): Promise<PrinterStatus>;
252
+
253
+ /**
254
+ * Prints a short ASCII test page (connection type, printer, address, time).
255
+ */
256
+ printTestPage(): Promise<void>;
257
+
258
+ /**
259
+ * Copies a finished file (e.g. the PDF from `htmlToPdf`) to cache, app storage, public
260
+ * Downloads/Documents or a chosen folder. Never rejects: check `result.success`.
261
+ */
262
+ saveFile(params: SaveFileParams): Promise<SaveFileResult>;
263
+
125
264
  /**
126
265
  * Print raw text using the printer's built-in font. ASCII only - not
127
266
  * suitable for Bangla or other non-Latin scripts, use printHtml() instead.
@@ -169,6 +308,14 @@ export interface Spec extends TurboModule {
169
308
  * content with Bangla or other non-Latin text, or any real HTML/CSS layout.
170
309
  */
171
310
  printHtml(params: PrintHtmlParams): Promise<boolean>;
311
+
312
+ /**
313
+ * Opens a file with whatever app the user has installed that can handle its
314
+ * type (e.g. a PDF viewer) - shows the system's "Open with" chooser if more
315
+ * than one app matches. `mimeType` is inferred from the file extension if
316
+ * not given. Returns false if the file doesn't exist or no app can open it.
317
+ */
318
+ openFile(params: OpenFileParams): boolean;
172
319
  }
173
320
 
174
321
  function getModule(): Spec {
package/src/enums.ts ADDED
@@ -0,0 +1,56 @@
1
+ import type {
2
+ FileLocation as FileLocationValue,
3
+ PrinterConfidence as PrinterConfidenceValue,
4
+ PrinterConnectionType as PrinterConnectionTypeValue,
5
+ PrinterFilter as PrinterFilterValue,
6
+ PrinterImageType as PrinterImageTypeValue,
7
+ } from './NativeReactNativePrinterKit';
8
+
9
+ // Runtime values for the string unions the native module uses, so callers can write
10
+ // `PrinterFilter.LIKELY_PRINTERS` instead of a bare string. Each object is checked
11
+ // against its union with `satisfies`, so a typo or a value the native side does not
12
+ // know fails to compile. The plain strings keep working everywhere too.
13
+
14
+ /** How sure the library is that a paired Bluetooth device is a printer. */
15
+ export const PrinterConfidence = {
16
+ CONFIDENT: 'CONFIDENT',
17
+ LIKELY: 'LIKELY',
18
+ UNKNOWN: 'UNKNOWN',
19
+ } as const satisfies Record<PrinterConfidenceValue, PrinterConfidenceValue>;
20
+ export type PrinterConfidence =
21
+ (typeof PrinterConfidence)[keyof typeof PrinterConfidence];
22
+
23
+ /** `ALL` lists every paired device, `LIKELY_PRINTERS` hides non-printers, `STRICT` keeps only certain ones. */
24
+ export const PrinterFilter = {
25
+ ALL: 'ALL',
26
+ LIKELY_PRINTERS: 'LIKELY_PRINTERS',
27
+ STRICT: 'STRICT',
28
+ } as const satisfies Record<PrinterFilterValue, PrinterFilterValue>;
29
+ export type PrinterFilter = (typeof PrinterFilter)[keyof typeof PrinterFilter];
30
+
31
+ export const PrinterConnectionType = {
32
+ BLUETOOTH: 'BLUETOOTH',
33
+ NETWORK: 'NETWORK',
34
+ USB: 'USB',
35
+ } as const satisfies Record<
36
+ PrinterConnectionTypeValue,
37
+ PrinterConnectionTypeValue
38
+ >;
39
+ export type PrinterConnectionType =
40
+ (typeof PrinterConnectionType)[keyof typeof PrinterConnectionType];
41
+
42
+ export const PrinterImageType = {
43
+ PNG: 'PNG',
44
+ JPEG: 'JPEG',
45
+ } as const satisfies Record<PrinterImageTypeValue, PrinterImageTypeValue>;
46
+ export type PrinterImageType =
47
+ (typeof PrinterImageType)[keyof typeof PrinterImageType];
48
+
49
+ export const FileLocation = {
50
+ CACHE: 'CACHE',
51
+ APP_FILES: 'APP_FILES',
52
+ DOWNLOADS: 'DOWNLOADS',
53
+ DOCUMENTS: 'DOCUMENTS',
54
+ CUSTOM_FOLDER: 'CUSTOM_FOLDER',
55
+ } as const satisfies Record<FileLocationValue, FileLocationValue>;
56
+ export type FileLocation = (typeof FileLocation)[keyof typeof FileLocation];
package/src/index.tsx CHANGED
@@ -1,14 +1,21 @@
1
1
  import ReactNativePrinterKit, {
2
2
  type BluetoothPrinterDevice,
3
3
  type ConnectPrinterParams,
4
+ type GetBondedPrintersParams,
4
5
  type HtmlToPdfParams,
6
+ type OpenFileParams,
5
7
  type PdfToImageParams,
6
- type PrinterImageType,
8
+ type PrinterStatus,
9
+ type PrinterStatusParams,
7
10
  type PrintHtmlParams,
8
11
  type PrintImageBase64Params,
9
12
  type PrintImageParams,
10
13
  type PrintPdfParams,
11
14
  type PrintTextParams,
15
+ type SaveFileParams,
16
+ type SaveFileResult,
17
+ type UsbDeviceParams,
18
+ type UsbPrinterDevice,
12
19
  } from './NativeReactNativePrinterKit';
13
20
 
14
21
  /**
@@ -34,16 +41,47 @@ export function requestBluetoothPermission(): Promise<boolean> {
34
41
  }
35
42
 
36
43
  /**
37
- * Get all Bluetooth devices already paired (bonded) with this device.
44
+ * Get the Bluetooth devices already paired (bonded) with this device, best printer
45
+ * matches first. Every device carries a `confidence`. Pass
46
+ * `{ filter: 'LIKELY_PRINTERS' }` to hide devices that do not look like printers
47
+ * (headphones, watches...), or `extraNameKeywords` for a printer with an unusual name.
38
48
  */
39
- export function getBondedBluetoothPrinters(): BluetoothPrinterDevice[] {
40
- return ReactNativePrinterKit.getBondedBluetoothPrinters();
49
+ export function getBondedBluetoothPrinters(
50
+ params: GetBondedPrintersParams = {}
51
+ ): BluetoothPrinterDevice[] {
52
+ return ReactNativePrinterKit.getBondedBluetoothPrinters(params);
41
53
  }
42
54
 
43
55
  /**
44
- * Connect to a paired Bluetooth printer by its MAC address. The connection
45
- * survives the app being swiped from Recents, and is reconnected to
46
- * automatically the next time the app starts.
56
+ * USB printers currently plugged in. Needs a phone with USB host (OTG).
57
+ */
58
+ export function getUsbPrinters(): UsbPrinterDevice[] {
59
+ return ReactNativePrinterKit.getUsbPrinters();
60
+ }
61
+
62
+ /**
63
+ * Whether the user already granted this app access to that USB printer.
64
+ */
65
+ export function hasUsbPermission(params: UsbDeviceParams): boolean {
66
+ return ReactNativePrinterKit.hasUsbPermission(params);
67
+ }
68
+
69
+ /**
70
+ * Shows the system USB permission dialog. Call before `connectPrinter` with
71
+ * `type: 'USB'`. The grant is lost when the app is reinstalled.
72
+ */
73
+ export function requestUsbPermission(
74
+ params: UsbDeviceParams
75
+ ): Promise<boolean> {
76
+ return ReactNativePrinterKit.requestUsbPermission(params);
77
+ }
78
+
79
+ /**
80
+ * Connect to a printer over Bluetooth (default; paired printer's MAC address),
81
+ * Wi-Fi/LAN (`type: 'NETWORK'`, `address` = IP) or USB (`type: 'USB'` with
82
+ * `vendorId`/`productId`). The connection survives the app being swiped from
83
+ * Recents, and is reconnected to automatically the next time the app starts.
84
+ * Resolves `false` if it could not connect.
47
85
  */
48
86
  export function connectPrinter(params: ConnectPrinterParams): Promise<boolean> {
49
87
  return ReactNativePrinterKit.connectPrinter(params);
@@ -70,6 +108,41 @@ export function getConnectedPrinter(): Promise<BluetoothPrinterDevice | null> {
70
108
  return ReactNativePrinterKit.getConnectedPrinter();
71
109
  }
72
110
 
111
+ /**
112
+ * Whether a printer is connected and currently accepts data. Does not check
113
+ * paper or cover - use `getPrinterStatus()`.
114
+ */
115
+ export function isPrinterReady(): Promise<boolean> {
116
+ return ReactNativePrinterKit.isPrinterReady();
117
+ }
118
+
119
+ /**
120
+ * Asks the printer for its state (online / paper out / cover open / error).
121
+ * Many printers do not answer: then `responded` is `false` and the other fields
122
+ * are `null` (unknown, not "ok"). Some printers report an open cover as
123
+ * `paperOut`, so treat `paperOut: true` as "no paper or cover open".
124
+ */
125
+ export function getPrinterStatus(
126
+ params: PrinterStatusParams = {}
127
+ ): Promise<PrinterStatus> {
128
+ return ReactNativePrinterKit.getPrinterStatus(params);
129
+ }
130
+
131
+ /**
132
+ * Prints a short ASCII test page.
133
+ */
134
+ export function printTestPage(): Promise<void> {
135
+ return ReactNativePrinterKit.printTestPage();
136
+ }
137
+
138
+ /**
139
+ * Copies a file (e.g. the PDF from `htmlToPdf`) to cache, app storage, public
140
+ * Downloads/Documents or a chosen folder. Never rejects: check `result.success`.
141
+ */
142
+ export function saveFile(params: SaveFileParams): Promise<SaveFileResult> {
143
+ return ReactNativePrinterKit.saveFile(params);
144
+ }
145
+
73
146
  /**
74
147
  * Print raw text using the printer's built-in font (ASCII only - use
75
148
  * printHtml() for Bangla or other non-Latin scripts).
@@ -89,7 +162,9 @@ export function printImage(params: PrintImageParams): Promise<void> {
89
162
  /**
90
163
  * Print a base64-encoded image as an ESC/POS raster image.
91
164
  */
92
- export function printImageBase64(params: PrintImageBase64Params): Promise<void> {
165
+ export function printImageBase64(
166
+ params: PrintImageBase64Params
167
+ ): Promise<void> {
93
168
  return ReactNativePrinterKit.printImageBase64(params);
94
169
  }
95
170
 
@@ -123,18 +198,44 @@ export function printHtml(params: PrintHtmlParams): Promise<boolean> {
123
198
  return ReactNativePrinterKit.printHtml(params);
124
199
  }
125
200
 
201
+ /**
202
+ * Opens a file with whatever app the user has installed that can handle its
203
+ * type (e.g. a PDF viewer) - shows the system's "Open with" chooser if more
204
+ * than one app matches. Returns false if the file doesn't exist or no app
205
+ * can open it.
206
+ */
207
+ export function openFile(params: OpenFileParams): boolean {
208
+ return ReactNativePrinterKit.openFile(params);
209
+ }
210
+
211
+ // Export enum-like constants (usable at runtime, and as types of the same name)
212
+ export {
213
+ FileLocation,
214
+ PrinterConfidence,
215
+ PrinterConnectionType,
216
+ PrinterFilter,
217
+ PrinterImageType,
218
+ } from './enums';
219
+
126
220
  // Export types
127
221
  export type {
128
222
  BluetoothPrinterDevice,
129
223
  ConnectPrinterParams,
224
+ GetBondedPrintersParams,
130
225
  HtmlToPdfParams,
226
+ OpenFileParams,
131
227
  PdfToImageParams,
132
- PrinterImageType,
228
+ PrinterStatus,
229
+ PrinterStatusParams,
133
230
  PrintHtmlParams,
134
231
  PrintImageBase64Params,
135
232
  PrintImageParams,
136
233
  PrintPdfParams,
137
234
  PrintTextParams,
235
+ SaveFileParams,
236
+ SaveFileResult,
237
+ UsbDeviceParams,
238
+ UsbPrinterDevice,
138
239
  };
139
240
 
140
241
  // Export native module