Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout for BT discovery scan #662

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
discoverCancelable = terminal.discoverReaders(
config = when (discoveryMethod) {
DiscoveryMethod.BLUETOOTH_SCAN -> DiscoveryConfiguration.BluetoothDiscoveryConfiguration(
0,
getInt(params, "timeout") ?: 0,
getBoolean(params, "simulated")
)
DiscoveryMethod.INTERNET -> DiscoveryConfiguration.InternetDiscoveryConfiguration(
isSimulated = getBoolean(params, "simulated")
)
DiscoveryMethod.USB -> DiscoveryConfiguration.UsbDiscoveryConfiguration(
0,
getInt(params, "timeout") ?: 0,
getBoolean(params, "simulated")
)
DiscoveryMethod.HANDOFF -> DiscoveryConfiguration.HandoffDiscoveryConfiguration()
Expand Down
1 change: 1 addition & 0 deletions dev-app/src/screens/DiscoverReadersScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export default function DiscoverReadersScreen() {
const { error: discoverReadersError } = await discoverReaders({
discoveryMethod,
simulated,
timeout: 0,
});

if (discoverReadersError) {
Expand Down
6 changes: 3 additions & 3 deletions ios/Mappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ class Mappers {
return DiscoveryMethod.internet
}

class func mapToDiscoveryConfiguration(_ discoveryMethod: String?, simulated: Bool) throws-> DiscoveryConfiguration {
class func mapToDiscoveryConfiguration(_ discoveryMethod: String?, simulated: Bool, timeout: UInt) throws-> DiscoveryConfiguration {
switch discoveryMethod {
case "bluetoothScan":
return try BluetoothScanDiscoveryConfigurationBuilder().setSimulated(simulated).build()
return try BluetoothScanDiscoveryConfigurationBuilder().setSimulated(simulated).setTimeout(timeout).build()
case "bluetoothProximity":
return try BluetoothProximityDiscoveryConfigurationBuilder().setSimulated(simulated).build()
case "internet":
Expand All @@ -134,7 +134,7 @@ class Mappers {
return try LocalMobileDiscoveryConfigurationBuilder().setSimulated(simulated).build()
@unknown default:
print("⚠️ Unknown discovery method! Defaulting to Bluetooth Scan.")
return try BluetoothScanDiscoveryConfigurationBuilder().setSimulated(simulated).build()
return try BluetoothScanDiscoveryConfigurationBuilder().setSimulated(simulated).setTimeout(timeout).build()
}
}

Expand Down
5 changes: 3 additions & 2 deletions ios/StripeTerminalReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
func discoverReaders(params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
let simulated = params["simulated"] as? Bool
let discoveryMethod = params["discoveryMethod"] as? String

let timeout = params["timeout"] as? UInt ?? 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bric-stripe any issues with setting the timeout to 0 by default?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, no concerns. that's the native SDKs default as well 👍


let config: DiscoveryConfiguration
do {
config = try Mappers.mapToDiscoveryConfiguration(discoveryMethod, simulated: simulated ?? false)
config = try Mappers.mapToDiscoveryConfiguration(discoveryMethod, simulated: simulated ?? false, timeout: timeout)
} catch {
resolve(Errors.createError(nsError: error as NSError))
return
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type LogLevelIOS = 'none' | 'verbose';
export type LogLevelAndroid = 'none' | 'verbose' | 'error' | 'warning';

export type DiscoverReadersParams = {
timeout: number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make this an optional param

Suggested change
timeout: number;
timeout?: number;

simulated?: boolean;
discoveryMethod: Reader.DiscoveryMethod;
};
Expand Down