Skip to content

Commit

Permalink
Merge pull request #37 from fragilehm/feature/allow-passing-scanning-…
Browse files Browse the repository at this point in the history
…timeout

Feature: allow passing scanning timeouts
  • Loading branch information
tr3v3r authored Nov 16, 2021
2 parents c02cb06 + ee8f3b9 commit 5c44f71
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,28 @@ private void startDiscovery(MyCallbackInterface callback, ReadableMap paramsMap)
}
}

private void performDiscovery(MyCallbackInterface callback) {
private void performDiscovery(MyCallbackInterface callback, ReadableMap paramsMap) {
final Handler handler = new Handler();

// Default to 5000 if the value is not passed.
int scanningTimeout = 5000

if (paramsMap != null) {
if (paramsMap.hasKey("scanningTimeoutAndroid")) {
scanningTimeout = paramsMap.getInt("scanningTimeoutAndroid");
}
}
final Runnable r = new Runnable() {
public void run() {
stopDiscovery();
if (mPrinterList.size() > 0) {
sendDiscoveredDataToJS(); // will be invoked after 5 sec with acc. results
sendDiscoveredDataToJS(); // will be invoked after {scanningTimeout / 1000} sec with acc. results
}
callback.onDone("Search completed");
}
};

handler.postDelayed(r, 5000);
handler.postDelayed(r, scanningTimeout);
}

private String getUSBAddress(String target) {
Expand Down Expand Up @@ -265,6 +273,6 @@ public void onDone(String result) {
public void onDone(String result) {
promise.resolve(result);
}
});
}, paramsMap);
}
}
13 changes: 7 additions & 6 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
4. [getPrinterCharsPerLine](#getprintercharsperlineseriesname)
5. [startMonitorPrinter](#startmonitorprinterinterval-number)
6. [stopMonitorPrinter](#stopmonitorprinter)
1. [printing](./PRINTING.md)

7. [printing](./PRINTING.md)

### init({ target, seriesName, language? })

Expand All @@ -19,7 +18,7 @@ Initializes printer using it's target and series name.
| ------------ | :------: | :------: | :---------------------------------------------------------------------------------------------------------------------------------------------: |
| `target` | `string` | `Yes` | The connection target of a device which can be specified by connectAPI: ("TCP:192.168.192.168" "BT:00:22:15:7D:70:9C" "USB:000000000000000000") |
| `seriesName` | `string` | `Yes` | Specifies the target printer model. |
| `language` | `string` | `No` | Specifies the language : EPOS2_LANG_EN, EPOS2_LANG_JA, EPOS2_LANG_ZH_CN... |
| `language` | `string` | `No` | Specifies the language : EPOS2_LANG_EN, EPOS2_LANG_JA, EPOS2_LANG_ZH_CN... |

```javascript
import EscPosPrinter from 'react-native-esc-pos-printer';
Expand All @@ -42,9 +41,11 @@ Returns list of printers.

#### params

| Name | Type | Required | Description |
| ----------------- | :-------: | :------: | :-------------------------------------------------------: |
| `usbSerialNumber` | `boolean` | `No` | To extract the serial number of the usb device on Android |
| Name | Type | Required | Default | Description |
| ------------------------ | :-------: | :------: | :-----: | :----------------------------------------------------------: |
| `usbSerialNumber` | `boolean` | `No` | `false` | To extract the serial number of the usb device on Android |
| `scanningTimeoutIOS` | `boolean` | `No` | `5000` | Timeout in milliseconds for scanning the printers on iOS |
| `scanningTimeoutAndroid` | `boolean` | `No` | `5000` | Timeout in milliseconds for scanning the printers on Android |

#### return type

Expand Down
10 changes: 7 additions & 3 deletions ios/EscPosPrinterDiscovery.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ @implementation EscPosPrinterDiscovery
}

RCT_REMAP_METHOD(discover,
params:(NSDictionary *)params
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
Expand All @@ -27,7 +28,7 @@ @implementation EscPosPrinterDiscovery
[self performDiscovery:^(NSString *result) {
resolve(result);

}];
} params:params];
}

- (void) startDiscovery: (void(^)(NSString *))onError
Expand Down Expand Up @@ -55,9 +56,12 @@ - (void)stopDiscovery
}
}

- (void) performDiscovery: (void(^)(NSString *))onFinish
- (void) performDiscovery: (void(^)(NSString *))onFinish params:(NSDictionary *)params
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// Default to 5000 if the value is not passed.
int scanningTimeout = (int)[params[@"scanningTimeoutIOS"] integerValue] ?: 5000;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, scanningTimeout * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
[self stopDiscovery];
onFinish(@"Search completed");
});
Expand Down
8 changes: 1 addition & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,8 @@ const _default = {
removeListener();
}
);
let promise;
if (Platform.OS === 'ios') {
promise = EscPosPrinterDiscovery.discover();
} else {
promise = EscPosPrinterDiscovery.discover(params);
}

promise
EscPosPrinterDiscovery.discover(params)
.then(() => {
removeListener();
res([]);
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,18 @@ export interface IPrinterInitParams {
}

export interface IDiscoverParams {
/**
* Whether to extract the serial number of the usb device on Android
*/
usbSerialNumber?: boolean;
/**
* Timeout in milliseconds for scanning the printers on iOS (default 5000 - 5 seconds)
*/
scanningTimeoutIOS?: number;
/**
* Timeout in milliseconds for scanning the printers on Android (default 5000 - 5 seconds)
*/
scanningTimeoutAndroid?: number;
}

export interface IMonitorStatus {
Expand Down

0 comments on commit 5c44f71

Please sign in to comment.